File tree 3 files changed +57
-1
lines changed 3 files changed +57
-1
lines changed Original file line number Diff line number Diff line change 51
51
"es6-shim" : " ^0.35.0" ,
52
52
"expect" : " ^1.8.0" ,
53
53
"mocha" : " ^2.4.5" ,
54
+ "proxyquire" : " ^1.7.10" ,
54
55
"redux" : " ^3.4.0" ,
55
56
"reflect-metadata" : " 0.1.3" ,
56
57
"rimraf" : " ^2.5.2" ,
Original file line number Diff line number Diff line change
1
+ const proxyquire = require ( 'proxyquire' ) ;
2
+
3
+ const { getIn : getInWithImmutable } = proxyquire ( '../../utils/get-in' , {
4
+ immutable : {
5
+ Iterable : {
6
+ isIterable : value => typeof value . getIn === 'function' ,
7
+ } ,
8
+ '@noCallThru' : true ,
9
+ } ,
10
+ } ) ;
11
+
12
+ const { getIn : getInWithNoImmutable } = require ( '../../utils/get-in' ) ;
13
+
14
+ import { expect } from 'chai' ;
15
+
16
+ describe ( 'getIn' , ( ) => {
17
+ it ( 'should make use of immutable when available in host project' , ( ) => {
18
+ const getIn =
19
+ path => {
20
+ expect ( path . length ) . to . equal ( 1 ) ;
21
+ expect ( path [ 0 ] ) . to . equal ( 'foo' ) ;
22
+ return 't' ;
23
+ } ;
24
+
25
+ const fakeImmutable = { getIn : getIn } ;
26
+
27
+ expect ( getInWithImmutable ( fakeImmutable , [ 'foo' ] ) ) . to . equal ( 't' ) ;
28
+ } ) ;
29
+
30
+ it ( 'should work on regular objects even when immutable is available' , ( ) => {
31
+ const test = { foo : 1 } ;
32
+
33
+ expect ( getInWithImmutable ( test , [ 'foo' ] ) ) . to . equal ( 1 ) ;
34
+ } ) ;
35
+
36
+ it ( 'should run without immutable when immutable is not available' , ( ) => {
37
+ const test = { foo : 1 } ;
38
+
39
+ expect ( getInWithNoImmutable ( test , [ 'foo' ] ) ) . to . equal ( 1 ) ;
40
+ } ) ;
41
+ } ) ;
Original file line number Diff line number Diff line change
1
+ // This may look suspicious, but the point is to add ImmutableJS integration
2
+ // only if the project itself already has a dependency on immutable. If not,
3
+ // then this variable is left null and no integration is attempted.
4
+ let immutable ;
5
+ try {
6
+ immutable = require ( 'immutable' ) ;
7
+ } catch ( e ) { }
8
+
1
9
/*
2
10
* Gets a deeply-nested property value from an object, given a 'path'
3
11
* of property names or array indices.
4
12
*/
5
13
export function getIn (
6
14
v : Object ,
7
15
pathElems : ( string | number ) [ ] ) : any {
8
- const [ firstElem , ...restElems ] = pathElems ;
9
16
if ( ! v ) {
10
17
return v ;
11
18
}
12
19
20
+ if ( immutable && immutable . Iterable . isIterable ( v ) ) {
21
+ return ( < { getIn : ( path : ( string | number ) [ ] ) => any } > v ) . getIn ( pathElems ) ;
22
+ }
23
+
24
+ const [ firstElem , ...restElems ] = pathElems ;
25
+
13
26
if ( undefined === v [ firstElem ] ) {
14
27
return undefined ;
15
28
}
@@ -20,3 +33,4 @@ export function getIn(
20
33
21
34
return getIn ( v [ firstElem ] , restElems ) ;
22
35
}
36
+
You can’t perform that action at this time.
0 commit comments