Blame view
api/dashboard/src/utils/functions.js
2.62 KB
0d8c0f816 initial commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
/** * Get cookie from request. * * @param {Object} req * @param {String} key * @return {String|undefined} */ export function cookieFromRequest (req, key) { if (!req.headers.cookie) { return } const cookie = req.headers.cookie.split(';').find( c => c.trim().startsWith(`${key}=`) ) if (cookie) { return cookie.split('=')[1] } } /** * https://router.vuejs.org/en/advanced/scroll-behavior.html */ export function scrollBehavior (to, from, savedPosition) { if (savedPosition) { return savedPosition } let position = {} if (to.matched.length < 2) { position = { x: 0, y: 0 } } else if (to.matched.some(r => r.components.default.options.scrollToTop)) { position = { x: 0, y: 0 } } if (to.hash) { position = { selector: to.hash } } return position } /** * Deep copy the given object. * * @param {Object} obj * @return {Object} */ export function deepCopy (obj) { if (obj === null || typeof obj !== 'object') { return obj } const copy = Array.isArray(obj) ? [] : {} Object.keys(obj).forEach((key) => { copy[key] = deepCopy(obj[key]) }) return copy } /** * If the given value is not an array, wrap it in one. * * @param {Any} value * @return {Array} */ export function arrayWrap (value) { return Array.isArray(value) ? value : [value] } export function passwordChecker () { return { strong: new RegExp('^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})'), medium: new RegExp('^(((?=.*[a-z])(?=.*[A-Z]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[A-Z])(?=.*[0-9])))(?=.{6,})') } } export function formatDateISO8601 (date) { if (Object.prototype.toString.call(date) === '[object Date]') { return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() } return null } export function formatDate (date) { if (Object.prototype.toString.call(date) === '[object Date]') { return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() } return null } export function localeArray2Assoc (arr, keyArg = 'value', valueArg = 'text') { const ret = [] if (arr) { for (const key in arr) { ret.push({ [keyArg]: key, [valueArg]: arr[key] }) } } return ret } export function debounce (fn, time) { let timeout return function () { const functionCall = () => fn.apply(this, arguments) clearTimeout(timeout) timeout = setTimeout(functionCall, time) } } export function getAge (birthDate) { const ageDifMs = Date.now() - new Date(birthDate).getTime() const ageDate = new Date(ageDifMs) // milliseconds from epoch return Math.abs(ageDate.getUTCFullYear() - 1970) } |