1 | import { concat, identical, identity, pipe, reduce, when } from 'ramda';
|
---|
2 |
|
---|
3 | import stubUndefined from './stubUndefined';
|
---|
4 |
|
---|
5 | const leftIdentitySemigroup = { concat: identity };
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Returns the result of concatenating the given lists or strings.
|
---|
9 | * Note: RA.concatAll expects all elements to be of the same type. It will throw an error if you concat an Array with a non-Array value.
|
---|
10 | * Dispatches to the concat method of the preceding element, if present. Can also concatenate multiple elements of a [fantasy-land compatible semigroup](https://github.com/fantasyland/fantasy-land#semigroup).
|
---|
11 | * Returns undefined if empty array was passed.
|
---|
12 | *
|
---|
13 | * @func concatAll
|
---|
14 | * @memberOf RA
|
---|
15 | * @since {@link https://char0n.github.io/ramda-adjunct/2.6.0|v2.6.0}
|
---|
16 | * @category List
|
---|
17 | * @sig [[a]] -> [a] | Undefined
|
---|
18 | * @sig [String] -> String | Undefined
|
---|
19 | * @sig Semigroup s => Foldable s f => f -> s | Undefined
|
---|
20 | * @param {Array.<Array|string>} list List containing elements that will be concatenated
|
---|
21 | * @return {Array|string|undefined} Concatenated elements
|
---|
22 | * @see {@link http://ramdajs.com/docs/#concat|R.concat}, {@link RA.concatRight|concatRight}, {@link http://ramdajs.com/docs/#unnest|R.unnest}, {@link http://ramdajs.com/docs/#join|R.join}
|
---|
23 | * @example
|
---|
24 | *
|
---|
25 | * concatAll([[1], [2], [3]]); //=> [1, 2, 3]
|
---|
26 | * concatAll(['1', '2', '3']); //=> '123'
|
---|
27 | * concatAll([]); //=> undefined
|
---|
28 | * concatAll(null); //=> undefined
|
---|
29 | */
|
---|
30 | const concatAll = pipe(
|
---|
31 | reduce(concat, leftIdentitySemigroup),
|
---|
32 | when(identical(leftIdentitySemigroup), stubUndefined)
|
---|
33 | );
|
---|
34 |
|
---|
35 | export default concatAll;
|
---|