diff --git a/src/function.js b/src/function.js index 566400d..86db977 100644 --- a/src/function.js +++ b/src/function.js @@ -11,11 +11,13 @@ import { head, isIterable, concatIter, iter, tail } from './list.js' */ export const id = x => x +const bareValue = x => Array.isArray(x) || typeof x === 'string' || !isIterable(x) + export const concat = dispatch(['fantasy-land/concat', 'concat'], (b, a) => { - if (Array.isArray(a) && (Array.isArray(b) || !isIterable(b))) { + if (Array.isArray(a) && bareValue(b)) { return a.concat(b) - } else if (Array.isArray(b) && (Array.isArray(a) || !isIterable(a))) { + } else if (Array.isArray(b) && bareValue(a)) { return b.concat(a) } else { return concatIter(iter(a), iter(b)) diff --git a/test/units/function.js b/test/units/function.js index 4ef6512..6729cec 100644 --- a/test/units/function.js +++ b/test/units/function.js @@ -5,8 +5,13 @@ import { isIterable, iter } from '../..//src/list.js' export const Functions = [ it('concat', () => { assertEq(concat(1, []), [1]) + assertEq(concat('a', []), ['a']) + assertEq(concat([], 1), [1]) + assertEq(concat([], 'a'), ['a']) + assertEq(concat([2], [1]), [1, 2]) + const a = concat([2], iter([1])) assert(isIterable(a), 'concat([2], iter([1])) returned a non-iterable') assertEq([...a], [1, 2])