8000 Allow intermediate null value in getIn path. by leebyron · Pull Request #1368 · immutable-js/immutable-js · GitHub
[go: up one dir, main page]

Skip to content

Allow intermediate null value in getIn path. #1368

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
Oct 10, 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
47 changes: 29 additions & 18 deletions __tests__/getIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,34 @@ describe('getIn', () => {
).toThrow('Invalid keyPath: expected Ordered Collection or Array: abc');
});

it('deep get throws if non-readable path', () => {
function withWarnings(fn) {
const realWarn = console.warn;
const warnings: Array<any> = [];
console.warn = w => warnings.push(w);

try {
const deep = Map({ key: { regular: "jsobj" }, list: List([ Map({num: 10}) ]) });
deep.getIn(["key", "foo", "item"]);
expect(warnings.length).toBe(1);
expect(warnings[0]).toBe(
'Invalid keyPath: Value at ["key"] does not have a .get() method: [object Object]' +
'\nThis warning will throw in a future version',
);

warnings.length = 0;
deep.getIn(["list", 0, "num", "badKey"]);
expect(warnings.length).toBe(1);
expect(warnings[0]).toBe(
'Invalid keyPath: Value at ["list",0,"num"] does not have a .get() method: 10' +
'\nThis warning will throw in a future version',
);
fn(warnings);
} finally {
console.warn = realWarn;
}
});
}

it('deep get throws if non-readable path', withWarnings(warnings => {
const deep = Map({ key: { regular: "jsobj" }, list: List([ Map({num: 10}) ]) });
deep.getIn(["key", "foo", "item"]);
expect(warnings.length).toBe(1);
expect(warnings[0]).toBe(
'Invalid keyPath: Value at ["key"] does not have a .get() method: [object Object]' +
'\nThis warning will throw in a future version',
);

warnings.length = 0;
deep.getIn(["list", 0, "num", "badKey"]);
expect(warnings.length).toBe(1);
expect(warnings[0]).toBe(
'Invalid keyPath: Value at ["list",0,"num"] does not have a .get() method: 10' +
'\nThis warning will throw in a future version',
);
}));

it('deep get returns not found if path does not match', () => {
const m = fromJS({a: {b: {c: 10}}});
Expand All @@ -76,4 +79,12 @@ describe('getIn', () => {
expect(m.getIn(['a', 'b', 'd'], 123)).toEqual(undefined);
});

it('deep get returns not found if path encounters null or undefined', withWarnings(warnings => {
const m = fromJS({a: {b: {c: null, d: undefined}}});
expect(m.getIn(['a', 'b', 'c', 'x'])).toEqual(undefined);
expect(m.getIn(['a', 'b', 'd', 'x'])).toEqual(undefined);
expect(m.getIn(['a', 'b', 'c', 'x'], 123)).toEqual(123);
expect(m.getIn(['a', 'b', 'd', 'x'], 123)).toEqual(123);
expect(warnings.length).toBe(0);
}));
});
4 changes: 4 additions & 0 deletions src/CollectionImpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,10 @@ function getIn(value, notSetValue, searchKeyPath, reportBadKeyPath) {
const keyPath = coerceKeyPath(searchKeyPath);
let i = 0;
while (i !== keyPath.length) {
// Intermediate null/undefined value along path
if (value == null) {
return notSetValue;
}
if (!value || !value.get) {
if (reportBadKeyPath) {
warn(
Expand Down
0