-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathobject+.js
More file actions
241 lines (189 loc) · 7.94 KB
/
object+.js
File metadata and controls
241 lines (189 loc) · 7.94 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
/* Object extensions: backbone-style OO functions and helpers...
* (c) Vlad Balin & Volicon, 2015
* ------------------------------------------------------------- */
(function( spec ){
for( var name in spec ){
Object[ name ] || Object.defineProperty( Object, name, {
enumerable : false,
configurable : true,
writable : true,
value : spec[ name ]
} );
}
})( {
// Object.assign polyfill from MDN.
assign : function( target, firstSource ){
if( target == null ){
throw new TypeError( 'Cannot convert first argument to object' );
}
var to = Object( target );
for( var i = 1; i < arguments.length; i++ ){
var nextSource = arguments[ i ];
if( nextSource == null ){
continue;
}
var keysArray = Object.keys( Object( nextSource ) );
for( var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++ ){
var nextKey = keysArray[ nextIndex ];
var desc = Object.getOwnPropertyDescriptor( nextSource, nextKey );
if( desc !== void 0 && desc.enumerable ){
to[ nextKey ] = nextSource[ nextKey ];
}
}
}
return to;
},
createForEach : function( attrSpecs ){
var statements = [ 'var v;' ];
for( var name in attrSpecs ){
statements.push( '(v=a.' + name + ')' + '===void 0||f(v,"' + name + '");' );
}
return new Function( 'a', 'f', statements.join( '' ) );
},
createCloneCtor : function ( attrSpecs ){
var statements = [];
for( var name in attrSpecs ){
statements.push( "this." + name + "=x." + name + ";" );
}
var CloneCtor = new Function( "x", statements.join( '' ) );
CloneCtor.prototype = Object.prototype;
return CloneCtor;
},
createTransformCtor : function ( attrSpecs ){
var statements = [ 'var v;' ];
for( var name in attrSpecs ){
statements.push( 'this.' + name + '=(v=a.' + name + ')' + '===void 0?void 0 :f(v,"' + name + '");' );
}
var TransformCtor = new Function( "a", 'f', statements.join( '' ) );
TransformCtor.prototype = Object.prototype;
return TransformCtor;
},
// Object.transform function, similar to _.mapObject
transform : function( dest, source, fun, context ){
for( var name in source ){
if( source.hasOwnProperty( name ) ){
var value = fun.call( context, source[ name ], name );
typeof value === 'undefined' || ( dest[ name ] = value );
}
}
return dest;
},
// get property descriptor looking through all prototype chain
getPropertyDescriptor : function( obj, prop ){
for( var desc; !desc && obj; obj = Object.getPrototypeOf( obj ) ){
desc = Object.getOwnPropertyDescriptor( obj, prop );
}
return desc;
},
// extend function in the fashion of Backbone, with extended features required by NestedTypes
// - supports native properties definitions
// - supports forward declarations
// - warn in case if base class method is overriden with value. It's popular mistake when working with Backbone.
extend : (function(){
var error = {
overrideMethodWithValue : function( Ctor, name, value ){
console.warn( '[Type Warning] Base class method overriden with value in Object.extend({ ' + name +
' : ' + value + ' }); Object =', Ctor.prototype );
}
};
function Class(){
this.initialize.apply( this, arguments );
}
// Backbone-style extend with native properties and late definition support
function extend( protoProps, staticProps ){
var Parent = this === Object ? Class : this,
Child;
if( typeof protoProps === 'function' ){
Child = protoProps;
protoProps = null;
}
else if( protoProps && protoProps.hasOwnProperty( 'constructor' ) ){
Child = protoProps.constructor;
}
else{
Child = function Constructor(){ return Parent.apply( this, arguments ); };
}
Object.assign( Child, Parent );
Child.prototype = Object.create( Parent.prototype );
Child.prototype.constructor = Child;
Child.__super__ = Parent.prototype;
protoProps && Child.define( protoProps, staticProps );
return Child;
}
function warnOnError( value, name ){
var prop = Object.getPropertyDescriptor( this.prototype, name );
if( prop ){
var baseIsFunction = typeof prop.value === 'function',
valueIsFunction = typeof value === 'function';
if( baseIsFunction && !valueIsFunction ){
error.overrideMethodWithValue( this, name, prop );
}
}
return value;
}
function preparePropSpec( spec, name ){
var prop = Object.getPropertyDescriptor( this.prototype, name );
if( prop && typeof prop.value === 'function' ){
error.overrideMethodWithValue( this, name, prop );
}
var prepared = spec instanceof Function ? { get : spec } : spec;
if( prepared.enumerable === void 0 ){
prepared.enumerable = true;
}
return prepared;
}
function attachMixins( protoProps ){
var mixins = protoProps.mixins,
merged = {}, properties = {};
for( var i = mixins.length - 1; i >= 0; i-- ){
var mixin = mixins[ i ];
Object.assign( properties, mixin.properties );
Object.assign( merged, mixin );
}
Object.assign( merged, protoProps );
Object.assign( properties, protoProps.properties );
merged.properties = properties;
return merged;
}
function createForEachProp( proto ){
var allProps = {};
// traverse prototype chain
for( var p = proto; p; p = Object.getPrototypeOf( p ) ){
Object.transform( allProps, p.properties, function( spec, name ){
if( !allProps[ name ] && spec.enumerable ){
return spec;
}
} );
}
return Object.createForEach( allProps );
}
function define( a_protoProps, a_staticProps ){
var protoProps = a_protoProps || {};
var staticProps = a_staticProps || {};
if( protoProps.mixins ){
protoProps = attachMixins( protoProps );
}
Object.transform( this.prototype, protoProps, warnOnError, this );
// do not inherit abstract class factory!
if( !staticProps.create ) staticProps.create = null;
Object.assign( this, staticProps ); // No override check here
protoProps && Object.defineProperties( this.prototype,
Object.transform( {}, protoProps.properties, preparePropSpec, this ) );
this.prototype.forEachProp = createForEachProp( this.prototype );
return this;
}
extend.attach = function(){
for( var i = 0; i < arguments.length; i++ ){
var Ctor = arguments[ i ];
Ctor.extend = extend;
Ctor.define = define;
Ctor.prototype.initialize || ( Ctor.prototype.initialize = function(){} );
}
};
extend.attach( Class );
extend.Class = Class;
extend.error = error;
return extend;
})()
} );
module.exports = Object.extend.Class;