8000 Allow Record factories to be used as a type cast pass through · powercoder23/immutable-js@fd1afd2 · GitHub
[go: up one dir, main page]

Skip to content

Commit fd1afd2

Browse files
committed
Allow Record factories to be used as a type cast pass through
1 parent 347d2ef commit fd1afd2

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

__tests__/Record.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ describe('Record', () => {
1515
var t2 = t1.set('a', 10);
1616
var t3 = t2.clear();
1717

18-
expect(t1 instanceof Record);
19-
expect(t1 instanceof MyType);
18+
expect(t1 instanceof Record).toBe(true);
19+
expect(t1 instanceof MyType).toBe(true);
2020

21-
expect(t3 instanceof Record);
22-
expect(t3 instanceof MyType);
21+
expect(t3 instanceof Record).toBe(true);
22+
expect(t3 instanceof MyType).toBe(true);
2323

2424
expect(t1.get('a')).toBe(1);
2525
expect(t2.get('a')).toBe(10);
@@ -28,6 +28,17 @@ describe('Record', () => {
2828
expect(t2.size).toBe(3);
2929
})
3030

31+
it('passes through records of the same type', () => {
32+
var P2 = Record({ x: 0, y: 0 });
33+
var P3 = Record({ x: 0, y: 0, z: 0 });
34+
var p2 = P2();
35+
var p3 = P3();
36+
expect(P3(p2) instanceof P3).toBe(true);
37+
expect(P2(p3) instanceof P2).toBe(true);
38+
expect(P2(p2)).toBe(p2);
39+
expect(P3(p3)).toBe(p3);
40+
})
41+
3142
it('only allows setting what it knows about', () => {
3243
var MyType = Record({a:1, b:2, c:3});
3344

dist/immutable.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3745,6 +3745,9 @@
37453745
var hasInitialized;
37463746

37473747
var RecordType = function Record(values) {
3748+
if (values instanceof RecordType) {
3749+
return values;
3750+
}
37483751
if (!(this instanceof RecordType)) {
37493752
return new RecordType(values);
37503753
}

dist/immutable.min.js

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Record.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export class Record extends KeyedCollection {
2121
var hasInitialized;
2222

2323
var RecordType = function Record(values) {
24+
if (values instanceof RecordType) {
25+
return values;
26+
}
2427
if (!(this instanceof RecordType)) {
2528
return new RecordType(values);
2629
}

0 commit comments

Comments
 (0)
0