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 origDefineProperty = Object.defineProperty;
|
---|
9 |
|
---|
10 | var isFunction = function (fn) {
|
---|
11 | return typeof fn === 'function' && toStr.call(fn) === '[object Function]';
|
---|
12 | };
|
---|
13 |
|
---|
14 | var arePropertyDescriptorsSupported = function () {
|
---|
15 | var obj = {};
|
---|
16 | try {
|
---|
17 | origDefineProperty(obj, 'x', { enumerable: false, value: obj });
|
---|
18 | // eslint-disable-next-line no-unused-vars, no-restricted-syntax
|
---|
19 | for (var _ in obj) { // jscs:ignore disallowUnusedVariables
|
---|
20 | return false;
|
---|
21 | }
|
---|
22 | return obj.x === obj;
|
---|
23 | } catch (e) { /* this is IE 8. */
|
---|
24 | return false;
|
---|
25 | }
|
---|
26 | };
|
---|
27 | var supportsDescriptors = origDefineProperty && arePropertyDescriptorsSupported();
|
---|
28 |
|
---|
29 | var defineProperty = function (object, name, value, predicate) {
|
---|
30 | if (name in object && (!isFunction(predicate) || !predicate())) {
|
---|
31 | return;
|
---|
32 | }
|
---|
33 | if (supportsDescriptors) {
|
---|
34 | origDefineProperty(object, name, {
|
---|
35 | configurable: true,
|
---|
36 | enumerable: false,
|
---|
37 | value: value,
|
---|
38 | writable: true
|
---|
39 | });
|
---|
40 | } else {
|
---|
41 | object[name] = value;
|
---|
42 | }
|
---|
43 | };
|
---|
44 |
|
---|
45 | var defineProperties = function (object, map) {
|
---|
46 | var predicates = arguments.length > 2 ? arguments[2] : {};
|
---|
47 | var props = keys(map);
|
---|
48 | if (hasSymbols) {
|
---|
49 | props = concat.call(props, Object.getOwnPropertySymbols(map));
|
---|
50 | }
|
---|
51 | for (var i = 0; i < props.length; i += 1) {
|
---|
52 | defineProperty(object, props[i], map[props[i]], predicates[props[i]]);
|
---|
53 | }
|
---|
54 | };
|
---|
55 |
|
---|
56 | defineProperties.supportsDescriptors = !!supportsDescriptors;
|
---|
57 |
|
---|
58 | module.exports = defineProperties;
|
---|