-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprimish.js
More file actions
277 lines (237 loc) · 7.54 KB
/
Copy pathprimish.js
File metadata and controls
277 lines (237 loc) · 7.54 KB
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
/**
* @module primish 0.3.6 - prototypal inheritance sugar, MooTools Style
* @description browser-friendly or node-js Class sugar
**/
;(function(factory){
// UMD wrap
if (typeof define === 'function' && define.amd){
define('primish/primish', factory);
} else if (typeof module !== 'undefined' && module.exports){
module.exports = factory();
} else {
this.primish = factory();
}
}).call(this, function(){
var hasOwnProperty = Object.hasOwnProperty,
has = function(obj, key){
// hasOwnProperty shortcut
return hasOwnProperty.call(obj, key);
};
var each = function(object, method, context){
for (var key in object)
if (method.call(context, object[key], key, object) === false)
break;
return object;
};
if (!{ valueOf: 0 }.propertyIsEnumerable('valueOf')){
var proto = Object.prototype,
buggy = 'constructor,toString,valueOf,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,toLocaleString'.split(',');
each = function(object, method, context){
var key,
i,
value;
for (key in object)
if (method.call(context, object[key], key, object) === false)
return object;
for (i = 0; key = buggy[i]; i++){
value = object[key];
if ((value !== proto[key] || has(object, key)) && method.call(context, value, key, object) === false)
break;
}
return object;
};
}
/**
* @description Object.create pollyfil
* @param {object} self - prototype object to use
* @returns {object} new object
*/
var create = Object.create || function(self){
var constructor = function(){};
constructor.prototype = self;
return new constructor();
};
/**
* @description Faster slice w/o ownProperty checks
* @param {Array|Object} args array or arguments object
* @param {Number} pos index to slice from
* @returns {Array}
*/
var slice = function(args, pos){
pos = ~~pos;
var i = -1, l = args.length - pos, x = Array(l);
while (++i < l) x[i] = args[i+pos]; return x;
};
var isObject = (function(){
var toString = Object.prototype.toString,
objString = '[object Object]';
return function(obj){
return toString.call(obj) === objString && obj != null;
};
}());
// polyfill these also
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor,
defineProperty = Object.defineProperty;
try {
defineProperty({}, '~', {});
getOwnPropertyDescriptor({}, '~');
} catch (e) {
// no native support, fix it.
getOwnPropertyDescriptor = function(object, key){
return { value: object[key] };
};
defineProperty = function(object, key, descriptor){
object[key] = descriptor.value;
return object;
};
}
var escapeKeys = /^constructor|extend|define$/;
/**
* @description Adds properties from a proto config object to the new proto
* @param {object} proto
* @returns {function}
*/
var implement = function(proto){
if (has(proto, 'implement')){
// mixins: mutator key.
// expects [array] or single [function constructor]
typeof proto.implement === 'function' && (proto.implement = [proto.implement]);
for (var i = 0, len = proto.implement.length; i < len; ++i)
this.implement(new proto.implement[i]());
delete proto.implement;
}
// copies properties from other classes
each(proto, function(value, key){
if (!key.match(escapeKeys)){
isObject(value) && (proto[key] = clone(value));
this.define(key, getOwnPropertyDescriptor(proto, key) || {
writable: true,
enumerable: true,
configurable: true,
value: value
});
}
}, this);
return this;
};
/**
* @description calls to super class methods, passing arguments
* @param {string} method - to call
* @param {*} args - optional arguments to pass
* @returns {*}
*/
var parent = function(method){
// call a method from immediate parent with scope of current object
// expected method argument
var parent = this._parent || this.constructor.parent,
result;
this._parent = parent.constructor.parent;
if (!method || !has(parent, method)){
throw new Error('You need to pass a valid super method to .parent', '');
}
result = parent[method].apply(this, slice(arguments, 1));
this._parent = parent;
return result;
};
/**
* @description dereference and shallow clone an object
* @param {object} obj - object to clone
* @returns {obj} cloned object
*/
var clone = function(obj){
var copy = create(obj),
key;
for (key in obj){
if (hasOwnProperty.call(obj, key)) copy[key] = obj[key];
}
return copy;
};
/**
* @description deep merge from right to left, returning left, cloning objects
* @param {object} a
* @param {object} b
* @returns {object} a
*/
var merge = function merge(a, b){
// extending objects (merge)
var k,
callback = function(key){
// primitives from b are just copied, if b is object, it is de-referenced and merged
a[key] = (isObject(b[key])) ? (!isObject(a[key])) ? clone(b[key]) : merge(a[key], clone(b[key])) : b[key];
};
// Don't touch 'null' or 'undefined' objects.
if (a == null || b == null)
return a;
for (k in b) if (hasOwnProperty.call(b, k)) callback(k);
return a;
};
/**
* @constructs primish
* @param {string=} id - id of class for reflection, optional
* @param {object} proto - config object to get props from
* @returns {function} constructor
*/
var primish = function(id, proto){
if (typeof id === 'object'){
proto = id;
id = undefined;
}
var superclass = proto.extend;
// if our nice proto object has no own constructor property
// then we proceed using a ghosting constructor that all it does is
// call the parent's constructor if it has a superclass, else an empty constructor
// proto.constructor becomes the effective constructor
var constructor = has(proto, 'constructor') ? proto.constructor : superclass ? function(){
return superclass.apply(this, arguments);
} : function(){
};
delete proto.constructor;
isObject(proto.options) && (proto.options = clone(proto.options));
if (superclass){
var superproto = superclass.prototype;
// inherit from superclass
var cproto = constructor.prototype = create(superproto);
// setting constructor.parent to superprime.prototype
// because it's the shortest possible absolute reference
constructor.parent = superproto;
cproto.constructor = constructor;
// merge objects
isObject(proto.options) && isObject(superproto.options) && (proto.options = merge(clone(superproto.options), proto.options));
delete proto.extend;
}
// ability to call super via .parent
proto.parent = parent;
// inherit (kindof inherit) define
constructor.define = proto.define || superclass && superclass.define || function(key, descriptor){
defineProperty(this.prototype, key, descriptor);
return this;
};
// copy mixin (this should never change)
constructor.implement = implement;
id && constructor.define('_id', {
value: id,
enumerable: false
});
// finally implement proto and return constructor
return constructor.implement(proto);
};
// helper exports
primish.has = has;
primish.each = each;
primish.merge = merge;
primish.clone = clone;
primish.slice = slice;
// prime.create is Object.create polyfill
primish.create = create;
primish.define = defineProperty;
/**
* @description Adds a property to object and configures it as non-enumerable, returning it back
* @param {object} obj - object to configure
* @param {string} prop - property to configure
* @param {*} value - initial value
*/
primish.hide = function(obj, prop, value){
return obj[prop] = obj[prop] || value, defineProperty(obj, prop, {enumerable: false, value: obj[prop]}), obj[prop];
};
return primish;
});