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.

90 lines
2.0 KiB

4 years ago
  1. import _curry1 from "./internal/_curry1.js";
  2. import _has from "./internal/_has.js";
  3. import _isArguments from "./internal/_isArguments.js"; // cover IE < 9 keys issues
  4. var hasEnumBug = !
  5. /*#__PURE__*/
  6. {
  7. toString: null
  8. }.propertyIsEnumerable('toString');
  9. var nonEnumerableProps = ['constructor', 'valueOf', 'isPrototypeOf', 'toString', 'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString']; // Safari bug
  10. var hasArgsEnumBug =
  11. /*#__PURE__*/
  12. function () {
  13. 'use strict';
  14. return arguments.propertyIsEnumerable('length');
  15. }();
  16. var contains = function contains(list, item) {
  17. var idx = 0;
  18. while (idx < list.length) {
  19. if (list[idx] === item) {
  20. return true;
  21. }
  22. idx += 1;
  23. }
  24. return false;
  25. };
  26. /**
  27. * Returns a list containing the names of all the enumerable own properties of
  28. * the supplied object.
  29. * Note that the order of the output array is not guaranteed to be consistent
  30. * across different JS platforms.
  31. *
  32. * @func
  33. * @memberOf R
  34. * @since v0.1.0
  35. * @category Object
  36. * @sig {k: v} -> [k]
  37. * @param {Object} obj The object to extract properties from
  38. * @return {Array} An array of the object's own properties.
  39. * @see R.keysIn, R.values
  40. * @example
  41. *
  42. * R.keys({a: 1, b: 2, c: 3}); //=> ['a', 'b', 'c']
  43. */
  44. var keys = typeof Object.keys === 'function' && !hasArgsEnumBug ?
  45. /*#__PURE__*/
  46. _curry1(function keys(obj) {
  47. return Object(obj) !== obj ? [] : Object.keys(obj);
  48. }) :
  49. /*#__PURE__*/
  50. _curry1(function keys(obj) {
  51. if (Object(obj) !== obj) {
  52. return [];
  53. }
  54. var prop, nIdx;
  55. var ks = [];
  56. var checkArgsLength = hasArgsEnumBug && _isArguments(obj);
  57. for (prop in obj) {
  58. if (_has(prop, obj) && (!checkArgsLength || prop !== 'length')) {
  59. ks[ks.length] = prop;
  60. }
  61. }
  62. if (hasEnumBug) {
  63. nIdx = nonEnumerableProps.length - 1;
  64. while (nIdx >= 0) {
  65. prop = nonEnumerableProps[nIdx];
  66. if (_has(prop, obj) && !contains(ks, prop)) {
  67. ks[ks.length] = prop;
  68. }
  69. nIdx -= 1;
  70. }
  71. }
  72. return ks;
  73. });
  74. export default keys;