Skip to content

Do not throw from hasIn #1319

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

Merged
merged 1 commit into from
Sep 30, 2017
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
8 changes: 8 additions & 0 deletions __tests__/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ describe('List', () => {
expect(list.hasIn(['str'])).toBe(false);
});

it('hasIn doesnt throw for bad key-path', () => {
let list = List.of(1, 2, 3, 4, 5);
expect(list.hasIn([1, 2, 3])).toBe(false);

let list2 = List([{}]);
expect(list2.hasIn([0, 'bad'])).toBe(false);
});

it('setting creates a new instance', () => {
let v0 = List.of('a');
let v1 = v0.set(0, 'A');
Expand Down
52 changes: 30 additions & 22 deletions dist/immutable.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -4711,27 +4711,8 @@ mixin(Collection, {
return this.find(function (_, key) { return is(key, searchKey); }, undefined, notSetValue);
},

getIn: function getIn(searchKeyPath, notSetValue) {
var nested = this;
var keyPath = coerceKeyPath(searchKeyPath);
var i = 0;
while (i !== keyPath.length) {
if (!nested || !nested.get) {
warn(
'Invalid keyPath: Value at [' +
keyPath.slice(0, i).map(quoteString) +
'] does not have a .get() method: ' +
nested +
'\nThis warning will throw in a future version'
);
return notSetValue;
}
nested = nested.get(keyPath[i++], NOT_SET);
if (nested === NOT_SET) {
return notSetValue;
}
}
return nested;
getIn: function getIn$1(searchKeyPath, notSetValue) {
return getIn(this, notSetValue, searchKeyPath, true /* report bad path */);
},

groupBy: function groupBy(grouper, context) {
Expand All @@ -4743,7 +4724,10 @@ mixin(Collection, {
},

hasIn: function hasIn(searchKeyPath) {
return this.getIn(searchKeyPath, NOT_SET) !== NOT_SET;
return (
getIn(this, NOT_SET, searchKeyPath, false /* report bad path */) !==
NOT_SET
);
},

isSubset: function isSubset(iter) {
Expand Down Expand Up @@ -5172,6 +5156,30 @@ function warn(message) {
/* eslint-enable no-console */
}

function getIn(value, notSetValue, searchKeyPath, reportBadKeyPath) {
var keyPath = coerceKeyPath(searchKeyPath);
var i = 0;
while (i !== keyPath.length) {
if (!value || !value.get) {
if (reportBadKeyPath) {
warn(
'Invalid keyPath: Value at [' +
keyPath.slice(0, i).map(quoteString) +
'] does not have a .get() method: ' +
value +
'\nThis warning will throw in a future version'
);
}
return notSetValue;
}
value = value.get(keyPath[i++], NOT_SET);
if (value === NOT_SET) {
return notSetValue;
}
}
return value;
}

var OrderedSet = (function (Set$$1) {
function OrderedSet(value) {
return value === null || value === undefined
Expand Down
52 changes: 30 additions & 22 deletions dist/immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4717,27 +4717,8 @@ mixin(Collection, {
return this.find(function (_, key) { return is(key, searchKey); }, undefined, notSetValue);
},

getIn: function getIn(searchKeyPath, notSetValue) {
var nested = this;
var keyPath = coerceKeyPath(searchKeyPath);
var i = 0;
while (i !== keyPath.length) {
if (!nested || !nested.get) {
warn(
'Invalid keyPath: Value at [' +
keyPath.slice(0, i).map(quoteString) +
'] does not have a .get() method: ' +
nested +
'\nThis warning will throw in a future version'
);
return notSetValue;
}
nested = nested.get(keyPath[i++], NOT_SET);
if (nested === NOT_SET) {
return notSetValue;
}
}
return nested;
getIn: function getIn$1(searchKeyPath, notSetValue) {
return getIn(this, notSetValue, searchKeyPath, true /* report bad path */);
},

groupBy: function groupBy(grouper, context) {
Expand All @@ -4749,7 +4730,10 @@ mixin(Collection, {
},

hasIn: function hasIn(searchKeyPath) {
return this.getIn(searchKeyPath, NOT_SET) !== NOT_SET;
return (
getIn(this, NOT_SET, searchKeyPath, false /* report bad path */) !==
NOT_SET
);
},

isSubset: function isSubset(iter) {
Expand Down Expand Up @@ -5178,6 +5162,30 @@ function warn(message) {
/* eslint-enable no-console */
}

function getIn(value, notSetValue, searchKeyPath, reportBadKeyPath) {
var keyPath = coerceKeyPath(searchKeyPath);
var i = 0;
while (i !== keyPath.length) {
if (!value || !value.get) {
if (reportBadKeyPath) {
warn(
'Invalid keyPath: Value at [' +
keyPath.slice(0, i).map(quoteString) +
'] does not have a .get() method: ' +
value +
'\nThis warning will throw in a future version'
);
}
return notSetValue;
}
value = value.get(keyPath[i++], NOT_SET);
if (value === NOT_SET) {
return notSetValue;
}
}
return value;
}

var OrderedSet = (function (Set$$1) {
function OrderedSet(value) {
return value === null || value === undefined
Expand Down
Loading