Skip to content

Migrate Record to a factory method #2078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions __tests__/Record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ describe('Record', () => {
const t1 = MyType();
const t2 = t1.set('a', 10);

expect(t1 instanceof Record).toBe(true);
expect(Record.isRecord(t1)).toBe(true);
expect(t1 instanceof MyType).toBe(true);

expect(t2 instanceof Record).toBe(true);
expect(Record.isRecord(t2)).toBe(true);
expect(t2 instanceof MyType).toBe(true);

expect(t1.get('a')).toBe(1);
Expand Down
4 changes: 2 additions & 2 deletions __tests__/RecordJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ describe('Record', () => {
const t = new Alphabet();
const t2 = t.set('b', 200);

expect(t instanceof Record);
expect(t instanceof Alphabet);
expect(Record.isRecord(t)).toBe(true);
expect(t instanceof Alphabet).toBe(true);
expect(t.soup()).toBe(6);
expect(t2.soup()).toBe(204);

Expand Down
2 changes: 1 addition & 1 deletion __tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Utils', () => {
['Number primitive ', 5],
['String primitive ', 'P'],
['Number Object', Number(6)],
['Immutable.List', new List()],
['Immutable.List', List()],
['simple array', ['one']],
['Error', Error],
['Internal namespaces', Math],
Expand Down
117 changes: 58 additions & 59 deletions src/Record.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,73 +43,72 @@ function throwOnInvalidDefaultValues(defaultValues) {
}
}

export class Record {
constructor(defaultValues, name) {
let hasInitialized;
export const Record = (defaultValues, name) => {
let hasInitialized;

throwOnInvalidDefaultValues(defaultValues);
throwOnInvalidDefaultValues(defaultValues);

const RecordType = function Record(values) {
if (values instanceof RecordType) {
return values;
}
if (!(this instanceof RecordType)) {
return new RecordType(values);
}
if (!hasInitialized) {
hasInitialized = true;
const keys = Object.keys(defaultValues);
const indices = (RecordTypePrototype._indices = {});
// Deprecated: left to attempt not to break any external code which
// relies on a ._name property existing on record instances.
// Use Record.getDescriptiveName() instead
RecordTypePrototype._name = name;
RecordTypePrototype._keys = keys;
RecordTypePrototype._defaultValues = defaultValues;
for (let i = 0; i < keys.length; i++) {
const propName = keys[i];
indices[propName] = i;
if (RecordTypePrototype[propName]) {
/* eslint-disable no-console */
// eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
typeof console === 'object' &&
console.warn &&
console.warn(
'Cannot define ' +
recordName(this) +
' with property "' +
propName +
'" since that property name is part of the Record API.'
);
/* eslint-enable no-console */
} else {
setProp(RecordTypePrototype, propName);
}
const RecordType = function Record(values) {
if (values instanceof RecordType) {
return values;
}
if (!(this instanceof RecordType)) {
return new RecordType(values);
}
if (!hasInitialized) {
hasInitialized = true;
const keys = Object.keys(defaultValues);
const indices = (RecordTypePrototype._indices = {});
// Deprecated: left to attempt not to break any external code which
// relies on a ._name property existing on record instances.
// Use Record.getDescriptiveName() instead
RecordTypePrototype._name = name;
RecordTypePrototype._keys = keys;
RecordTypePrototype._defaultValues = defaultValues;
for (let i = 0; i < keys.length; i++) {
const propName = keys[i];
indices[propName] = i;
if (RecordTypePrototype[propName]) {
/* eslint-disable no-console */
// eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
typeof console === 'object' &&
console.warn &&
console.warn(
'Cannot define ' +
recordName(this) +
' with property "' +
propName +
'" since that property name is part of the Record API.'
);
/* eslint-enable no-console */
} else {
setProp(RecordTypePrototype, propName);
}
}
this.__ownerID = undefined;
this._values = List().withMutations((l) => {
l.setSize(this._keys.length);
KeyedCollection(values).forEach((v, k) => {
l.set(this._indices[k], v === this._defaultValues[k] ? undefined : v);
});
}
this.__ownerID = undefined;
this._values = List().withMutations((l) => {
l.setSize(this._keys.length);
KeyedCollection(values).forEach((v, k) => {
l.set(this._indices[k], v === this._defaultValues[k] ? undefined : v);
});
return this;
};

const RecordTypePrototype = (RecordType.prototype =
Object.create(RecordPrototype));
RecordTypePrototype.constructor = RecordType;
RecordTypePrototype.create = RecordType;
});
return this;
};

if (name) {
RecordType.displayName = name;
}
const RecordTypePrototype = (RecordType.prototype =
Object.create(RecordPrototype));
RecordTypePrototype.constructor = RecordType;
RecordTypePrototype.create = RecordType;

// eslint-disable-next-line no-constructor-return
return RecordType;
if (name) {
RecordType.displayName = name;
}

return RecordType;
};

export class RecordImpl {
toString() {
let str = recordName(this) + ' { ';
const keys = this._keys;
Expand Down Expand Up @@ -212,7 +211,7 @@ export class Record {

Record.isRecord = isRecord;
Record.getDescriptiveName = recordName;
const RecordPrototype = Record.prototype;
const RecordPrototype = RecordImpl.prototype;
RecordPrototype[IS_RECORD_SYMBOL] = true;
RecordPrototype[DELETE] = RecordPrototype.remove;
RecordPrototype.deleteIn = RecordPrototype.removeIn = deleteIn;
Expand Down
2 changes: 1 addition & 1 deletion src/Repeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class RepeatImpl extends IndexedSeqImpl {
const size = this.size;
return wholeSlice(begin, end, size)
? this
: new Repeat(
: new RepeatImpl(
this._value,
resolveEnd(end, size) - resolveBegin(begin, size)
);
Expand Down