-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
150 lines (121 loc) · 4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* Created by tommyZZM.OSX on 2017/1/14. */
"use strict";
const curry = require("fast-curry");
const Symbol = (typeof global.Symbol === "function"
&& typeof global.Symbol.iterator === "symbol") ? global.Symbol : function (key) {
return key + "_" + Date.now() + "_" + ((Math.random() * 10).toFixed(2));
};
const KEY_ACCESSOR_VALUES = Symbol('accessorValues');
const KEY_ACCESSOR_VALUE_GETTERS = Symbol('accessorValueGetters');
const KEY_ACCESSOR_VALUE_SETTERS = Symbol('accessorValueSetters');
const OptimizedAccessors = {};
function getterOf(prop) {
return function () {
const self = this;
const accessorValues = self[KEY_ACCESSOR_VALUES];
const accessorValueGetters = self[KEY_ACCESSOR_VALUE_GETTERS];
const lastValue = accessorValues[prop];
const getter = accessorValueGetters[prop];
return getter(lastValue, prop, self);
}
}
function setterOf(prop) {
return function (newValue) {
const self = this;
const accessorValues = self[KEY_ACCESSOR_VALUES];
const accessorValueSetters = self[KEY_ACCESSOR_VALUE_SETTERS];
const setter = accessorValueSetters[prop];
let lastValue = accessorValues[prop];
lastValue = setter(
lastValue, newValue, prop, self
);
accessorValues[prop] = lastValue;
}
}
const objectAssignProperties = curry(function (descriptor, properties, object) {
const isOwnsGetter = typeof descriptor.get === "function";
const isOwnsSetter = typeof descriptor.set === "function";
const isOwnsGetterOrSetter = isOwnsGetter || isOwnsSetter;
const get = descriptor.get;
const set = descriptor.set;
const isShareAccessor = Boolean(descriptor.shareAccessor);
const _descriptor = Object.keys(descriptor)
.reduce((_descriptor, key) => {
if ( key === "get" || key === "set" ) {
return _descriptor;
}
// Cannot both specify accessors and a value or writable attribute
if ( key === "writable" && isOwnsGetterOrSetter ) {
return _descriptor;
}
_descriptor[key] = descriptor[key];
return _descriptor;
}, {});
if ( isOwnsGetterOrSetter && isShareAccessor ) {
[
KEY_ACCESSOR_VALUES,
KEY_ACCESSOR_VALUE_GETTERS,
KEY_ACCESSOR_VALUE_SETTERS
].forEach(key => {
let descriptor = Object.getOwnPropertyDescriptor(
object, key
);
if ( !descriptor ) {
Object.defineProperty(object, key, {
value: {},
enumerable: false,
configurable: false
})
}
});
}
const accessorValues = object[KEY_ACCESSOR_VALUES];
const accessorValueGetters = object[KEY_ACCESSOR_VALUE_GETTERS];
const accessorValueSetters = object[KEY_ACCESSOR_VALUE_SETTERS];
let propNames = Object.keys(properties);
if (typeof Object.getOwnPropertySymbols === "function") {
propNames.push.apply(propNames, Object.getOwnPropertySymbols(properties));
}
return Object.defineProperties(object, propNames
.reduce((_properties, prop) => {
let value = properties[prop];
if ( isOwnsGetterOrSetter && isShareAccessor ) {
accessorValues[prop] = value;
accessorValueGetters[prop] = get;
accessorValueSetters[prop] = set;
}
let accessor = [];
if (isShareAccessor) {
accessor = OptimizedAccessors[prop]
if ( !accessor ) {
accessor = [
getterOf(prop),
setterOf(prop)
];
OptimizedAccessors[prop] = accessor;
}
} else {
accessor = [
_ => get(value, prop, this),
(new_value) => value = set(value, new_value, prop, this)
];
}
let _descriptorValue = isOwnsGetterOrSetter ? Object.assign(
isOwnsGetter ? {
get: accessor[0]
} : {},
isOwnsSetter ? {
set: accessor[1]
} : {}
) : { value };
_properties[prop] = Object.assign(
{}, _descriptor, _descriptorValue
);
return _properties
}, {}));
})
/**
* @module object-assign-properties
* @function
*/
module.exports = objectAssignProperties;