File tree Expand file tree Collapse file tree 4 files changed +27
-9
lines changed
Filter options
Expand file tree Collapse file tree 4 files changed +27
-9
lines changed
Original file line number Diff line number Diff line change @@ -58,9 +58,9 @@ describe('List', () => {
58
58
} ) ;
59
59
60
60
it ( 'accepts an array-like' , ( ) => {
61
- const v = List ( { length : 3 , 1 : 'b ' } as any ) ;
62
- expect ( v . get ( 1 ) ) . toBe ( 'b ' ) ;
63
- expect ( v . toArray ( ) ) . toEqual ( [ undefined , 'b' , undefined ] ) ;
61
+ const v = List ( { length : 3 , 2 : 'c ' } as any ) ;
62
+ expect ( v . get ( 2 ) ) . toBe ( 'c ' ) ;
63
+ expect ( v . toArray ( ) ) . toEqual ( [ undefined , undefined , 'c' ] ) ;
64
64
} ) ;
65
65
66
66
it ( 'accepts any array-like collection, including strings' , ( ) => {
Original file line number Diff line number Diff line change @@ -47,11 +47,19 @@ describe('Seq', () => {
47
47
} ) ;
48
48
49
49
it ( 'accepts an array-like' , ( ) => {
50
- const alike : any = { length : 2 , 0 : 'a' , 1 : 'b' } ;
51
- const seq = Seq ( alike ) ;
50
+ const seq = Seq ( { length : 2 , 0 : 'a' , 1 : 'b' } ) ;
52
51
expect ( isIndexed ( seq ) ) . toBe ( true ) ;
53
52
expect ( seq . size ) . toBe ( 2 ) ;
54
53
expect ( seq . get ( 1 ) ) . toBe ( 'b' ) ;
54
+
55
+ const map = Seq ( { length : 1 , foo : 'bar' } ) ;
56
+ expect ( isIndexed ( map ) ) . toBe ( false ) ;
57
+ expect ( map . size ) . toBe ( 2 ) ;
58
+ expect ( map . get ( 'foo' ) ) . toBe ( 'bar' ) ;
59
+
60
+ const empty = Seq ( { length : 0 } ) ;
61
+ expect ( isIndexed ( empty ) ) . toBe ( true ) ;
62
+ expect ( empty . size ) . toEqual ( 0 ) ;
55
63
} ) ;
56
64
57
65
it ( 'does not accept a scalar' , ( ) => {
Original file line number Diff line number Diff line change @@ -20,11 +20,11 @@ describe('Set', () => {
20
20
} ) ;
21
21
22
22
it ( 'accepts array-like of values' , ( ) => {
23
- const s = Set < any > ( { length : 3 , 1 : 2 } as any ) ;
23
+ const s = Set < any > ( { length : 3 , 2 : 3 } as any ) ;
24
24
expect ( s . size ) . toBe ( 2 ) ;
25
25
expect ( s . has ( undefined ) ) . toBe ( true ) ;
26
- expect ( s . has ( 2 ) ) . toBe ( true ) ;
27
- expect ( s . has ( 1 ) ) . toBe ( false ) ;
26
+ expect ( s . has ( 3 ) ) . toBe ( true ) ;
27
+ expect ( s . has ( 2 ) ) . toBe ( false ) ;
28
28
} ) ;
29
29
30
30
it ( 'accepts string, an array-like collection' , ( ) => {
Original file line number Diff line number Diff line change 6
6
*/
7
7
8
8
export default function isArrayLike ( value ) {
9
- return value && typeof value . length === 'number' ;
9
+ if ( Array . isArray ( value ) || typeof value === 'string' ) {
10
+ return true ;
11
+ }
12
+
13
+ return (
14
+ value &&
15
+ typeof value === 'object' &&
16
+ typeof value . length === 'number' &&
17
+ ( ( value . length && value . length - 1 in value ) ||
18
+ ( ! value . length && Object . keys ( value ) . length <= 1 ) )
19
+ ) ;
10
20
}
You can’t perform that action at this time.
0 commit comments