empty.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import _curry1 from "./internal/_curry1.js";
  2. import _isArguments from "./internal/_isArguments.js";
  3. import _isArray from "./internal/_isArray.js";
  4. import _isObject from "./internal/_isObject.js";
  5. import _isString from "./internal/_isString.js";
  6. import _isTypedArray from "./internal/_isTypedArray.js";
  7. /**
  8. * Returns the empty value of its argument's type. Ramda defines the empty
  9. * value of Array (`[]`), Object (`{}`), String (`''`),
  10. * TypedArray (`Uint8Array []`, `Float32Array []`, etc), and Arguments. Other
  11. * types are supported if they define `<Type>.empty`,
  12. * `<Type>.prototype.empty` or implement the
  13. * [FantasyLand Monoid spec](https://github.com/fantasyland/fantasy-land#monoid).
  14. *
  15. * Dispatches to the `empty` method of the first argument, if present.
  16. *
  17. * @func
  18. * @memberOf R
  19. * @since v0.3.0
  20. * @category Function
  21. * @sig a -> a
  22. * @param {*} x
  23. * @return {*}
  24. * @example
  25. *
  26. * R.empty(Just(42)); //=> Nothing()
  27. * R.empty([1, 2, 3]); //=> []
  28. * R.empty('unicorns'); //=> ''
  29. * R.empty({x: 1, y: 2}); //=> {}
  30. * R.empty(Uint8Array.from('123')); //=> Uint8Array []
  31. */
  32. var empty =
  33. /*#__PURE__*/
  34. _curry1(function empty(x) {
  35. return x != null && typeof x['fantasy-land/empty'] === 'function' ? x['fantasy-land/empty']() : x != null && x.constructor != null && typeof x.constructor['fantasy-land/empty'] === 'function' ? x.constructor['fantasy-land/empty']() : x != null && typeof x.empty === 'function' ? x.empty() : x != null && x.constructor != null && typeof x.constructor.empty === 'function' ? x.constructor.empty() : _isArray(x) ? [] : _isString(x) ? '' : _isObject(x) ? {} : _isArguments(x) ? function () {
  36. return arguments;
  37. }() : _isTypedArray(x) ? x.constructor.from('') : void 0 // else
  38. ;
  39. });
  40. export default empty;