You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
12 KiB
1 line
12 KiB
11 months ago
|
{"version":3,"file":"util.mjs","sources":["../../../../packages/utils/util.ts"],"sourcesContent":["import {\n camelize,\n capitalize,\n extend,\n hasOwn,\n hyphenate,\n isArray,\n isObject,\n isString,\n isFunction,\n looseEqual,\n toRawType,\n} from '@vue/shared'\nimport isEqualWith from 'lodash/isEqualWith'\nimport { isClient } from '@vueuse/core'\nimport { debugWarn, throwError } from './error'\n\nimport type { ComponentPublicInstance, CSSProperties, Ref } from 'vue'\nimport type { TimeoutHandle, Nullable } from './types'\n\nexport const SCOPE = 'Util'\n\nexport function toObject<T>(arr: Array<T>): Record<string, T> {\n const res = {}\n for (let i = 0; i < arr.length; i++) {\n if (arr[i]) {\n extend(res, arr[i])\n }\n }\n return res\n}\n\nexport const getValueByPath = (obj, paths = ''): unknown => {\n let ret: unknown = obj\n paths.split('.').map((path) => {\n ret = ret?.[path]\n })\n return ret\n}\n\nexport function getPropByPath(\n obj: any,\n path: string,\n strict: boolean\n): {\n o: unknown\n k: string\n v: Nullable<unknown>\n} {\n let tempObj = obj\n let key, value\n\n if (obj && hasOwn(obj, path)) {\n key = path\n value = tempObj?.[path]\n } else {\n path = path.replace(/\\[(\\w+)\\]/g, '.$1')\n path = path.replace(/^\\./, '')\n\n const keyArr = path.split('.')\n let i = 0\n for (i; i < keyArr.length - 1; i++) {\n if (!tempObj && !strict) break\n const key = keyArr[i]\n\n if (key in tempObj) {\n tempObj = tempObj[key]\n } else {\n if (strict) {\n throwError(SCOPE, 'Please transfer a valid prop path to form item!')\n }\n break\n }\n }\n key = keyArr[i]\n value = tempObj?.[keyArr[i]]\n }\n return {\n o: tempObj,\n k: key,\n v: value,\n }\n}\n\n/**\n * Generate random number in range [0, 1000]\n * Maybe replace with [uuid](https://www.npmjs.com/package/uuid)\n */\nexport const generateId = (): number => Math.floor(Math.random() * 10000)\n\n// use isEqual instead\n// export const valueEquals\n\nexport const escapeRegexpString = (value = ''): string =>\n String(value).replace(/[|\\\\{}()[\\]^$+*?.]/g, '\\\\$&')\n\n// Use native Array.find, Array.findIndex instead\n\n// coerce truthy value to array\nexport const coerceTruthyValueToArray = (arr) => {\n if (!arr && arr !== 0) {\n return []\n }\n return Array.isArray(arr) ? arr : [arr]\n}\n\n// drop IE and (Edge < 79) support\n// export const isIE\n// export const isEdge\n\nexport const isFirefox = function (): boolean {\n return isClient && !!window.navigator.userAgent.match(/firefox/i)\n}\n\nexport const autoprefixer = function (style: CSSProperties): CSSProperties {\n const rules = ['transform', 'transition', 'animation']\n const prefixes = ['ms-', 'webkit-']\n rules.forEach((rule) => {\n const value = style[rule]\n if (rule && value) {\n prefixes.forEach((prefix) => {\n style[prefix + rule] = value\n })\n }\n })\n return style\n}\n\nexport const kebabCase = hyphenate\n\n// reexport from lodash & vue shared\nexport { isVNode } from 'vue'\nexport {\n hasOwn,\n // isEmpty,\n // isEqual,\n isObject,\n isArray,\n isString,\n capitalize,\n camelize,\n looseEqual,\n extend,\n}\n\nexport const isBool = (val: unknown): val is boolean => typeof val === 'boolean'\nexport const isNumber = (val: unknown): val is number => typeof val === 'number'\nexport const isHTMLElement = (val: unknown) => toRawType(val).startsWith('HTML')\n\nexport function rafThrottle<T extends (...args: any) => any>(fn: T): T {\n let locked = false\n return function (this: ThisParameterType<T>, ...args: any[]) {\n if (locked) return\n locked = true\n\n window.requestAnimationFrame(() => {\n Reflect.apply(fn, this, args)\n locked = false\n })\n } as T\n}\n\nexport const clearTimer = (timer: Ref<TimeoutHandle>) => {\n clearTimeout(timer.value)\n timer.value = null\n}\n\n/**\n * Generating a random int in range (0, max - 1)\n * @param max {number}\n */\nexport function getRandomInt(max: nu
|