2023-12-18 13:12:25 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var keys = require('object-keys');
|
|
|
|
var hasSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbol';
|
|
|
|
|
|
|
|
var toStr = Object.prototype.toString;
|
|
|
|
var concat = Array.prototype.concat;
|
2023-12-28 23:41:32 +08:00
|
|
|
var defineDataProperty = require('define-data-property');
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
var isFunction = function (fn) {
|
|
|
|
return typeof fn === 'function' && toStr.call(fn) === '[object Function]';
|
|
|
|
};
|
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
var supportsDescriptors = require('has-property-descriptors')();
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
var defineProperty = function (object, name, value, predicate) {
|
2023-12-28 23:41:32 +08:00
|
|
|
if (name in object) {
|
|
|
|
if (predicate === true) {
|
|
|
|
if (object[name] === value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (!isFunction(predicate) || !predicate()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
2023-12-28 23:41:32 +08:00
|
|
|
|
2023-12-18 13:12:25 +08:00
|
|
|
if (supportsDescriptors) {
|
2023-12-28 23:41:32 +08:00
|
|
|
defineDataProperty(object, name, value, true);
|
2023-12-18 13:12:25 +08:00
|
|
|
} else {
|
2023-12-28 23:41:32 +08:00
|
|
|
defineDataProperty(object, name, value);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var defineProperties = function (object, map) {
|
|
|
|
var predicates = arguments.length > 2 ? arguments[2] : {};
|
|
|
|
var props = keys(map);
|
|
|
|
if (hasSymbols) {
|
|
|
|
props = concat.call(props, Object.getOwnPropertySymbols(map));
|
|
|
|
}
|
|
|
|
for (var i = 0; i < props.length; i += 1) {
|
|
|
|
defineProperty(object, props[i], map[props[i]], predicates[props[i]]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
defineProperties.supportsDescriptors = !!supportsDescriptors;
|
|
|
|
|
|
|
|
module.exports = defineProperties;
|