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.

76 lines
2.4 KiB

4 years ago
  1. #object-keys <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
  2. [![Build Status][travis-svg]][travis-url]
  3. [![dependency status][deps-svg]][deps-url]
  4. [![dev dependency status][dev-deps-svg]][dev-deps-url]
  5. [![License][license-image]][license-url]
  6. [![Downloads][downloads-image]][downloads-url]
  7. [![npm badge][npm-badge-png]][package-url]
  8. [![browser support][testling-svg]][testling-url]
  9. An Object.keys shim. Invoke its "shim" method to shim Object.keys if it is unavailable.
  10. Most common usage:
  11. ```js
  12. var keys = Object.keys || require('object-keys');
  13. ```
  14. ## Example
  15. ```js
  16. var keys = require('object-keys');
  17. var assert = require('assert');
  18. var obj = {
  19. a: true,
  20. b: true,
  21. c: true
  22. };
  23. assert.deepEqual(keys(obj), ['a', 'b', 'c']);
  24. ```
  25. ```js
  26. var keys = require('object-keys');
  27. var assert = require('assert');
  28. /* when Object.keys is not present */
  29. delete Object.keys;
  30. var shimmedKeys = keys.shim();
  31. assert.equal(shimmedKeys, keys);
  32. assert.deepEqual(Object.keys(obj), keys(obj));
  33. ```
  34. ```js
  35. var keys = require('object-keys');
  36. var assert = require('assert');
  37. /* when Object.keys is present */
  38. var shimmedKeys = keys.shim();
  39. assert.equal(shimmedKeys, Object.keys);
  40. assert.deepEqual(Object.keys(obj), keys(obj));
  41. ```
  42. ## Source
  43. Implementation taken directly from [es5-shim][es5-shim-url], with modifications, including from [lodash][lodash-url].
  44. ## Tests
  45. Simply clone the repo, `npm install`, and run `npm test`
  46. [package-url]: https://npmjs.org/package/object-keys
  47. [npm-version-svg]: http://versionbadg.es/ljharb/object-keys.svg
  48. [travis-svg]: https://travis-ci.org/ljharb/object-keys.svg
  49. [travis-url]: https://travis-ci.org/ljharb/object-keys
  50. [deps-svg]: https://david-dm.org/ljharb/object-keys.svg
  51. [deps-url]: https://david-dm.org/ljharb/object-keys
  52. [dev-deps-svg]: https://david-dm.org/ljharb/object-keys/dev-status.svg
  53. [dev-deps-url]: https://david-dm.org/ljharb/object-keys#info=devDependencies
  54. [testling-svg]: https://ci.testling.com/ljharb/object-keys.png
  55. [testling-url]: https://ci.testling.com/ljharb/object-keys
  56. [es5-shim-url]: https://github.com/es-shims/es5-shim/blob/master/es5-shim.js#L542-589
  57. [lodash-url]: https://github.com/lodash/lodash
  58. [npm-badge-png]: https://nodei.co/npm/object-keys.png?downloads=true&stars=true
  59. [license-image]: http://img.shields.io/npm/l/object-keys.svg
  60. [license-url]: LICENSE
  61. [downloads-image]: http://img.shields.io/npm/dm/object-keys.svg
  62. [downloads-url]: http://npm-stat.com/charts.html?package=object-keys