-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModel.js
1089 lines (914 loc) · 27.7 KB
/
Model.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint camelcase: ["off"] */
const inherit = require('./util/inherit');
let ArrayType;
let primitives;
let Model;
function _emptyObject () {
return Object.create(null);
}
const NOT_INSTANCE = {};
const EMPTY_PROPERTIES = _emptyObject();
function _forProperty (property, options, work) {
options = _toOptions(options);
const origProperty = options.property;
options.property = property;
work(options);
options.property = origProperty;
return options;
}
function _notifySet (model, oldValue, newValue, property) {
const Type = model.constructor;
if (Type._onSet) {
const event = {
model: model,
propertyName: property.getName(),
oldValue: oldValue,
newValue: newValue,
property: property
};
const len = Type._onSet.length;
for (let i = 0; i < len; i++) {
Type._onSet[i](model, event);
}
}
}
function _set (model, data, property, value, options) {
const Type = property.getType();
// this the value that we return (it may be the result of wrapping/coercing the original value)
const wrapped = Type.isWrapped();
if (Model.isModel(value) && Type.isInstance(value)) {
// value is expected type
// store raw data in this model's data
} else if (!wrapped && Type.coerce) {
// Only call coerce if this type is not wrapped
// (otherwise we'd call coerce twice which is wasteful)
// The coerce function needs some context in the options
options = _forProperty(property, options, function (options) {
value = Type.coerce(value, options);
});
}
const propertyKey = property.getKey();
const oldValue = data[propertyKey];
const setter = property.getSetter();
if (setter) {
// setter will do all of the work
setter.call(model, value, property);
// get the new value
value = data[propertyKey];
}
if (wrapped) {
options = _forProperty(property, options, function (options) {
// recursively call setters
// The return value is the wrapped value
value = Type.wrap(value, options);
});
}
if (oldValue !== value) {
data[propertyKey] = value;
_notifySet(model, oldValue, value, property);
}
return value;
}
function _generateGetter (property) {
const propertyKey = property.getKey();
const getter = property.getGetter();
if (getter) {
return function (options) {
return getter.call(this, property);
};
} else {
return function (options) {
return this.data[propertyKey];
};
}
}
function _generateSetter (property) {
return function (value, options) {
return _set(this, this.data, property, value, options);
};
}
// This function will be used to make sure an array value
// exists for the given property
function _ensureArray (model, property) {
const propertyKey = property.getKey();
let array = model.data[propertyKey];
if (!array) {
model.data[propertyKey] = array = [];
ArrayType.flagAsArrayType(array);
}
return array;
}
function _generateAddValueTo (property) {
const items = property.items;
const ItemType = items && items.type;
let coerce;
if (ItemType) {
coerce = ItemType.coerce;
}
if (ItemType && ItemType.isWrapped()) {
// the addTo<Property> function will need to wrap each item
// when adding it to the array
return function (value, options) {
const array = _ensureArray(this, property);
array.push(ItemType.wrap(value, options));
};
} else {
// the addTo<Property> function should add raw value to array
// (call the type coercion function if it exists)
return function (value, options) {
const array = _ensureArray(this, property);
array.push((coerce) ? coerce.call(ItemType, value, options) : value);
};
}
}
function _initialUpperCase (str) {
return str.charAt(0).toUpperCase() + str.substring(1);
}
function _convertArray (array, options) {
return ArrayType.convertArrayItems(array, this, options);
}
// This is the constructor that gets called when ever a Model (or derived type)
// is created
module.exports = Model = function Model (rawData, options) {
const Type = this.constructor;
if (Type.constructable === false) {
throw new Error('Instances of this type cannot be created. data: ' + rawData);
}
};
Model.typeName = 'Model';
// This is a internal helper function that will do some work in the context
// of a property. The options.property value will be temporarily updated to
// reflect the given property and then options.property will be restored
// to its original value after the work is executed.
Model._forProperty = _forProperty;
Model.isModel = function (obj) {
return obj && obj.Model;
};
Model.cleanArray = function (array, options) {
let i = array.length;
const newArray = new Array(i);
while (--i >= 0) {
newArray[i] = Model.clean(array[i], options);
}
return newArray;
};
Model.clean = function (obj, options) {
options = _toOptions(options);
if (obj == null) {
return obj;
} else if (Array.isArray(obj)) {
return Model.cleanArray(obj, options);
} else if (obj.Model) {
// obj is an instance of a Model
return obj.clean(options);
} else {
// obj is not associated with a model instance...
// If the obj we were given is a complex object then return a
// deep clone...
// NOTE: Don't clone Date object to clean it since we assume it
// is already clean.
if ((obj.constructor !== Date) && (typeof obj === 'object')) {
const clean = {};
const keys = Object.keys(obj);
const len = keys.length;
for (let i = 0; i < len; i++) {
const key = keys[i];
const value = Model.clean(obj[key], options);
if (value !== undefined) {
clean[key] = value;
}
}
return clean;
} else {
return obj;
}
}
};
Model.unwrap = function (obj) {
if (obj == null) {
return obj;
}
if (obj.Model) {
return obj.data || obj;
} else {
return obj;
}
};
Model.hasProperties = function () {
return (this.properties !== EMPTY_PROPERTIES) || (this.additionalProperties === true);
};
Model.hasProperty = function (propertyName) {
return !!this.properties[propertyName];
};
Model.getProperties = function () {
return this.properties;
};
Model.getProperty = function (propertyName) {
return this.properties[propertyName];
};
Model.forEachProperty = function (options, callback) {
if (!this.Properties) {
return;
}
if (arguments.length === 1) {
callback = arguments[0];
options = _emptyObject();
}
let proto = this.Properties.prototype;
const inherited = (options.inherited !== false);
const seen = _emptyObject();
do {
for (let key in proto) {
// Make sure we haven't already handled the given property
if (!seen[key] && proto.hasOwnProperty(key)) {
const property = proto[key];
if (property.constructor === Property) {
if (key === property.getName()) {
callback(property);
}
}
seen[key] = true;
}
}
if (!inherited) {
break;
}
// Move up the prototype chain if we care about inherited properties
} while (((proto = Object.getPrototypeOf(proto)) != null));
};
Model.preventConstruction = function () {
this.constructable = false;
};
Model.isCompatibleWith = function (other) {
let cur = this;
do {
if (cur === other) {
return true;
}
} while ((cur = (cur.$super)));
return false;
};
Model.isInstance = function (value) {
return (value instanceof this);
};
Model.isPrimitive = function () {
return false;
};
Model.coercionError = function (value, options, errorMessage) {
let message = '';
if (options && options.property && options.property.getName) {
message += options.property.getName() + ': ';
}
message += 'Invalid value: ' + value;
if (errorMessage) {
message += ' - ' + errorMessage;
}
if (options && options.errors) {
options.errors.push(message);
} else {
const err = new Error(message);
err.source = Model;
throw err;
}
};
Model.stringify = function (obj, pretty) {
return JSON.stringify(Model.clean(obj), null, pretty ? ' ' : undefined);
};
const Model_proto = Model.prototype;
Model_proto.unwrap = function () {
return this.data || this;
};
/**
* Creates a deep clone of the data stored in this object with all temporary
* and non-persisted values removed.
*/
Model_proto.clean = function (options) {
options = _toOptions(options);
const Type = this.Model;
if (Type.clean) {
// call "clean" function provided by type
return Type.clean(this, options);
}
// The "properties" object is a map that we can use to lookup
// all property definitions
const properties = Type.properties;
let data = this.data;
if (Type.hasProperties()) {
const clone = {};
const keys = Object.keys(data);
const len = keys.length;
for (let i = 0; i < len; i++) {
const key = keys[i];
const property = options.property = properties[key];
let value = data[key];
if (property && (property.isPersisted())) {
// no need to clean null/undefined values
if (value == null) {
if (value !== undefined) {
clone[key] = value;
}
} else {
const propertyType = property.type;
const propertyClean = property.clean;
const clean = propertyType.clean;
const oldProperty = options.property;
options.property = property;
if (propertyClean) {
// call the clean method on the property
value = propertyClean(value, options);
} else if (clean) {
// call the clean function provided by model
value = propertyType.clean(value, options);
} else if (value.Model || propertyType.isWrapped()) {
// value is a Model instance or it is something
// that could be wrapped.
// Use the default clean function...
value = Model.clean(value, options);
}
// restore the old property
options.property = oldProperty;
if (value !== undefined) {
// put the cleaned value into the clone
clone[key] = value;
}
}
} else if (Type.additionalProperties) {
if (value.Model) {
value = value.clean(options);
}
// simply copy the additional property
if (value !== undefined) {
clone[key] = value;
}
} else if (options.errors) {
options.errors.push('Unrecognized property: ' + key);
}
}
data = clone;
}
if (Type.afterClean) {
const result = Type.afterClean(data, options);
if (result !== undefined) {
data = result;
}
}
// Model does not have properties so simply return the raw data
// (there should be no wrapper)
return data;
};
function _getProperty (model, propertyName, errors) {
const Type = model.constructor;
const properties = Type.properties;
const property = properties[propertyName];
if (!property) {
if (!Type.additionalProperties) {
const err = new Error('Invalid property: ' + propertyName);
if (errors) {
errors.push(err);
} else {
throw err;
}
}
}
return property;
}
/**
* Set value of property with given propertyName to given value.
* @param {String} propertyName the property name
* @param {Object} value the new value
* @param {Object|Array} options an optional object that specifies options
* or an array which will have any errors added to it
*/
Model_proto.set = function (propertyName, value, options) {
const modelData = this.data;
const property = _getProperty(this, propertyName, options);
if (property) {
_set(this, modelData, property, value, options);
} else {
const oldValue = modelData[propertyName];
if (oldValue !== value) {
modelData[propertyName] = value;
_notifySet(this, propertyName, oldValue, value);
}
}
};
/**
* Get value of property with given propertyName.
* @param {String} propertyName the property name
* @param {Object|Array} options an optional object that specifies options
* or an array which will have any errors added to it
*/
Model_proto.get = function (propertyName, options) {
const Type = this.constructor;
const property = Type.properties[propertyName];
if (property) {
const getter = property.getGetter();
if (getter) {
return getter.call(this, propertyName, property);
}
}
return this.data[propertyName];
};
Model_proto.stringify = function (pretty) {
return Model.stringify(this.data, pretty);
};
function Property (config) {
for (let key in config) {
if (config.hasOwnProperty(key)) {
this[key] = config[key];
}
}
}
const Property_proto = Property.prototype;
Property_proto.getName = function () {
return this.name;
};
Property_proto.getKey = Property_proto.toString = function () {
return this.key;
};
Property_proto.getType = function () {
return this.type;
};
Property_proto.getItems = function () {
return this.items;
};
Property_proto.getGetter = function () {
return this.get;
};
Property_proto.getSetter = function () {
return this.set;
};
Property_proto.isPersisted = function () {
return (this.persist !== false);
};
function Items (owner) {
this.owner = owner;
}
const Items_proto = Items.prototype;
Items_proto.getName = function () {
return this.owner.getName();
};
function _parseType (type) {
if (type.Model) {
// type is derived from Model
return type;
}
switch (type) {
case Date:
return primitives.date;
case Number:
return primitives.number;
case Boolean:
return primitives.boolean;
case String:
return primitives.string;
case Object:
return primitives.object;
case Function:
return primitives.function;
case Array:
return ArrayType;
}
if ((typeof type === 'object') && (Object.keys(type).length === 0)) {
return primitives.any;
}
return null;
}
function _parseTypeStr (typeStr, propertyConfig, resolver, Type) {
const len = typeStr.length;
if ((typeStr.charAt(len - 2) === '[') && (typeStr.charAt(len - 1) === ']')) {
// array type
propertyConfig.type = ArrayType;
propertyConfig.items = _emptyObject();
_parseTypeStr(typeStr.substring(0, len - 2), propertyConfig.items, resolver, Type);
} else {
propertyConfig.type = _resolve(typeStr, resolver, Type);
}
}
function _resolve (typeName, resolver, Type) {
let type = primitives[typeName];
if (type) {
return type;
}
if (typeName === 'self') {
return Type;
}
if (resolver) {
if ((type = resolver(typeName))) {
return type;
}
}
throw new Error('Invalid type: ' + typeName);
}
function _parseTypeConfig (propertyName, propertyConfig, resolver, Type) {
if (Array.isArray(propertyConfig)) {
propertyConfig = {
type: propertyConfig
};
} else if ((typeof propertyConfig) !== 'object') {
propertyConfig = {
type: propertyConfig
};
}
const type = propertyConfig.type;
if (type) {
if (Array.isArray(type)) {
// handle short-hand notation for Array types
propertyConfig.type = ArrayType;
if (type.length) {
const items = type[0];
if (items != null) {
propertyConfig.items = _parseTypeConfig(propertyName, items, resolver, Type);
}
}
} else if (type.constructor === String) {
_parseTypeStr(type, propertyConfig, resolver, Type);
} else {
// handle normal notation for types
propertyConfig.type = _parseType(type);
if (!propertyConfig.type) {
throw new Error('Unrecognized type ' + JSON.stringify(type) + ' for property "' + propertyName + '". Expected type derived from Model or primitive type.');
}
// Convert the subtype to special type if necessary
if (propertyConfig.items) {
propertyConfig.items = _parseTypeConfig(propertyName, propertyConfig.items, resolver, Type);
}
}
} else {
propertyConfig.type = primitives.any;
}
return propertyConfig;
}
function _toProperty (name, propertyConfig, resolver, Type) {
propertyConfig = _parseTypeConfig(name, propertyConfig, resolver, Type);
propertyConfig.name = name;
propertyConfig.key = propertyConfig.key || name;
return new Property(propertyConfig);
}
const SPECIAL_PROPERTIES = {
init: 1,
wrap: 1,
coerce: 1,
properties: 1,
prototype: 1,
mixins: 1
};
function _copyNonSpecialPropertiesToType (config, Type) {
for (let key in config) {
if (config.hasOwnProperty(key) && !SPECIAL_PROPERTIES[key]) {
Type[key] = config[key];
}
}
}
function _toOptions (options) {
if (options == null) {
return _emptyObject();
}
if (Array.isArray(options)) {
return {
errors: options
};
}
return options;
}
function _addToArray (obj, propertyName, value) {
if (!value) {
return;
}
const arr = obj[propertyName] || (obj[propertyName] = []);
arr.push(value);
}
function _concatToArray (obj, propertyName, otherArr) {
if (!otherArr) {
return;
}
const arr = obj[propertyName] || (obj[propertyName] = []);
for (let i = 0; i < otherArr.length; i++) {
arr.push(otherArr[i]);
}
}
function _installMixin (mixin, Type, Base, existingProperties) {
if (mixin.initType) {
mixin.initType(Type);
}
let key;
if (mixin.id) {
key = '_mixin_' + mixin.id;
if ((Base.properties && Base.properties[key]) || Type.Properties.prototype[key]) {
// this mixin is already installed
return;
}
Type.Properties.prototype[key] = true;
}
const mixinPrototype = mixin.prototype;
if (mixinPrototype) {
for (key in mixinPrototype) {
if (mixinPrototype.hasOwnProperty(key)) {
Type.prototype[key] = mixinPrototype[key];
}
}
}
let mixinProperties;
if ((mixinProperties = mixin.properties)) {
for (key in mixinProperties) {
if (mixinProperties.hasOwnProperty(key) && (existingProperties[key] === undefined)) {
existingProperties[key] = mixinProperties[key];
}
}
}
_addToArray(Type, '_init', mixin.init);
_addToArray(Type, '_onSet', mixin.onSet);
let mixins;
if ((mixins = mixin.mixins)) {
for (const mixin of mixins) {
_installMixin(mixin, Type, Base, existingProperties);
}
}
}
function _checkInstance (obj, wrap, Type) {
return wrap && Type.isInstance(obj) ? obj : NOT_INSTANCE;
}
function _extend (Base, config, resolver) {
config = config || _emptyObject();
const init = config.init;
let wrap = config.wrap;
const coerce = config.coerce;
let properties = config.properties;
const prototype = config.prototype;
const mixins = config.mixins;
let Data;
function Type (rawData, options) {
if (Data) {
if (this.data === undefined) {
this.data = new Data(this, rawData, options);
}
} else {
this.data = rawData;
}
// Call the super constructor
Type.$super.call(this, rawData, options);
// Call initialization functions provided by mixins (if any)
const initArr = Type._init;
if (initArr) {
for (let i = 0; i < initArr.length; i++) {
initArr[i].call(this, rawData, options);
}
}
// Call the user-provided "constructor" function
if (init) {
init.call(this, rawData, options);
}
}
// Selectively copy properties from Model to Type
for (const property of [
'getProperty',
'getProperties',
'hasProperty',
'hasProperties',
'preventConstruction',
'unwrap',
'coercionError',
'forEachProperty',
'isCompatibleWith',
'isInstance',
'isPrimitive'
]) {
Type[property] = Model[property];
}
Type.convertArray = _convertArray;
// Now copy any properties from config to Type that might
// override any of the special prpoerties that were copied above
_copyNonSpecialPropertiesToType(config, Type);
if (Base.additionalProperties) {
// the additionalProperties flag should trickle down if true
Type.additionalProperties = true;
}
_concatToArray(Type, '_onSet', Base._onSet);
// Store reference to Model
Object.defineProperty(Type, 'Model', {
enumerable: false,
configurable: false,
writable: false,
value: Model
});
if (coerce) {
// Create a proxy coerce function that guarantees that options
// argument will be provided.
Type.coerce = function (value, options) {
return coerce.call(Type, value, _toOptions(options));
};
}
// provide method to extend this model
Type.extend = function (config) {
return _extend(Type, config);
};
Type.isWrapped = function () {
return (wrap !== false);
};
let factory;
if (wrap && wrap.constructor === Function) {
factory = wrap;
} else {
wrap = (wrap !== false);
factory = function (data, options) {
if (wrap && (arguments.length === 0)) {
return new Type();
}
let instance;
// see if the data is already an instance
if ((data != null) && (instance = _checkInstance(data, wrap, Type)) !== NOT_INSTANCE) {
// we already have instance of correct type so return it
return instance;
}
if (coerce) {
data = coerce.call(Type, data, (options = _toOptions(options)));
// If the coerce function returns null/undefined then not
// much more we can do so simply return that value
if (data == null) {
return data;
}
// Do we have the correct type after coercion?
if ((instance = _checkInstance(data, wrap, Type)) !== NOT_INSTANCE) {
// coercion return instance of correct type so return it
return instance;
}
} else if (data == null) {
return data;
}
data = Model.unwrap(data);
if (!wrap) {
// if we're not wrapping or data is null then simply return the raw value
return data;
}
if (Array.isArray(data)) {
return Type.convertArray(data, options);
}
// return new model instance
return new Type(data, options);
};
}
Type.wrap = factory;
if (!Type.create) {
Type.create = factory;
}
inherit(Type, Base);
const classPrototype = Type.prototype;
classPrototype.Model = Type;
let propertyNames;
if ((properties && (propertyNames = Object.keys(properties)).length > 0) || mixins) {
// Use prototype chaining to create property map
Type.Properties = function () {
// nothing to do here
};
if (Base.Properties) {
inherit(Type.Properties, Base.Properties);
} else {
Type.Properties.prototype = {};
}
if (mixins) {
const mixinProperties = _emptyObject();
for (const mixin of mixins) {
_installMixin(mixin, Type, Base, mixinProperties);
}
const mixinPropertyNames = Object.keys(mixinProperties);
if (mixinPropertyNames.length) {
if (properties) {
// combine mixin properties with properties provided for Type
// but give precedence to the Type properties.
for (let i = 0; i < mixinPropertyNames.length; i++) {
const propertyName = mixinPropertyNames[i];
if (!properties.hasOwnProperty(propertyName)) {
properties[propertyName] = mixinProperties[propertyName];
propertyNames.push(propertyName);
}
}
} else {
// use properties from mixin
properties = mixinProperties;
propertyNames = mixinPropertyNames;
}
}
}
if (properties) {
const propertiesPrototype = Type.Properties.prototype;
for (const propertyName of propertyNames) {
const property = _toProperty(propertyName, properties[propertyName], resolver, Type);
const propertyKey = property.getKey();
// Put the properties in the prototype by name and property
propertiesPrototype[propertyName] = property;
if (propertyName !== propertyKey) {
propertiesPrototype[propertyKey] = property;
}
let funcName;
const funcSuffix = _initialUpperCase(propertyName);
// generate getter
if (property.getGetter() !== null) {
funcName = 'get' + funcSuffix;
classPrototype[funcName] = _generateGetter(property);
}
// generate setter
if (property.getSetter() !== null) {
funcName = 'set' + funcSuffix;
classPrototype[funcName] = _generateSetter(property);
}
// generate addTo<Property> if property is Array type
if (property.getType() === ArrayType) {
funcName = 'addTo' + funcSuffix;
classPrototype[funcName] = _generateAddValueTo(property);
}
}
}
Type.properties = new Type.Properties();
} else {
Type.Properties = Base.Properties;
Type.properties = Base.properties || EMPTY_PROPERTIES;
}
if (Type.hasProperties()) {
const _allProperties = [];
Type.forEachProperty(function (property) {
_allProperties.push(property.getKey());
});
// We define a constructor function for the "data" that this
// Model stores which will allow the JavaScript Engine to create
// "hidden classes" for the purpose of optimization.
Type.Data = Data = function (model, rawData, options) {
const properties = Type.properties;
let allProperties;
let len;
let key;