File tree 3 files changed +54
-6
lines changed 3 files changed +54
-6
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Copyright (c) 2014-present, Facebook, Inc.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ describe ( 'toJS' , ( ) => {
9
+ const array32 = [ ] ;
10
+ for ( let ii = 0 ; ii < 32 ; ii ++ ) {
11
+ array32 [ ii ] = ii ;
12
+ }
13
+ const list = Immutable . List ( array32 ) ;
14
+
15
+ it ( 'List of 32' , ( ) => {
16
+ Immutable . toJS ( list ) ;
17
+ } ) ;
18
+
19
+ const obj32 = { } ;
20
+ for ( let ii = 0 ; ii < 32 ; ii ++ ) {
21
+ obj32 [ ii ] = ii ;
22
+ }
23
+ const map = Immutable . Map ( obj32 ) ;
24
+
25
+ it ( 'Map of 32' , ( ) => {
26
+ Immutable . toJS ( map ) ;
27
+ } ) ;
28
+ } ) ;
Original file line number Diff line number Diff line change 6
6
*/
7
7
8
8
import { Seq } from './Seq' ;
9
+ import { isCollection , isKeyed } from './Predicates' ;
9
10
import isDataStructure from './utils/isDataStructure' ;
10
11
11
12
export function toJS ( value ) {
12
- return isDataStructure ( value )
13
- ? Seq ( value )
14
- . map ( toJS )
15
- . toJSON ( )
16
- : value ;
13
+ if ( ! value || typeof value !== 'object' ) {
14
+ return value ;
15
+ }
16
+ if ( ! isCollection ( value ) ) {
17
+ if ( ! isDataStructure ( value ) ) {
18
+ return value ;
19
+ }
20
+ value = Seq ( value ) ;
21
+ }
22
+ if ( isKeyed ( value ) ) {
23
+ const result = { } ;
24
+ value . __iterate ( ( v , k ) => {
25
+ result [ k ] = toJS ( v ) ;
26
+ } ) ;
27
+ return result ;
28
+ }
29
+ const result = [ ] ;
30
+ value . __iterate ( v => {
31
+ result . push ( toJS ( v ) ) ;
32
+ } ) ;
33
+ return result ;
17
34
}
Original file line number Diff line number Diff line change @@ -13,5 +13,8 @@ import isPlainObj from './isPlainObj';
13
13
* provided by Immutable.js or a plain Array or Object.
14
14
*/
15
15
export default function isDataStructure ( value ) {
16
- return isImmutable ( value ) || Array . isArray ( value ) || isPlainObj ( value ) ;
16
+ return (
17
+ typeof value === 'object' &&
18
+ ( isImmutable ( value ) || Array . isArray ( value ) || isPlainObj ( value ) )
19
+ ) ;
17
20
}
You can’t perform that action at this time.
0 commit comments