-
Notifications
You must be signed in to change notification settings - Fork 0
/
propertize.js
473 lines (421 loc) · 12.5 KB
/
propertize.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/**
* Normalize target.
* @param {object|function} target
* @returns {object}
*/
function normalTarget(target) {
return typeof target === "function"
? target.prototype
: target;
}
/**
* Configure a basic property. The property will be configurable, enumerable,
* and writable. If no value is provided, the current value will be used.
* This function can be useful after you've already configured a property and
* wish it to return back to normal behavior.
* @param {object|function} target
* @param {string} prop
* @param {*} [val]
*/
function basic(target, prop, val) {
target = normalTarget(target);
if (arguments.length < 3) val = target[prop];
Object.defineProperty(target, prop, {
configurable: true,
enumerable: true,
writable: true,
value: val
});
}
/**
* Configure a field property on an object which will not be configurable. If
* no value if provided, the current value will be used.
* @param {object|function} target
* @param {string} prop
* @param {*} [val]
*/
function field(target, prop, val) {
target = normalTarget(target);
if (arguments.length < 3) val = target[prop];
Object.defineProperty(target, prop, {
configurable: false,
enumerable: true,
writable: true,
value: val
});
}
/**
* Configure a hidden property on an object which will not be enumerable. If
* no value if provided, the current value will be used.
* @param {object|function} target
* @param {string} prop
* @param {*} [val]
*/
function hidden(target, prop, val) {
target = normalTarget(target);
if (arguments.length < 3) val = target[prop];
Object.defineProperty(target, prop, {
configurable: true,
enumerable: false,
writable: true,
value: val
});
}
/**
* Configure a read-only property on an object which will not be writable. If
* no value is provided, the current value will be used.
* @param {object|function} target
* @param {string} prop
* @param {*} [val]
*/
function readonly(target, prop, val) {
target = normalTarget(target);
if (arguments.length < 3) val = target[prop];
Object.defineProperty(target, prop, {
configurable: true,
enumerable: true,
writable: false,
value: val
});
}
/**
* Configure an internal property on an object which will not be enumerable or
* writable. If no value is provided, the current value will be used.
* @param {object|function} target
* @param {string} prop
* @param {*} [val]
*/
function internal(target, prop, val) {
target = normalTarget(target);
if (arguments.length < 3) val = target[prop];
Object.defineProperty(target, prop, {
configurable: true,
enumerable: false,
writable: false,
value: val
});
}
/**
* Configure an attribute property on an object which will not be configurable
* or writable. If no value is provided, the current value will be used.
* @param {object|function} target
* @param {string} prop
* @param {*} [val]
*/
function attribute(target, prop, val) {
target = normalTarget(target);
if (arguments.length < 3) val = target[prop];
Object.defineProperty(target, prop, {
configurable: false,
enumerable: true,
writable: false,
value: val
});
}
/**
* Configure a setting property on an object which will not be configurable or
* enumerable. If no value is provided, the current value will be used.
* @param {object|function} target
* @param {string} prop
* @param {*} [val]
*/
function setting(target, prop, val) {
target = normalTarget(target);
if (arguments.length < 3) val = target[prop];
Object.defineProperty(target, prop, {
configurable: false,
enumerable: false,
writable: true,
value: val
});
}
/**
* Configure a locked property on an object which will not be configurable,
* enumerable, or writable. If no value is provided, the current value will be
* used.
* @param {object|function} target
* @param {string} prop
* @param {*} [val]
*/
function locked(target, prop, val) {
target = normalTarget(target);
if (arguments.length < 3) val = target[prop];
Object.defineProperty(target, prop, {
configurable: false,
enumerable: false,
writable: false,
value: val
});
}
/**
* Update an object property value, resetting any get/set and bypassing
* non-writable descriptor.
* @param {object|function} target
* @param {string} prop
* @param {*} val
*/
function value(target, prop, val) {
target = normalTarget(target);
var desc = describe(target, prop);
// if property is already defined, remove get/set and update value
if (desc) {
desc.value = val;
if (desc.get && !desc.set) desc.writable = false;
delete desc.get;
delete desc.set;
Object.defineProperty(target, prop, desc);
}
// just set the property value to get a basic property
else target[prop] = val;
}
/**
* Update the configuration for an object property, setting the configurable
* flag (default to true). In practice, the flag cannot be set to true once set
* to false, but it may be set to true any number of times before being set to
* false.
* @param {object|function} target
* @param {string} prop
* @param {boolean} [configurable]
*/
function configurable(target, prop, configurable) {
target = normalTarget(target);
configurable = arguments.length < 3 ? true : !!configurable;
var desc = describe(target, prop);
// if property is already defined, update its configurable flag
if (desc) {
desc.configurable = !!configurable;
Object.defineProperty(target, prop, desc);
}
// make field if configurable is being set to false
else if (!configurable) field(target, prop);
}
/**
* Update the configuration for an object property, setting the enumerable flag
* (default to true).
* @param {object|function} target
* @param {string} prop
* @param {boolean} [enumerable]
*/
function enumerable(target, prop, enumerable) {
target = normalTarget(target);
enumerable = arguments.length < 3 ? true : !!enumerable;
var desc = describe(target, prop);
// if property is already defined, update its enumerable flag
if (desc) {
desc.enumerable = !!enumerable;
Object.defineProperty(target, prop, desc);
}
// make hidden if enumerable is being set to false
else if (!enumerable) hidden(target, prop);
}
/**
* Update the configuration for an object property, setting the writable flag
* (default to true).
* @param {object|function} target
* @param {string} prop
* @param {boolean} [writable]
*/
function writable(target, prop, writable) {
target = normalTarget(target);
writable = arguments.length < 3 ? true : !!writable;
var desc = describe(target, prop);
// if property is already defined, update its writable flag
if (desc) {
desc.writable = !!writable;
Object.defineProperty(target, prop, desc);
}
// make readonly if writable is being set to false
else if (!writable) readonly(target, prop);
}
/**
* Update the configuration for an object property, setting the getter.
* @param {object|function} target
* @param {string} prop
* @param {function} getter
*/
function get(target, prop, getter) {
target = normalTarget(target);
var desc = describe(target, prop);
// if property defined, update get in descriptor
if (desc) desc.get = getter;
// otherwise create new descriptor
else desc = {
configurable: true,
enumerable: true,
get: getter,
set: undefined
};
// define property
Object.defineProperty(target, prop, desc);
}
/**
* Update the configuration for an object property, setting the setter.
* @param {object|function} target
* @param {string} prop
* @param {function} setter
*/
function set(target, prop, setter) {
target = normalTarget(target);
var desc = describe(target, prop);
// if property defined, update set in descriptor
if (desc) desc.set = setter;
// otherwise create new descriptor
else desc = {
configurable: true,
enumerable: true,
get: undefined,
set: setter
};
// define property
Object.defineProperty(target, prop, desc);
}
/**
* Configure a validated property on an object which will ignore updates for
* values which return a non-true value when passed to a validator function.
* @param {object|function} target
* @param {string} prop
* @param {*} [val]
* @param {function} validator
*/
function validated(target, prop, val, validator) {
target = normalTarget(target);
var desc = describe(target, prop) || {};
if (arguments.length < 4) validator = val, val = target[prop];
Object.defineProperty(target, prop, {
configurable: true,
enumerable: true,
get: function() {
return desc.get
? desc.get.call(this)
: val;
},
set: function(newval) {
if (validator.call(this, newval) === true) {
val = newval;
if (desc.set) desc.set.call(this, newval);
}
}
});
}
/**
* Configure a normalized property on an object which passes a value through a
* normalizer function before updating.
* @param {object|function} target
* @param {string} prop
* @param {*} [val]
* @param {function} normalizer
*/
function normalized(target, prop, val, normalizer) {
target = normalTarget(target);
var desc = describe(target, prop) || {};
if (arguments.length < 4) normalizer = val, val = target[prop];
Object.defineProperty(target, prop, {
configurable: true,
enumerable: true,
get: function() {
return desc.get
? desc.get.call(this)
: val;
},
set: function(newval) {
if (desc.set) desc.set.call(this, normalizer.call(this, newval));
else val = normalizer.call(this, newval);
}
});
}
/**
* Configure a derived property on an object which is read-only and whose value
* is calculated by a derive function every time it is accessed.
* @param {object|function} target
* @param {string} prop
* @param {function} derive
*/
function derived(target, prop, derive) {
target = normalTarget(target);
Object.defineProperty(target, prop, {
configurable: true,
enumerable: false,
get: derive,
set: undefined
});
}
/**
* Configure a property on an object which has get/set handlers.
* @param {object|function} target
* @param {string} prop
* @param {function} set
* @param {function} get
*/
function managed(target, prop, set, get) {
target = normalTarget(target);
Object.defineProperty(target, prop, {
configurable: true,
enumerable: false,
set: set,
get: get
});
}
/**
* Configure a property on an object which triggers a callback when set.
* @param {object|function} target
* @param {string} prop
* @param {function} change
*/
function triggered(target, prop, change) {
target = normalTarget(target);
var val = target[prop],
desc = describe(target, prop) || {};
Object.defineProperty(target, prop, {
configurable: true,
enumerable: false,
set: function(newval) {
var oldval = val;
val = newval;
if (desc.set) desc.set.call(this, newval);
change.call(this, newval, oldval);
},
get: function() {
return desc.get
? desc.get.call(this)
: val;
}
});
}
/**
* Return the current descriptor for an object property, which may be an own
* descriptor or a prototype descriptor.
* @param {object|function} target
* @param {string} prop
* @returns {object}
*/
function describe(target, prop) {
var proto = target,
desc;
while (!(desc = Object.getOwnPropertyDescriptor(proto, prop))
&& ((proto = Object.getPrototypeOf(proto)) !== Object.prototype))
;
return desc;
}
/** export propertize functions */
module.exports = {
value: value,
configurable: configurable,
enumerable: enumerable,
writable: writable,
get: get,
set: set,
attribute: attribute,
basic: basic,
field: field,
hidden: hidden,
internal: internal,
locked: locked,
readonly: readonly,
setting: setting,
derived: derived,
managed: managed,
normalized: normalized,
triggered: triggered,
validated: validated,
describe: describe
};