Skip to content

Add seamless support for ImmutableJS structures in select() path #160

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 4 commits into from
Jul 18, 2016
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"es6-shim": "^0.35.0",
"expect": "^1.8.0",
"mocha": "^2.4.5",
"proxyquire": "^1.7.10",
"redux": "^3.4.0",
"reflect-metadata": "0.1.3",
"rimraf": "^2.5.2",
Expand Down
41 changes: 41 additions & 0 deletions src/___tests___/utils/get-in.immutable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const proxyquire = require('proxyquire');

const { getIn: getInWithImmutable } = proxyquire('../../utils/get-in', {
immutable: {
Iterable: {
isIterable: value => typeof value.getIn === 'function',
},
'@noCallThru': true,
},
});

const { getIn: getInWithNoImmutable } = require('../../utils/get-in');

import { expect } from 'chai';

describe('getIn', () => {
it('should make use of immutable when available in host project', () => {
const getIn =
path => {
expect(path.length).to.equal(1);
expect(path[0]).to.equal('foo');
return 't';
};

const fakeImmutable = { getIn: getIn };

expect(getInWithImmutable(fakeImmutable, [ 'foo' ])).to.equal('t');
});

it('should work on regular objects even when immutable is available', () => {
const test = { foo: 1 };

expect(getInWithImmutable(test, [ 'foo' ])).to.equal(1);
});

it('should run without immutable when immutable is not available', () => {
const test = { foo: 1 };

expect(getInWithNoImmutable(test, [ 'foo' ])).to.equal(1);
});
});
16 changes: 15 additions & 1 deletion src/utils/get-in.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
// This may look suspicious, but the point is to add ImmutableJS integration
// only if the project itself already has a dependency on immutable. If not,
// then this variable is left null and no integration is attempted.
let immutable;
try {
immutable = require('immutable');
} catch (e) {}

/*
* Gets a deeply-nested property value from an object, given a 'path'
* of property names or array indices.
*/
export function getIn(
v: Object,
pathElems: (string | number)[]): any {
const [ firstElem, ...restElems] = pathElems;
if (!v) {
return v;
}

if (immutable && immutable.Iterable.isIterable(v)) {
return (<{getIn: (path: (string | number)[]) => any}>v).getIn(pathElems);
}

const [ firstElem, ...restElems] = pathElems;

if (undefined === v[firstElem]) {
return undefined;
}
Expand All @@ -20,3 +33,4 @@ export function getIn(

return getIn(v[firstElem], restElems);
}