MCPcopy Create free account
hub / github.com/immutable-js/immutable-js / constructor

Method constructor

src/Record.js:47–110  ·  view source on GitHub ↗
(defaultValues, name)

Source from the content-addressed store, hash-verified

45
46export class Record {
47 constructor(defaultValues, name) {
48 let hasInitialized;
49
50 throwOnInvalidDefaultValues(defaultValues);
51
52 const RecordType = function Record(values) {
53 if (values instanceof RecordType) {
54 return values;
55 }
56 if (!(this instanceof RecordType)) {
57 return new RecordType(values);
58 }
59 if (!hasInitialized) {
60 hasInitialized = true;
61 const keys = Object.keys(defaultValues);
62 const indices = (RecordTypePrototype._indices = {});
63 // Deprecated: left to attempt not to break any external code which
64 // relies on a ._name property existing on record instances.
65 // Use Record.getDescriptiveName() instead
66 RecordTypePrototype._name = name;
67 RecordTypePrototype._keys = keys;
68 RecordTypePrototype._defaultValues = defaultValues;
69 for (let i = 0; i < keys.length; i++) {
70 const propName = keys[i];
71 indices[propName] = i;
72 if (RecordTypePrototype[propName]) {
73 /* eslint-disable no-console */
74 // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
75 typeof console === 'object' &&
76 console.warn &&
77 console.warn(
78 'Cannot define ' +
79 recordName(this) +
80 ' with property "' +
81 propName +
82 '" since that property name is part of the Record API.'
83 );
84 /* eslint-enable no-console */
85 } else {
86 setProp(RecordTypePrototype, propName);
87 }
88 }
89 }
90 this.__ownerID = undefined;
91 this._values = List().withMutations((l) => {
92 l.setSize(this._keys.length);
93 KeyedCollection(values).forEach((v, k) => {
94 l.set(this._indices[k], v === this._defaultValues[k] ? undefined : v);
95 });
96 });
97 return this;
98 };
99
100 const RecordTypePrototype = (RecordType.prototype =
101 Object.create(RecordPrototype));
102 RecordTypePrototype.constructor = RecordType;
103
104 if (name) {

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected