You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.8 KiB

4 years ago
  1. import _curry2 from "./internal/_curry2.js";
  2. import _isArray from "./internal/_isArray.js";
  3. import _isFunction from "./internal/_isFunction.js";
  4. import _isString from "./internal/_isString.js";
  5. import toString from "./toString.js";
  6. /**
  7. * Returns the result of concatenating the given lists or strings.
  8. *
  9. * Note: `R.concat` expects both arguments to be of the same type,
  10. * unlike the native `Array.prototype.concat` method. It will throw
  11. * an error if you `concat` an Array with a non-Array value.
  12. *
  13. * Dispatches to the `concat` method of the first argument, if present.
  14. * Can also concatenate two members of a [fantasy-land
  15. * compatible semigroup](https://github.com/fantasyland/fantasy-land#semigroup).
  16. *
  17. * @func
  18. * @memberOf R
  19. * @since v0.1.0
  20. * @category List
  21. * @sig [a] -> [a] -> [a]
  22. * @sig String -> String -> String
  23. * @param {Array|String} firstList The first list
  24. * @param {Array|String} secondList The second list
  25. * @return {Array|String} A list consisting of the elements of `firstList` followed by the elements of
  26. * `secondList`.
  27. *
  28. * @example
  29. *
  30. * R.concat('ABC', 'DEF'); // 'ABCDEF'
  31. * R.concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
  32. * R.concat([], []); //=> []
  33. */
  34. var concat =
  35. /*#__PURE__*/
  36. _curry2(function concat(a, b) {
  37. if (_isArray(a)) {
  38. if (_isArray(b)) {
  39. return a.concat(b);
  40. }
  41. throw new TypeError(toString(b) + ' is not an array');
  42. }
  43. if (_isString(a)) {
  44. if (_isString(b)) {
  45. return a + b;
  46. }
  47. throw new TypeError(toString(b) + ' is not a string');
  48. }
  49. if (a != null && _isFunction(a['fantasy-land/concat'])) {
  50. return a['fantasy-land/concat'](b);
  51. }
  52. if (a != null && _isFunction(a.concat)) {
  53. return a.concat(b);
  54. }
  55. throw new TypeError(toString(a) + ' does not have a method named "concat" or "fantasy-land/concat"');
  56. });
  57. export default concat;