8000 clean up conversions and broaden coverage of infinite iteration checks · ui-frontend/immutable-js@b94941b · GitHub
[go: up one dir, main page]

Skip to content

Commit b94941b

Browse files
committed
clean up conversions and broaden coverage of infinite iteration checks
1 parent 07a7219 commit b94941b

File tree

13 files changed

+27
-103
lines changed

13 files changed

+27
-103
lines changed

__tests__/Range.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('Range', () => {
2626
expect(v.length).toBe(Infinity);
2727
expect(v.first()).toBe(10);
2828
expect(v.last()).toBe(Infinity);
29-
expect(() => v.toArray()).toThrow('Cannot access end of infinite range.');
29+
expect(() => v.toArray()).toThrow('Cannot perform this action with an infinite sequence.');
3030
});
3131

3232
it('backwards range', () => {

dist/Map.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,6 @@ for(var Sequence____Key in Sequence){if(Sequence.hasOwnProperty(Sequence____Key)
122122

123123
// @pragma Iteration
124124

125-
Map.prototype.toMap=function() {"use strict";
126-
return this;
127-
};
128-
129125
Map.prototype.__deepEqual=function(other) {"use strict";
130126
var is = require('./Immutable').is;
131127
// Using Sentinel here ensures that a missing key is not interpretted as an

dist/OrderedMap.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ for(var ImmutableMap____Key in ImmutableMap){if(ImmutableMap.hasOwnProperty(Immu
116116

117117
// @pragma Iteration
118118

119-
OrderedMap.prototype.toOrderedMap=function() {"use strict";
120-
return this;
121-
};
122-
123119
OrderedMap.prototype.__deepEqual=function(other) {"use strict";
124120
var is = require('./Immutable').is;
125121
var iterator = this.$OrderedMap_vector.__iterator__();

dist/Range.js

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -69,36 +69,6 @@ for(var IndexedSequence____Key in IndexedSequence){if(IndexedSequence.hasOwnProp
6969
return this.$Range_start === other.$Range_start && this.$Range_end === other.$Range_end && this.$Range_step === other.$Range_step;
7070
};
7171

72-
Range.prototype.toArray=function() {"use strict";
73-
assertNotInfinite(this.length);
74-
return ____SuperProtoOfIndexedSequence.toArray.call(this);
75-
};
76-
77-
Range.prototype.toObject=function() {"use strict";
78-
assertNotInfinite(this.length);
79-
return ____SuperProtoOfIndexedSequence.toObject.call(this);
80-
};
81-
82-
Range.prototype.toVector=function() {"use strict";
83-
assertNotInfinite(this.length);
84-
return ____SuperProtoOfIndexedSequence.toVector.call(this);
85-
};
86-
87-
Range.prototype.toMap=function() {"use strict";
88-
assertNotInfinite(this.length);
89-
return ____SuperProtoOfIndexedSequence.toMap.call(this);
90-
};
91-
92-
Range.prototype.toOrderedMap=function() {"use strict";
93-
assertNotInfinite(this.length);
94-
return ____SuperProtoOfIndexedSequence.toOrderedMap.call(this);
95-
};
96-
97-
Range.prototype.toSet=function() {"use strict";
98-
assertNotInfinite(this.length);
99-
return ____SuperProtoOfIndexedSequence.toSet.call(this);
100-
};
101-
10272
Range.prototype.indexOf=function(searchValue) {"use strict";
10373
var offsetValue = searchValue - this.$Range_start;
10474
if (offsetValue % this.$Range_step === 0) {
@@ -124,7 +94,6 @@ for(var IndexedSequence____Key in IndexedSequence){if(IndexedSequence.hasOwnProp
12494

12595
Range.prototype.__iterate=function(fn, reverse, flipIndices) {"use strict";
12696
var reversedIndices = reverse ^ flipIndices;
127-
reversedIndices && assertNotInfinite(this.length);
12897
var maxIndex = this.length - 1;
12998
var step = this.$Range_step;
13099
var value = reverse ? this.$Range_start + maxIndex * step : this.$Range_start;
@@ -147,9 +116,5 @@ function invariant(condition, error) {
147116
if (!condition) throw new Error(error);
148117
}
149118

150-
function assertNotInfinite(length) {
151-
invariant(length < Infinity, 'Cannot access end of infinite range.');
152-
}
153-
154119

155120
module.exports = Range;

dist/Sequence.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,40 @@ var Immutable = require('./Immutable');
4141
};
4242

4343
Sequence.prototype.toArray=function() {"use strict";
44+
assertNotInfinite(this.length);
4445
var array = new Array(this.length || 0);
4546
this.values().forEach(function(v, i) { array[i] = v; });
4647
return array;
4748
};
4849

4950
Sequence.prototype.toObject=function() {"use strict";
51+
assertNotInfinite(this.length);
5052
var object = {};
5153
this.forEach(function(v, k) { object[k] = v; });
5254
return object;
5355
};
5456

5557
Sequence.prototype.toVector=function() {"use strict";
5658
// Use Late Binding here to solve the circular dependency.
59+
assertNotInfinite(this.length);
5760
return require('./Vector').from(this);
5861
};
5962

6063
Sequence.prototype.toMap=function() {"use strict";
6164
// Use Late Binding here to solve the circular dependency.
65+
assertNotInfinite(this.length);
6266
return require('./Map').from(this);
6367
};
6468

6569
Sequence.prototype.toOrderedMap=function() {"use strict";
6670
// Use Late Binding here to solve the circular dependency.
71+
assertNotInfinite(this.length);
6772
return require('./OrderedMap').from(this);
6873
};
6974

7075
Sequence.prototype.toSet=function() {"use strict";
7176
// Use Late Binding here to solve the circular dependency.
77+
assertNotInfinite(this.length);
7278
return require('./Set').from(this);
7379
};
7480

@@ -410,6 +416,7 @@ var Immutable = require('./Immutable');
410416

411417
Sequence.prototype.cacheResult=function() {"use strict";
412418
if (!this.$Sequence_cache && this.__iterateUncached) {
419+
assertNotInfinite(this.length);
413420
this.$Sequence_cache = this.entries().toArray();
414421
if (this.length == null) {
415422
this.length = this.$Sequence_cache.length;
@@ -459,6 +466,7 @@ for(var Sequence____Key in Sequence){if(Sequence.hasOwnProperty(Sequence____Key)
459466
};
460467

461468
IndexedSequence.prototype.toArray=function() {"use strict";
469+
assertNotInfinite(this.length);
462470
var array = new Array(this.length || 0);
463471
array.length = this.forEach(function(v, i) { array[i] = v; });
464472
return array;
@@ -866,6 +874,11 @@ function repeatString(string, times) {
866874
return repeated;
867875
}
868876

877+
function assertNotInfinite(length) {
878+
if (length === Infinity) {
879+
throw new Error('Cannot perform this action with an infinite sequence.');
880+
}
881+
}
869882

870883
var __SENTINEL = {};
871884

dist/Set.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,6 @@ for(var Sequence____Key in Sequence){if(Sequence.hasOwnProperty(Sequence____Key)
162162

163163
// @pragma Iteration
164164

165-
Set.prototype.toSet=function() {"use strict";
166-
return this;
167-
};
168-
169165
Set.prototype.__deepEquals=function(other) {"use strict";
170166
return !(this.$Set_map || other.$Set_map) || this.$Set_map.equals(other.$Set_map);
171167
};

dist/Vector.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,6 @@ for(var IndexedSequence____Key in IndexedSequence){if(IndexedSequence.hasOwnProp
346346

347347
// @pragma Iteration
348348

349-
Vector.prototype.toVector=function() {"use strict";
350-
return this;
351-
};
352-
353349
Vector.prototype.slice=function(begin, end, maintainIndices) {"use strict";
354350
var sliceSequence = ____SuperProtoOfIndexedSequence.slice.call(this,begin, end, maintainIndices);
355351
// Optimize the case of vector.slice(b, e).toVector()

src/Map.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,6 @@ class Map extends Sequence {
122122

123123
// @pragma Iteration
124124

125-
toMap() {
126-
return this;
127-
}
128-
129125
__deepEqual(other) {
130126
var is = require('./Immutable').is;
131127
// Using Sentinel here ensures that a missing key is not interpretted as an

src/OrderedMap.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ class OrderedMap extends ImmutableMap {
116116

117117
// @pragma Iteration
118118

119-
toOrderedMap() {
120-
return this;
121-
}
122-
123119
__deepEqual(other) {
124120
var is = require('./Immutable').is;
125121
var iterator = this._vector.__iterator__();

src/Range.js

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -69,36 +69,6 @@ class Range extends IndexedSequence {
6969
return this._start === other._start && this._end === other._end && this._step === other._step;
7070
}
7171

72-
toArray() {
73-
assertNotInfinite(this.length);
74-
return super.toArray();
75-
}
76-
77-
toObject() {
78-
assertNotInfinite(this.length);
79-
return super.toObject();
80-
}
81-
82-
toVector() {
83-
assertNotInfinite(this.length);
84-
return super.toVector();
85-
}
86-
87-
toMap() {
88-
assertNotInfinite(this.length);
89-
return super.toMap();
90-
}
91-
92-
toOrderedMap() {
93-
assertNotInfinite(this.length);
94-
return super.toOrderedMap();
95-
}
96-
97-
toSet() {
98-
assertNotInfinite(this.length);
99-
return super.toSet();
100-
}
101-
10272
indexOf(searchValue) {
10373
var offsetValue = searchValue - this._start;
10474
if (offsetValue % this._step === 0) {
@@ -124,7 +94,6 @@ class Range extends IndexedSequence {
12494

12595
__iterate(fn, reverse, flipIndices) {
12696
var reversedIndices = reverse ^ flipIndices;
127-
reversedIndices && assertNotInfinite(this.length);
12897
var maxIndex = this.length - 1;
12998
var step = this._step;
13099
var value = reverse ? this._start + maxIndex * step : this._start;
@@ -147,9 +116,5 @@ function invariant(condition, error) {
147116
if (!condition) throw new Error(error);
148117
}
149118

150-
function assertNotInfinite(length) {
151-
invariant(length < Infinity, 'Cannot access end of infinite range.');
152-
}
153-
154119

155120
module.exports = Range;

0 commit comments

Comments
 (0)
0