1 | 'use strict';
|
---|
2 |
|
---|
3 | var keys = require('object-keys');
|
---|
4 | var hasSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbol';
|
---|
5 |
|
---|
6 | var toStr = Object.prototype.toString;
|
---|
7 | var concat = Array.prototype.concat;
|
---|
8 | var defineDataProperty = require('define-data-property');
|
---|
9 |
|
---|
10 | var isFunction = function (fn) {
|
---|
11 | return typeof fn === 'function' && toStr.call(fn) === '[object Function]';
|
---|
12 | };
|
---|
13 |
|
---|
14 | var supportsDescriptors = require('has-property-descriptors')();
|
---|
15 |
|
---|
16 | var defineProperty = function (object, name, value, predicate) {
|
---|
17 | if (name in object) {
|
---|
18 | if (predicate === true) {
|
---|
19 | if (object[name] === value) {
|
---|
20 | return;
|
---|
21 | }
|
---|
22 | } else if (!isFunction(predicate) || !predicate()) {
|
---|
23 | return;
|
---|
24 | }
|
---|
25 | }
|
---|
26 |
|
---|
27 | if (supportsDescriptors) {
|
---|
28 | defineDataProperty(object, name, value, true);
|
---|
29 | } else {
|
---|
30 | defineDataProperty(object, name, value);
|
---|
31 | }
|
---|
32 | };
|
---|
33 |
|
---|
34 | var defineProperties = function (object, map) {
|
---|
35 | var predicates = arguments.length > 2 ? arguments[2] : {};
|
---|
36 | var props = keys(map);
|
---|
37 | if (hasSymbols) {
|
---|
38 | props = concat.call(props, Object.getOwnPropertySymbols(map));
|
---|
39 | }
|
---|
40 | for (var i = 0; i < props.length; i += 1) {
|
---|
41 | defineProperty(object, props[i], map[props[i]], predicates[props[i]]);
|
---|
42 | }
|
---|
43 | };
|
---|
44 |
|
---|
45 | defineProperties.supportsDescriptors = !!supportsDescriptors;
|
---|
46 |
|
---|
47 | module.exports = defineProperties;
|
---|