Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c6cdd4f

Browse filesBrowse files
committed
Merge branch 'array-like'
2 parents 02e539d + 590bf98 commit c6cdd4f
Copy full SHA for c6cdd4f

File tree

Expand file treeCollapse file tree

4 files changed

+27
-9
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+27
-9
lines changed

‎__tests__/List.ts

Copy file name to clipboardExpand all lines: __tests__/List.ts
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ describe('List', () => {
5858
});
5959

6060
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']);
6464
});
6565

6666
it('accepts any array-like collection, including strings', () => {

‎__tests__/Seq.ts

Copy file name to clipboardExpand all lines: __tests__/Seq.ts
+10-2Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,19 @@ describe('Seq', () => {
4747
});
4848

4949
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' });
5251
expect(isIndexed(seq)).toBe(true);
5352
expect(seq.size).toBe(2);
5453
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);
5563
});
5664

5765
it('does not accept a scalar', () => {

‎__tests__/Set.ts

Copy file name to clipboardExpand all lines: __tests__/Set.ts
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ describe('Set', () => {
2020
});
2121

2222
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);
2424
expect(s.size).toBe(2);
2525
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);
2828
});
2929

3030
it('accepts string, an array-like collection', () => {

‎src/utils/isArrayLike.js

Copy file name to clipboardExpand all lines: src/utils/isArrayLike.js
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,15 @@
66
*/
77

88
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+
);
1020
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.