123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- export function isArray(value) {
- if (typeof Array.isArray === 'function') {
- return Array.isArray(value);
- } else {
- return Object.prototype.toString.call(value) === '[object Array]';
- }
- }
- export function isObject(value) {
- return Object.prototype.toString.call(value) === '[object Object]';
- }
- export function isNumber(value) {
- return !isNaN(Number(value));
- }
- export function isFunction(value) {
- return typeof value == 'function';
- }
- export function isString(value) {
- return typeof value == 'string';
- }
- export function isEmpty(value) {
- if (isArray(value)) {
- return value.length === 0;
- }
- if (isObject(value)) {
- return Object.keys(value).length === 0;
- }
- return value === '' || value === undefined || value === null;
- }
- export function isBoolean(value) {
- return typeof value === 'boolean';
- }
- export function last(data) {
- if (isArray(data) || isString(data)) {
- return data[data.length - 1];
- }
- }
- export function cloneDeep(obj) {
- const d = isArray(obj) ? obj : {};
- if (isObject(obj)) {
- for (const key in obj) {
- if (obj[key]) {
- if (obj[key] && typeof obj[key] === 'object') {
- d[key] = cloneDeep(obj[key]);
- } else {
- d[key] = obj[key];
- }
- }
- }
- }
- return d;
- }
- export function clone(obj) {
- return Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
- }
- export function deepMerge(a, b) {
- let k;
- for (k in b) {
- a[k] = a[k] && a[k].toString() === '[object Object]' ? deepMerge(a[k], b[k]) : (a[k] = b[k]);
- }
- return a;
- }
- export function contains(parent, node) {
- while (node && (node = node.parentNode)) if (node === parent) return true;
- return false;
- }
- export function orderBy(list, key) {
- return list.sort((a, b) => a[key] - b[key]);
- }
- export function deepTree(list) {
- const newList = [];
- const map = {};
- list.forEach((e) => (map[e.id] = e));
- list.forEach((e) => {
- const parent = map[e.parentId];
- if (parent) {
- (parent.children || (parent.children = [])).push(e);
- } else {
- newList.push(e);
- }
- });
- const fn = (list) => {
- list.map((e) => {
- if (e.children instanceof Array) {
- e.children = orderBy(e.children, 'orderNum');
- fn(e.children);
- }
- });
- };
- fn(newList);
- return orderBy(newList, 'orderNum');
- }
- export function revDeepTree(list = []) {
- const d = [];
- let id = 0;
- const deep = (list, parentId) => {
- list.forEach((e) => {
- if (!e.id) {
- e.id = id++;
- }
- e.parentId = parentId;
- d.push(e);
- if (e.children && isArray(e.children)) {
- deep(e.children, e.id);
- }
- });
- };
- deep(list || [], null);
- return d;
- }
- export function basename(path) {
- let index = path.lastIndexOf('/');
- index = index > -1 ? index : path.lastIndexOf('\\');
- if (index < 0) {
- return path;
- }
- return path.substring(index + 1);
- }
- export function isWxBrowser() {
- const ua = navigator.userAgent.toLowerCase();
- if (ua.match(/MicroMessenger/i) == 'micromessenger') {
- return true;
- } else {
- return false;
- }
- }
- /**
- * @description 如果value小于min,取min;如果value大于max,取max
- * @param {number} min
- * @param {number} max
- * @param {number} value
- */
- export function range(min = 0, max = 0, value = 0) {
- return Math.max(min, Math.min(max, Number(value)));
- }
|