Skip to content

Commit b2b1bf8

Browse files
committed
Fix IE8 issue with Record and throw when setting an invalid key
1 parent 58c27ba commit b2b1bf8

File tree

4 files changed

+57
-46
lines changed

4 files changed

+57
-46
lines changed

__tests__/Record.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,27 @@ describe('Record', () => {
2828
expect(t2.length).toBe(3);
2929
})
3030

31-
it('only persists values it knows about', () => {
31+
it('only allows setting what it knows about', () => {
3232
var MyType = Record({a:1, b:2, c:3});
3333

3434
var t1 = new MyType({a: 10, b:20});
35-
var t2 = t1.set('d', 4);
36-
var t3 = t2.remove('a');
35+
expect(() => {
36+
t1.set('d', 4);
37+
}).toThrow('Cannot set unknown key "d" on Record');
38+
});
39+
40+
it('has a fixed length and falls back to default values', () => {
41+
var MyType = Record({a:1, b:2, c:3});
42+
43+
var t1 = new MyType({a: 10, b:20});
44+
var t3 = t1.remove('a');
3745
var t4 = t3.clear();
3846

3947
expect(t1.length).toBe(3);
40-
expect(t2.length).toBe(3);
4148
expect(t3.length).toBe(3);
4249
expect(t4.length).toBe(3);
4350

4451
expect(t1.get('a')).toBe(10);
45-
expect(t2.get('d')).toBe(undefined);
4652
expect(t3.get('a')).toBe(1);
4753
expect(t4.get('b')).toBe(2);
4854
})

dist/Immutable.js

+16-15
Original file line numberDiff line numberDiff line change
@@ -3121,15 +3121,15 @@ var Record = function Record(defaultValues, name) {
31213121
}
31223122
this._map = Map(values);
31233123
};
3124-
defaultValues = Sequence(defaultValues);
3124+
var keys = Object.keys(defaultValues);
31253125
var RecordTypePrototype = RecordType.prototype = Object.create(RecordPrototype);
31263126
RecordTypePrototype.constructor = RecordType;
3127-
RecordTypePrototype._name = name;
3127+
name && (RecordTypePrototype._name = name);
31283128
RecordTypePrototype._defaultValues = defaultValues;
3129-
var keys = Object.keys(defaultValues);
3130-
RecordType.prototype.length = keys.length;
3131-
if (Object.defineProperty) {
3132-
defaultValues.forEach((function(_, key) {
3129+
RecordTypePrototype._keys = keys;
3130+
RecordTypePrototype.length = keys.length;
3131+
try {
3132+
Sequence(defaultValues).forEach((function(_, key) {
31333133
Object.defineProperty(RecordType.prototype, key, {
31343134
get: function() {
31353135
return this.get(key);
@@ -3140,22 +3140,22 @@ var Record = function Record(defaultValues, name) {
31403140
}
31413141
});
31423142
}));
3143-
}
3143+
} catch (error) {}
31443144
return RecordType;
31453145
};
31463146
var $Record = Record;
31473147
($traceurRuntime.createClass)(Record, {
31483148
toString: function() {
3149-
return this.__toString((this._name || 'Record') + ' {', '}');
3149+
return this.__toString(this._name + ' {', '}');
31503150
},
31513151
has: function(k) {
3152-
return this._defaultValues.has(k);
3152+
return this._defaultValues.hasOwnProperty(k);
31533153
},
31543154
get: function(k, notSetValue) {
31553155
if (notSetValue !== undefined && !this.has(k)) {
31563156
return notSetValue;
31573157
}
3158-
return this._map.get(k, this._defaultValues.get(k));
3158+
return this._map.get(k, this._defaultValues[k]);
31593159
},
31603160
clear: function() {
31613161
if (this.__ownerID) {
@@ -3166,8 +3166,8 @@ var $Record = Record;
31663166
return $Record._empty || ($Record._empty = makeRecord(this, Map.empty()));
31673167
},
31683168
set: function(k, v) {
3169-
if (k == null || !this.has(k)) {
3170-
return this;
3169+
if (!this.has(k)) {
3170+
throw new Error('Cannot set unknown key "' + k + '" on ' + this._name);
31713171
}
31723172
var newMap = this._map.set(k, v);
31733173
if (this.__ownerID || newMap === this._map) {
@@ -3201,9 +3201,9 @@ var $Record = Record;
32013201
return this._map.__iterator(type, reverse);
32023202
},
32033203
__iterate: function(fn, reverse) {
3204-
var record = this;
3205-
return this._defaultValues.map((function(_, k) {
3206-
return record.get(k);
3204+
var $__0 = this;
3205+
return Sequence(this._defaultValues).map((function(_, k) {
3206+
return $__0.get(k);
32073207
})).__iterate(fn, reverse);
32083208
},
32093209
__ensureOwner: function(ownerID) {
@@ -3220,6 +3220,7 @@ var $Record = Record;
32203220
}
32213221
}, {}, Sequence);
32223222
var RecordPrototype = Record.prototype;
3223+
RecordPrototype._name = 'Record';
32233224
RecordPrototype[DELETE] = RecordPrototype.remove;
32243225
RecordPrototype.merge = MapPrototype.merge;
32253226
RecordPrototype.mergeWith = MapPrototype.mergeWith;

0 commit comments

Comments
 (0)