UNPKG

element-plus

Version:
238 lines (237 loc) 8.61 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.refAttacher = exports.isEqualWithFunction = exports.addUnit = exports.$ = exports.deduplicate = exports.arrayFlat = exports.isEmpty = exports.arrayFind = exports.arrayFindIndex = exports.useGlobalConfig = exports.isVNode = exports.isUndefined = exports.entries = exports.getRandomInt = exports.clearTimer = exports.rafThrottle = exports.isHTMLElement = exports.isNumber = exports.isBool = exports.extend = exports.looseEqual = exports.camelize = exports.capitalize = exports.isString = exports.isArray = exports.isObject = exports.hasOwn = exports.kebabCase = exports.autoprefixer = exports.isFirefox = exports.isEdge = exports.isIE = exports.coerceTruthyValueToArray = exports.escapeRegexpString = exports.generateId = exports.getPropByPath = exports.getValueByPath = exports.toObject = exports.SCOPE = void 0; const vue_1 = require("vue"); const shared_1 = require("@vue/shared"); Object.defineProperty(exports, "camelize", { enumerable: true, get: function () { return shared_1.camelize; } }); Object.defineProperty(exports, "capitalize", { enumerable: true, get: function () { return shared_1.capitalize; } }); Object.defineProperty(exports, "extend", { enumerable: true, get: function () { return shared_1.extend; } }); Object.defineProperty(exports, "hasOwn", { enumerable: true, get: function () { return shared_1.hasOwn; } }); Object.defineProperty(exports, "isArray", { enumerable: true, get: function () { return shared_1.isArray; } }); Object.defineProperty(exports, "isObject", { enumerable: true, get: function () { return shared_1.isObject; } }); Object.defineProperty(exports, "isString", { enumerable: true, get: function () { return shared_1.isString; } }); Object.defineProperty(exports, "looseEqual", { enumerable: true, get: function () { return shared_1.looseEqual; } }); const isEqualWith_1 = __importDefault(require("lodash/isEqualWith")); const isServer_1 = __importDefault(require("./isServer")); const error_1 = require("./error"); exports.SCOPE = 'Util'; function toObject(arr) { const res = {}; for (let i = 0; i < arr.length; i++) { if (arr[i]) { shared_1.extend(res, arr[i]); } } return res; } exports.toObject = toObject; const getValueByPath = (obj, paths = '') => { let ret = obj; paths.split('.').map(path => { ret = ret === null || ret === void 0 ? void 0 : ret[path]; }); return ret; }; exports.getValueByPath = getValueByPath; function getPropByPath(obj, path, strict) { let tempObj = obj; path = path.replace(/\[(\w+)\]/g, '.$1'); path = path.replace(/^\./, ''); const keyArr = path.split('.'); let i = 0; for (i; i < keyArr.length - 1; i++) { if (!tempObj && !strict) break; const key = keyArr[i]; if (key in tempObj) { tempObj = tempObj[key]; } else { if (strict) { throw new Error('please transfer a valid prop path to form item!'); } break; } } return { o: tempObj, k: keyArr[i], v: tempObj === null || tempObj === void 0 ? void 0 : tempObj[keyArr[i]], }; } exports.getPropByPath = getPropByPath; /** * Generate random number in range [0, 1000] * Maybe replace with [uuid](https://www.npmjs.com/package/uuid) */ const generateId = () => Math.floor(Math.random() * 10000); exports.generateId = generateId; // use isEqual instead // export const valueEquals const escapeRegexpString = (value = '') => String(value).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'); exports.escapeRegexpString = escapeRegexpString; // Use native Array.find, Array.findIndex instead // coerce truthy value to array const coerceTruthyValueToArray = arr => { if (!arr && arr !== 0) { return []; } return Array.isArray(arr) ? arr : [arr]; }; exports.coerceTruthyValueToArray = coerceTruthyValueToArray; const isIE = function () { return !isServer_1.default && !isNaN(Number(document.documentMode)); }; exports.isIE = isIE; const isEdge = function () { return !isServer_1.default && navigator.userAgent.indexOf('Edge') > -1; }; exports.isEdge = isEdge; const isFirefox = function () { return !isServer_1.default && !!window.navigator.userAgent.match(/firefox/i); }; exports.isFirefox = isFirefox; const autoprefixer = function (style) { const rules = ['transform', 'transition', 'animation']; const prefixes = ['ms-', 'webkit-']; rules.forEach(rule => { const value = style[rule]; if (rule && value) { prefixes.forEach(prefix => { style[prefix + rule] = value; }); } }); return style; }; exports.autoprefixer = autoprefixer; exports.kebabCase = shared_1.hyphenate; const isBool = (val) => typeof val === 'boolean'; exports.isBool = isBool; const isNumber = (val) => typeof val === 'number'; exports.isNumber = isNumber; const isHTMLElement = (val) => shared_1.toRawType(val).startsWith('HTML'); exports.isHTMLElement = isHTMLElement; function rafThrottle(fn) { let locked = false; return function (...args) { if (locked) return; locked = true; window.requestAnimationFrame(() => { fn.apply(this, args); locked = false; }); }; } exports.rafThrottle = rafThrottle; const clearTimer = (timer) => { clearTimeout(timer.value); timer.value = null; }; exports.clearTimer = clearTimer; /** * Generating a random int in range (0, max - 1) * @param max {number} */ function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } exports.getRandomInt = getRandomInt; function entries(obj) { return Object .keys(obj) .map((key) => ([key, obj[key]])); } exports.entries = entries; function isUndefined(val) { return val === void 0; } exports.isUndefined = isUndefined; var vue_2 = require("vue"); Object.defineProperty(exports, "isVNode", { enumerable: true, get: function () { return vue_2.isVNode; } }); function useGlobalConfig() { const vm = vue_1.getCurrentInstance(); if ('$ELEMENT' in vm.proxy) { return vm.proxy.$ELEMENT; } return {}; } exports.useGlobalConfig = useGlobalConfig; const arrayFindIndex = function (arr, pred) { return arr.findIndex(pred); }; exports.arrayFindIndex = arrayFindIndex; const arrayFind = function (arr, pred) { return arr.find(pred); }; exports.arrayFind = arrayFind; function isEmpty(val) { if (!val && val !== 0 || shared_1.isArray(val) && !val.length || shared_1.isObject(val) && !Object.keys(val).length) return true; return false; } exports.isEmpty = isEmpty; function arrayFlat(arr) { return arr.reduce((acm, item) => { const val = Array.isArray(item) ? arrayFlat(item) : item; return acm.concat(val); }, []); } exports.arrayFlat = arrayFlat; function deduplicate(arr) { return Array.from(new Set(arr)); } exports.deduplicate = deduplicate; /** * Unwraps refed value * @param ref Refed value */ function $(ref) { return ref.value; } exports.$ = $; function addUnit(value) { if (shared_1.isString(value)) { return value; } else if (exports.isNumber(value)) { return value + 'px'; } if (process.env.NODE_ENV === 'development') { error_1.warn(exports.SCOPE, 'binding value must be a string or number'); } return ''; } exports.addUnit = addUnit; /** * Enhance `lodash.isEqual` for it always return false even two functions have completely same statements. * @param obj The value to compare * @param other The other value to compare * @returns Returns `true` if the values are equivalent, else `false`. * @example * lodash.isEqual(() => 1, () => 1) // false * isEqualWith(() => 1, () => 1) // true */ function isEqualWithFunction(obj, other) { return isEqualWith_1.default(obj, other, (objVal, otherVal) => { return shared_1.isFunction(objVal) && shared_1.isFunction(otherVal) ? `${objVal}` === `${otherVal}` : undefined; }); } exports.isEqualWithFunction = isEqualWithFunction; /** * Generate function for attach ref for the h renderer * @param ref Ref<HTMLElement | ComponentPublicInstance> * @returns (val: T) => void */ const refAttacher = (ref) => { return (val) => { ref.value = val; }; }; exports.refAttacher = refAttacher;