@mucfe/matui
Version:
基于Vue和ElementUi的PC组件库
346 lines (284 loc) • 8.79 kB
JavaScript
;
exports.__esModule = true;
exports.copyTypedArray = exports.copyRegExp = exports.copyObject = exports.copySet = exports.copyMap = exports.createCopyIterable = exports.copyBuffer = exports.copyArrayBuffer = exports.copyArray = exports.shouldObjectBeCopied = exports.isObjectCopyable = exports.getRegExpFlags = exports.getObjectToCopy = exports.getPrototype = exports.getProto = exports.getNewCache = void 0;
var _constants = require("./constants");
// constants
var create = Object.create,
getKeys = Object.keys,
getSymbols = Object.getOwnPropertySymbols,
getPrototypeOf = Object.getPrototypeOf;
var propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
/**
* @function getNewCache
*
* @description
* get a new cache object to prevent circular references
*
* @returns {Object|Weakset} the new cache object
*/
var getNewCache = function getNewCache() {
return _constants.HAS_WEAKSET_SUPPORT ? new WeakSet() : create({
_values: [],
add: function add(value) {
this._values.push(value);
},
has: function has(value) {
return !!~this._values.indexOf(value);
}
});
};
/**
* @function getProto
*
* @description
* get the __proto__ prototype property from the object
*
* @param {any} object the object to get the __proto__ from
* @returns {Object} tthe prototype of the object
*/
exports.getNewCache = getNewCache;
var getProto = function getProto(object) {
return object ? object.__proto__ : null;
};
/**
* @function getPrototype
*
* @description
* get the prototype of the object passed, using __proto__ when supported and
* falling back to getPrototypeOf
*
* @param {any} object the object to get the prototype of
* @returns {Object} the object's prototype
*/
exports.getProto = getProto;
var getPrototype = function () {
try {
var object = {};
if (object.__proto__ !== void 0) {
return getProto;
}
throw new Error();
} catch (error) {
return getPrototypeOf;
}
}();
/**
* @function getObjectToCopy
*
* @description
* get the object to copy, including appropriate prototype
*
* @param {Object} object the object to copy
* @param {any} realm the realm to base the object prototype on
* @param {boolean} isPlainObject is the object a plain object
* @returns {Object} an empty version of the object to copy
*/
exports.getPrototype = getPrototype;
var getObjectToCopy = function getObjectToCopy(object, realm, isPlainObject) {
if (isPlainObject) {
var prototype = getPrototype(object);
return prototype === realm.Object.prototype ? {} : create(prototype);
}
return object.constructor ? new object.constructor() : create(null);
};
/**
* @function getRegExpFlags
*
* @description
* get the flags to apply to the copied regexp
*
* @param {RegExp} regExp the regexp to get the flags of
* @returns {string} the flags for the regexp
*/
exports.getObjectToCopy = getObjectToCopy;
var getRegExpFlags = function getRegExpFlags(regExp) {
var flags = '';
if (regExp.global) {
flags += 'g';
}
if (regExp.ignoreCase) {
flags += 'i';
}
if (regExp.multiline) {
flags += 'm';
}
if (regExp.unicode) {
flags += 'u';
}
if (regExp.sticky) {
flags += 'y';
}
return flags;
};
/**
* @function isObjectCopyable
*
* @description
* is the object able to be copied
*
* @param {any} object the object to test
* @param {Object|Weakset} cache the cache of copied values
* @returns {boolean} can the object be copied
*/
exports.getRegExpFlags = getRegExpFlags;
var isObjectCopyable = function isObjectCopyable(object, cache) {
return typeof object === 'object' && object !== null && !cache.has(object);
};
/**
* @function shouldObjectBeCopied
*
* @description
* should the object be copied
*
* @param {any} object the object to test
* @param {any} realm the realm to check instanceof in
* @returns {boolean} should the object be copied
*/
exports.isObjectCopyable = isObjectCopyable;
var shouldObjectBeCopied = function shouldObjectBeCopied(object, realm) {
return typeof object.then !== 'function' && !(object instanceof realm.Error) && !(realm.WeakMap && object instanceof realm.WeakMap) && !(realm.WeakSet && object instanceof realm.WeakSet);
};
/**
* @function copyArray
*
* @description
* copy the array, deeply copying the values
*
* @param {Array<any>} array the array to copy
* @param {function} copy the function to copy values
* @param {any} realm the realm to check instanceof in
* @returns {Array<any>} the copied array
*/
exports.shouldObjectBeCopied = shouldObjectBeCopied;
var copyArray = function copyArray(array, copy, realm) {
var newArray = new array.constructor();
for (var index = 0; index < array.length; index++) {
newArray[index] = copy(array[index], realm);
}
return newArray;
};
/**
* @function copyArrayBuffer
*
* @description
* copy the arrayBuffer, deeply copying the values
*
* @param {ArrayBuffer} arrayBuffer the arrayBuffer to copy
* @returns {ArrayBuffer} the copied bufarrayBufferfer
*/
exports.copyArray = copyArray;
var copyArrayBuffer = function copyArrayBuffer(arrayBuffer) {
return arrayBuffer.slice(0);
};
/**
* @function copyBuffer
*
* @description
* copy the buffer, deeply copying the values
*
* @param {Buffer} buffer the buffer to copy
* @param {any} realm the realm to check instanceof in
* @returns {Buffer} the copied buffer
*/
exports.copyArrayBuffer = copyArrayBuffer;
var copyBuffer = function copyBuffer(buffer, realm) {
var newBuffer = realm.Buffer.allocUnsafe ? realm.Buffer.allocUnsafe(buffer.length) : new realm.Buffer(buffer.length);
buffer.copy(newBuffer);
return newBuffer;
};
/**
* @function copyIterable
*
* @description
* copy the iterable values into a new iterable of the same type
*
* @param {function} assignmentHandler the handler for assigning the values to the new iterable
* @returns {function((Map|Set), function, any): (Map|Set)} the copied iterable
*/
exports.copyBuffer = copyBuffer;
var createCopyIterable = function createCopyIterable(assignmentHandler) {
return function (iterable, copy, realm) {
var newIterable = new iterable.constructor();
iterable.forEach(assignmentHandler(newIterable, copy, realm));
return newIterable;
};
};
exports.createCopyIterable = createCopyIterable;
var copyMap = createCopyIterable(function (iterable, copy, realm) {
return function (value, key) {
return iterable.set(key, copy(value, realm));
};
});
exports.copyMap = copyMap;
var copySet = createCopyIterable(function (iterable, copy, realm) {
return function (value) {
return iterable.add(copy(value, realm));
};
});
/**
* @function copyObject
*
* @description
* copy the object values into a new object of the same type
*
* @param {Object} object the object to copy
* @param {function} copy the copy method
* @param {any} realm the realm to check instanceof in
* @param {boolean} isPlainObject is the object to copy a plain object
* @returns {Object} the copied object
*/
exports.copySet = copySet;
var copyObject = function copyObject(object, copy, realm, isPlainObject) {
var newObject = getObjectToCopy(object, realm, isPlainObject);
var keys = getKeys(object);
if (keys.length) {
var key;
for (var index = 0; index < keys.length; index++) {
key = keys[index];
newObject[key] = copy(object[key], realm);
}
}
if (_constants.HAS_PROPERTY_SYMBOL_SUPPORT) {
var symbols = getSymbols(object);
if (symbols.length) {
var symbol;
for (var _index = 0; _index < symbols.length; _index++) {
symbol = symbols[_index];
if (propertyIsEnumerable.call(object, symbol)) {
newObject[symbol] = copy(object[symbol], realm);
}
}
}
}
return newObject;
};
/**
* @function copyRegExp
*
* @description
* copy the RegExp to a new RegExp with the same properties
*
* @param {RegExp} regExp the RegExp to copy
* @param {any} realm the realm to check instanceof in
* @returns {RegExp} the copied RegExp
*/
exports.copyObject = copyObject;
var copyRegExp = function copyRegExp(regExp, realm) {
var newRegExp = new realm.RegExp(regExp.source, _constants.HAS_FLAGS_SUPPORT ? regExp.flags : getRegExpFlags(regExp));
newRegExp.lastIndex = regExp.lastIndex;
return newRegExp;
};
/**
* @function copyTypedArray
*
* @description
* copy the typedArray, deeply copying the values
*
* @param {TypedArray} typedArray the typedArray to copy
* @returns {TypedArray} the copied typedArray
*/
exports.copyRegExp = copyRegExp;
var copyTypedArray = function copyTypedArray(typedArray) {
return new typedArray.constructor(copyArrayBuffer(typedArray.buffer));
};
exports.copyTypedArray = copyTypedArray;