toArray.js 694 B

1234567891011121314151617181920
  1. import isArray from './isArray.js';
  2. import { slice } from './_setup.js';
  3. import isString from './isString.js';
  4. import isArrayLike from './_isArrayLike.js';
  5. import map from './map.js';
  6. import identity from './identity.js';
  7. import values from './values.js';
  8. // Safely create a real, live array from anything iterable.
  9. var reStrSymbol = /[^\ud800-\udfff]|[\ud800-\udbff][\udc00-\udfff]|[\ud800-\udfff]/g;
  10. export default function toArray(obj) {
  11. if (!obj) return [];
  12. if (isArray(obj)) return slice.call(obj);
  13. if (isString(obj)) {
  14. // Keep surrogate pair characters together.
  15. return obj.match(reStrSymbol);
  16. }
  17. if (isArrayLike(obj)) return map(obj, identity);
  18. return values(obj);
  19. }