vuepress-plugin-element-tabs-less
Version:
Vuepress plugin - Tabs Container for Vuepress without SASS
135 lines (116 loc) • 3.14 kB
JavaScript
import Vue from 'vue';
const hasOwnProperty = Object.prototype.hasOwnProperty;
export function noop() {}
export function hasOwn(obj, key) {
return hasOwnProperty.call(obj, key);
}
function extend(to, _from) {
for (let key in _from) {
to[key] = _from[key];
}
return to;
}
export function toObject(arr) {
var res = {};
for (let i = 0; i < arr.length; i++) {
if (arr[i]) {
extend(res, arr[i]);
}
}
return res;
}
export const getValueByPath = (object, prop) => {
prop = prop || '';
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;
};
export 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,
};
}
export const generateId = () => Math.floor(Math.random() * 10000);
export 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;
};
export const escapeRegexpString = (value = '') =>
String(value).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
// TODO: use native Array.find, Array.findIndex when IE support is dropped
export const arrayFindIndex = (arr, pred) => {
for (let i = 0; i !== arr.length; ++i) {
if (pred(arr[i])) {
return i;
}
}
return -1;
};
export const arrayFind = (arr, pred) => {
const idx = arrayFindIndex(arr, pred);
return idx !== -1 ? arr[idx] : undefined;
};
// coerce truthy value to array
export const coerceTruthyValueToArray = val => {
if (Array.isArray(val)) {
return val;
} else if (val) {
return [val];
} else {
return [];
}
};
export const isIE = () =>
!Vue.prototype.$isServer && !isNaN(Number(document.documentMode));
export const isEdge = () =>
!Vue.prototype.$isServer && navigator.userAgent.indexOf('Edge') > -1;
export const 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;
};