Skip to content
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

Fix List notSetValue not working for several key types #545

Merged
merged 2 commits into from
Jul 14, 2015
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
40 changes: 40 additions & 0 deletions __tests__/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,46 @@ 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('disallows adding items at an index of \'\'', () => {
var list: any = Immutable.List.of(1, 2, 3, 4, 5);
expect(() => {list.set('', 'value')}).toThrow();
});

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

function wrapIndex(iter, index) {
return index >= 0 ? (+index) : ensureSize(iter) + (+index);
if (index === '' || isNaN(+index)) {
return NaN;
} else if (index >= 0) {
return +index;
}
return ensureSize(iter) + (+index);
}

function returnTrue() {
Expand Down Expand Up @@ -2613,14 +2618,22 @@

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

List.prototype.has = function(index) {
index = wrapIndex(this, index);
return index >= 0 && (this.size !== undefined ?
this.size === Infinity || index < this.size :
this.indexOf(index) !== -1
);
};

// @pragma Modification

List.prototype.set = function(index, value) {
Expand Down Expand Up @@ -2927,6 +2940,9 @@

function updateList(list, index, value) {
index = wrapIndex(list, index);
if (index !== index) {
throw new Error('Cannot set item at unknown index "' + index + '" on ' + listName(list));
}

if (index >= list.size || index < 0) {
return list.withMutations(function(list ) {
Expand Down Expand Up @@ -3157,6 +3173,10 @@
return size < SIZE ? 0 : (((size - 1) >>> SHIFT) << SHIFT);
}

function listName(list) {
return list._name || list.constructor.name || 'List';
}

createClass(OrderedMap, src_Map__Map);

// @pragma Construction
Expand Down
Loading