UNPKG

@xmini/x-mini

Version:

封装小程序

60 lines (47 loc) 1.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.find = find; exports.deepCopy = deepCopy; exports.forEachValue = forEachValue; exports.isObject = isObject; exports.isPromise = isPromise; function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function find(list, f) { return list.filter(f)[0]; } function deepCopy(obj) { var cache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; // just return if obj is immutable value if (obj === null || _typeof(obj) !== 'object') { return obj; } // if obj is hit, it is in circular structure var hit = find(cache, function (c) { return c.original === obj; }); if (hit) { return hit.copy; } var copy = Array.isArray(obj) ? [] : {}; // put the copy into cache at first // because we want to refer it in recursive deepCopy cache.push({ original: obj, copy: copy }); Object.keys(obj).forEach(function (key) { copy[key] = deepCopy(obj[key], cache); }); return copy; } function forEachValue(obj, fn) { Object.keys(obj).forEach(function (key) { return fn(obj[key], key); }); } function isObject(obj) { return obj !== null && _typeof(obj) === 'object'; } function isPromise(val) { return val && typeof val.then === 'function'; }