element-nice-ui
Version:
A Component Library for Vue.js.
249 lines (245 loc) • 7.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.generateId = exports.escapeRegexpString = exports.coerceTruthyValueToArray = exports.capitalize = exports.autoprefixer = exports.arrayFindIndex = exports.arrayFind = exports.arrayEquals = void 0;
exports.getDefined = getDefined;
exports.getPropByPath = getPropByPath;
exports.getValueByPath = void 0;
exports.hasOwn = hasOwn;
exports.looseEqual = exports.kebabCase = exports.isIE = exports.isFirefox = exports.isEqual = exports.isEmpty = exports.isEdge = void 0;
exports.noop = noop;
exports.objToArray = objToArray;
exports.rafThrottle = rafThrottle;
exports.toObject = toObject;
exports.valueEquals = void 0;
var _vue = _interopRequireDefault(require("vue"));
var _types = require("element-nice-ui/src/utils/types");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const hasOwnProperty = Object.prototype.hasOwnProperty;
function noop() {}
;
function hasOwn(obj, key) {
return hasOwnProperty.call(obj, key);
}
;
function extend(to, _from) {
for (let key in _from) {
to[key] = _from[key];
}
return to;
}
;
function toObject(arr) {
var res = {};
for (let i = 0; i < arr.length; i++) {
if (arr[i]) {
extend(res, arr[i]);
}
}
return res;
}
;
const getValueByPath = exports.getValueByPath = function getValueByPath(object, prop) {
if (!prop) return object;
const paths = prop.split('.');
let current = object;
let result = null;
for (let i = 0, j = paths.length; i < j; i++) {
const path = paths[i];
if (!current) break;
if (i === j - 1) {
result = current[path];
break;
}
current = current[path];
}
return result;
};
function getPropByPath(obj, path, strict) {
let tempObj = obj;
path = path.replace(/\[(\w+)\]/g, '.$1');
path = path.replace(/^\./, '');
let keyArr = path.split('.');
let i = 0;
for (let len = keyArr.length; i < len - 1; ++i) {
if (!tempObj && !strict) break;
let 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 ? tempObj[keyArr[i]] : null
};
}
;
const generateId = exports.generateId = function generateId() {
return Math.floor(Math.random() * 10000);
};
const valueEquals = (a, b) => {
// see: https://stackoverflow.com/questions/3115982/how-to-check-if-two-arrays-are-equal-with-javascript
if (a === b) return true;
if (!(a instanceof Array)) return false;
if (!(b instanceof Array)) return false;
if (a.length !== b.length) return false;
for (let i = 0; i !== a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
};
exports.valueEquals = valueEquals;
const escapeRegexpString = exports.escapeRegexpString = function escapeRegexpString() {
let value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
return String(value).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
};
// TODO: use native Array.find, Array.findIndex when IE support is dropped
const arrayFindIndex = exports.arrayFindIndex = function arrayFindIndex(arr, pred) {
for (let i = 0; i !== arr.length; ++i) {
if (pred(arr[i])) {
return i;
}
}
return -1;
};
const arrayFind = exports.arrayFind = function arrayFind(arr, pred) {
const idx = arrayFindIndex(arr, pred);
return idx !== -1 ? arr[idx] : undefined;
};
// coerce truthy value to array
const coerceTruthyValueToArray = exports.coerceTruthyValueToArray = function coerceTruthyValueToArray(val) {
if (Array.isArray(val)) {
return val;
} else if (val) {
return [val];
} else {
return [];
}
};
const isIE = exports.isIE = function isIE() {
return !_vue.default.prototype.$isServer && !isNaN(Number(document.documentMode));
};
const isEdge = exports.isEdge = function isEdge() {
return !_vue.default.prototype.$isServer && navigator.userAgent.indexOf('Edge') > -1;
};
const isFirefox = exports.isFirefox = function isFirefox() {
return !_vue.default.prototype.$isServer && !!window.navigator.userAgent.match(/firefox/i);
};
const autoprefixer = exports.autoprefixer = function autoprefixer(style) {
if (typeof style !== 'object') return 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;
};
const kebabCase = exports.kebabCase = function kebabCase(str) {
const hyphenateRE = /([^-])([A-Z])/g;
return str.replace(hyphenateRE, '$1-$2').replace(hyphenateRE, '$1-$2').toLowerCase();
};
const capitalize = exports.capitalize = function capitalize(str) {
if (!(0, _types.isString)(str)) return str;
return str.charAt(0).toUpperCase() + str.slice(1);
};
const looseEqual = exports.looseEqual = function looseEqual(a, b) {
const isObjectA = (0, _types.isObject)(a);
const isObjectB = (0, _types.isObject)(b);
if (isObjectA && isObjectB) {
return JSON.stringify(a) === JSON.stringify(b);
} else if (!isObjectA && !isObjectB) {
return String(a) === String(b);
} else {
return false;
}
};
const arrayEquals = exports.arrayEquals = function arrayEquals(arrayA, arrayB) {
arrayA = arrayA || [];
arrayB = arrayB || [];
if (arrayA.length !== arrayB.length) {
return false;
}
for (let i = 0; i < arrayA.length; i++) {
if (!looseEqual(arrayA[i], arrayB[i])) {
return false;
}
}
return true;
};
const isEqual = exports.isEqual = function isEqual(value1, value2) {
if (Array.isArray(value1) && Array.isArray(value2)) {
return arrayEquals(value1, value2);
}
return looseEqual(value1, value2);
};
const isEmpty = exports.isEmpty = function isEmpty(val) {
// null or undefined
if (val == null) return true;
if (typeof val === 'boolean') return false;
if (typeof val === 'number') return !val;
if (val instanceof Error) return val.message === '';
switch (Object.prototype.toString.call(val)) {
// String or Array
case '[object String]':
case '[object Array]':
return !val.length;
// Map or Set or File
case '[object File]':
case '[object Map]':
case '[object Set]':
{
return !val.size;
}
// Plain Object
case '[object Object]':
{
return !Object.keys(val).length;
}
}
return false;
};
function rafThrottle(fn) {
let locked = false;
return function () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
if (locked) return;
locked = true;
window.requestAnimationFrame(_ => {
fn.apply(this, args);
locked = false;
});
};
}
function objToArray(obj) {
if (Array.isArray(obj)) {
return obj;
}
return isEmpty(obj) ? [] : [obj];
}
function getDefined() {
let ret = undefined;
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
args.some(arg => {
if (arg !== undefined) {
ret = arg;
return true;
}
});
return ret;
}