-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
325 lines (289 loc) · 8.25 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
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
'use strict';
var toString = Object.prototype.toString;
/**
* The properties that need should be on a valid description object. As defined
* in the specification.
*
* @type {Object}
* @private
*/
var description = {
configurable: 'boolean', // Property may be changed or deleted.
enumerable: 'boolean', // Shows up in enumeration of the properties.
get: 'function', // A function that serves as a getter.
set: 'function', // A function that serves as a setter.
value: undefined, // Value associated with the property.
writable: 'boolean' // Property may be changed using assignment.
};
/**
* Check if a given object is valid as an descriptor.
*
* @param {Object} obj The object with a possible description.
* @returns {Boolean}
* @api public
*/
function descriptor(obj) {
if (!obj || 'object' !== typeof obj || Array.isArray(obj)) return false;
var keys = Object.keys(obj);
//
// A descriptor can only be a data or accessor descriptor, never both.
// An data descriptor can only specify:
//
// - configurable
// - enumerable
// - (optional) value
// - (optional) writable
//
// And an accessor descriptor can only specify;
//
// - configurable
// - enumerable
// - (optional) get
// - (optional) set
//
if (
('value' in obj || 'writable' in obj)
&& ('function' === typeof obj.set || 'function' === typeof obj.get)
) return false;
return !!keys.length && keys.every(function allowed(key) {
var type = description[key]
, valid = type === undefined || is(obj[key], type);
return key in description && valid;
});
}
/**
* Get accurate type information for a given JavaScript thing.
*
* @param {Mixed} thing The thing we want to know.
* @param {String} type The class
* @returns {Boolean}
* @api private
*/
function is(thing, type) {
return toString.call(thing).toLowerCase().slice(8, -1) === type;
}
/**
* Predefine, preconfigure an Object.defineProperty.
*
* @param {Object} obj The context, prototype or object we define on.
* @param {Object} pattern The default description.
* @param {Boolean} override Override the pattern.
* @returns {Function} The function definition.
* @api public
*/
function predefine(obj, pattern) {
pattern = pattern || predefine.READABLE;
return function predefined(method, description, clean) {
//
// If we are given a description compatible Object, use that instead of
// setting it as value. This allows easy creation of getters and setters.
//
if (
!predefine.descriptor(description)
|| is(description, 'object')
&& !clean
&& !predefine.descriptor(predefine.mixin({}, pattern, description))
) { description = {
value: description
};
}
//
// Prevent thrown errors when we attempt to override a readonly
// property
//
var described = Object.getOwnPropertyDescriptor(obj, method);
if (described && !described.configurable) {
return predefined;
}
Object.defineProperty(obj, method, !clean
? predefine.mixin({}, pattern, description)
: description
);
return predefined;
};
}
/**
* Lazy initialization pattern.
*
* @param {Object} obj The object where we need to add lazy loading prop.
* @param {String} prop The name of the property that should lazy load.
* @param {Function} fn The function that returns the lazy laoded value.
* @api public
*/
function lazy(obj, prop, fn) {
Object.defineProperty(obj, prop, {
configurable: true,
get: function get() {
return Object.defineProperty(this, prop, {
value: fn.call(this)
})[prop];
},
set: function set(value) {
return Object.defineProperty(this, prop, {
value: value
})[prop];
}
});
}
/**
* A Object could override the `hasOwnProperty` method so we cannot blindly
* trust the value of `obj.hasOwnProperty` so instead we get `hasOwnProperty`
* directly from the Object.
*
* @type {Function}
* @api private
*/
var has = Object.prototype.hasOwnProperty;
/**
* Remove all enumerable properties from an given object.
*
* @param {Object} obj The object that needs cleaning.
* @param {Array} keep Properties that should be kept.
* @api public
*/
function remove(obj, keep) {
if (!obj) return false;
keep = keep || [];
for (var prop in obj) {
if (has.call(obj, prop) && !~keep.indexOf(prop)) {
delete obj[prop];
}
}
return true;
}
/**
* Create a description that can be used for Object.create(null, definition) or
* Object.defineProperties.
*
* @param {String} property The name of the property we are going to define.
* @param {Object} description The object's description.
* @param {Object} pattern Optional pattern that needs to be merged in.
* @returns {Object} A object compatible with Object.create & defineProperties.
*/
function create(property, description, pattern) {
pattern = pattern || {};
if (!predefine.descriptor(description)) description = {
enumberable: false,
value: description
};
var definition = {};
definition[property] = predefine.mixin(pattern, description);
return definition;
}
/**
* Mix multiple objects in to one single object that contains the properties of
* all given objects. This assumes objects that are not nested deeply and it
* correctly transfers objects that were created using `Object.defineProperty`.
*
* @returns {Object} target
* @api public
*/
function mixin(target) {
Array.prototype.slice.call(arguments, 1).forEach(function forEach(o) {
Object.getOwnPropertyNames(o).forEach(function eachAttr(attr) {
Object.defineProperty(target, attr, Object.getOwnPropertyDescriptor(o, attr));
});
});
return target;
}
/**
* Iterate over a collection. When you return false, it will stop the iteration.
*
* @param {Mixed} collection Either an Array or Object.
* @param {Function} iterator Function to be called for each item.
* @param {Mixed} context The context for the iterator.
* @api public
*/
function each(collection, iterator, context) {
if (arguments.length === 1) {
iterator = collection;
collection = this;
}
var isArray = Array.isArray(collection || this)
, length = collection.length
, i = 0
, value;
if (context) {
if (isArray) {
for (; i < length; i++) {
value = iterator.apply(collection[ i ], context);
if (value === false) break;
}
} else {
for (i in collection) {
value = iterator.apply(collection[ i ], context);
if (value === false) break;
}
}
} else {
if (isArray) {
for (; i < length; i++) {
value = iterator.call(collection[i], i, collection[i]);
if (value === false) break;
}
} else {
for (i in collection) {
value = iterator.call(collection[i], i, collection[i]);
if (value === false) break;
}
}
}
return this;
}
/**
* Merge in objects, deeply nested objects.
*
* @param {Object} target The object that receives the props.
* @param {Object} additional Extra object that needs to be merged in the target.
* @returns {Object} The first argument, target, which is fully merged.
* @api public
*/
function merge(target, additional) {
var result = target
, undefined;
if (Array.isArray(target)) {
each(additional, function arrayForEach(index) {
if (JSON.stringify(target).indexOf(JSON.stringify(additional[index])) === -1) {
result.push(additional[index]);
}
});
} else if ('object' === typeof target) {
each(additional, function objectForEach(key, value) {
if (target[key] === undefined) {
result[key] = value;
} else if (has.call(target, key)) {
result[key] = merge(target[key], additional[key]);
}
});
} else {
result = additional;
}
return result;
}
//
// Attach some convenience functions.
//
predefine.extend = require('extendible');
predefine.descriptor = descriptor;
predefine.create = create;
predefine.remove = remove;
predefine.merge = merge;
predefine.mixin = mixin;
predefine.each = each;
predefine.lazy = lazy;
//
// Predefined description templates.
//
predefine.WRITABLE = {
configurable: true,
enumerable: false,
writable: true
};
predefine.READABLE = {
enumerable: false,
writable: false
};
//
// Expose the module.
//
module.exports = predefine;