Skip to content

Commit

Permalink
Merge branch 'darrenburns-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
leebyron committed Jul 14, 2015
2 parents dbbd3db + e4a1e34 commit 8031a8b
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 42 deletions.
35 changes: 35 additions & 0 deletions __tests__/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,41 @@ describe('List', () => {
expect(i.set('3', 10).get('-3')).toBe(10);
});

it('uses not set value for string index', () => {
var list: any = Immutable.List();
expect(list.get('stringKey', 'NOT-SET')).toBe('NOT-SET');
});

it('uses not set value for index {}', () => {
var list: any = Immutable.List.of(1, 2, 3, 4, 5);
expect(list.get({}, 'NOT-SET')).toBe('NOT-SET');
});

it('uses not set value for index void 0', () => {
var list: any = Immutable.List.of(1, 2, 3, 4, 5);
expect(list.get(void 0, 'NOT-SET')).toBe('NOT-SET');
});

it('uses not set value for index undefined', () => {
var list: any = Immutable.List.of(1, 2, 3, 4, 5);
expect(list.get(undefined, 'NOT-SET')).toBe('NOT-SET');
});

it('doesnt coerce empty strings to index 0', () => {
var list: any = Immutable.List.of(1, 2, 3);
expect(list.has('')).toBe(false);
});

it('doesnt contain elements at non-empty string keys', () => {
var list: any = Immutable.List.of(1, 2, 3, 4, 5);
expect(list.has('str')).toBe(false);
});

it('hasIn doesnt contain elements at non-empty string keys', () => {
var list: any = Immutable.List.of(1, 2, 3, 4, 5);
expect(list.hasIn(('str'))).toBe(false);
});

it('setting creates a new instance', () => {
var v0 = List.of('a');
var v1 = v0.set(0, 'A');
Expand Down
30 changes: 24 additions & 6 deletions dist/immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,21 @@
}

function wrapIndex(iter, index) {
return index >= 0 ? (+index) : ensureSize(iter) + (+index);
// This implements "is array index" which the ECMAString spec defines as:
// A String property name P is an array index if and only if
// ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal
// to 2^32−1.
// However note that we're currently calling ToNumber() instead of ToUint32()
// which should be improved in the future, as floating point numbers should
// not be accepted as an array index.
if (typeof index !== 'number') {
var numIndex = +index;
if ('' + numIndex !== index) {
return NaN;
}
index = numIndex;
}
return index < 0 ? ensureSize(iter) + index : index;
}

function returnTrue() {
Expand Down Expand Up @@ -2613,12 +2627,12 @@

List.prototype.get = function(index, notSetValue) {
index = wrapIndex(this, index);
if (index < 0 || index >= this.size) {
return notSetValue;
if (index >= 0 && index < this.size) {
index += this._origin;
var node = listNodeFor(this, index);
return node && node.array[index & MASK];
}
index += this._origin;
var node = listNodeFor(this, index);
return node && node.array[index & MASK];
return notSetValue;
};

// @pragma Modification
Expand Down Expand Up @@ -2928,6 +2942,10 @@
function updateList(list, index, value) {
index = wrapIndex(list, index);

if (index !== index) {
return list;
}

if (index >= list.size || index < 0) {
return list.withMutations(function(list ) {
index < 0 ?
Expand Down
Loading

0 comments on commit 8031a8b

Please sign in to comment.