main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
877 bytes
|
Line | |
---|
1 | import { bind } from 'ramda';
|
---|
2 |
|
---|
3 | import isIterable from '../../isIterable';
|
---|
4 | import isNotUndefined from '../../isNotUndefined';
|
---|
5 | import isNotNil from '../../isNotNil';
|
---|
6 | import isNotFunction from '../../isNotFunction';
|
---|
7 |
|
---|
8 | const copyArray = (items, mapFn, thisArg) => {
|
---|
9 | const boundMapFn = isNotUndefined(thisArg) ? bind(mapFn, thisArg) : mapFn;
|
---|
10 |
|
---|
11 | return isNotUndefined(mapFn) ? [...items].map(boundMapFn) : [...items];
|
---|
12 | };
|
---|
13 |
|
---|
14 | const fromArray = (items, mapFn, thisArg) => {
|
---|
15 | if (items == null) {
|
---|
16 | throw new TypeError(
|
---|
17 | 'Array.from requires an array-like object - not null or undefined'
|
---|
18 | );
|
---|
19 | }
|
---|
20 |
|
---|
21 | if (isNotNil(mapFn) && isNotFunction(mapFn)) {
|
---|
22 | throw new TypeError(
|
---|
23 | 'Array.from: when provided, the second argument must be a function'
|
---|
24 | );
|
---|
25 | }
|
---|
26 |
|
---|
27 | if (isIterable(items)) {
|
---|
28 | return copyArray(items, mapFn, thisArg);
|
---|
29 | }
|
---|
30 |
|
---|
31 | return [];
|
---|
32 | };
|
---|
33 |
|
---|
34 | export default fromArray;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.