Object.es7.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @polyfill
  9. * @nolint
  10. */
  11. (function() {
  12. 'use strict';
  13. const hasOwnProperty = Object.prototype.hasOwnProperty;
  14. /**
  15. * Returns an array of the given object's own enumerable entries.
  16. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
  17. */
  18. if (typeof Object.entries !== 'function') {
  19. Object.entries = function(object) {
  20. // `null` and `undefined` values are not allowed.
  21. if (object == null) {
  22. throw new TypeError('Object.entries called on non-object');
  23. }
  24. const entries = [];
  25. for (const key in object) {
  26. if (hasOwnProperty.call(object, key)) {
  27. entries.push([key, object[key]]);
  28. }
  29. }
  30. return entries;
  31. };
  32. }
  33. /**
  34. * Returns an array of the given object's own enumerable entries.
  35. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values
  36. */
  37. if (typeof Object.values !== 'function') {
  38. Object.values = function(object) {
  39. // `null` and `undefined` values are not allowed.
  40. if (object == null) {
  41. throw new TypeError('Object.values called on non-object');
  42. }
  43. const values = [];
  44. for (const key in object) {
  45. if (hasOwnProperty.call(object, key)) {
  46. values.push(object[key]);
  47. }
  48. }
  49. return values;
  50. };
  51. }
  52. })();