object.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. module.metadata = {
  6. "stability": "unstable"
  7. };
  8. const { flatten } = require('./array');
  9. /**
  10. * Merges all the properties of all arguments into first argument. If two or
  11. * more argument objects have own properties with the same name, the property
  12. * is overridden, with precedence from right to left, implying, that properties
  13. * of the object on the left are overridden by a same named property of the
  14. * object on the right.
  15. *
  16. * Any argument given with "falsy" value - commonly `null` and `undefined` in
  17. * case of objects - are skipped.
  18. *
  19. * @examples
  20. * var a = { bar: 0, a: 'a' }
  21. * var b = merge(a, { foo: 'foo', bar: 1 }, { foo: 'bar', name: 'b' });
  22. * b === a // true
  23. * b.a // 'a'
  24. * b.foo // 'bar'
  25. * b.bar // 1
  26. * b.name // 'b'
  27. */
  28. function merge(source) {
  29. let descriptor = {};
  30. // `Boolean` converts the first parameter to a boolean value. Any object is
  31. // converted to `true` where `null` and `undefined` becames `false`. Therefore
  32. // the `filter` method will keep only objects that are defined and not null.
  33. Array.slice(arguments, 1).filter(Boolean).forEach(function onEach(properties) {
  34. Object.getOwnPropertyNames(properties).forEach(function(name) {
  35. descriptor[name] = Object.getOwnPropertyDescriptor(properties, name);
  36. });
  37. });
  38. return Object.defineProperties(source, descriptor);
  39. }
  40. exports.merge = merge;
  41. /**
  42. * Returns an object that inherits from the first argument and contains all the
  43. * properties from all following arguments.
  44. * `extend(source1, source2, source3)` is equivalent of
  45. * `merge(Object.create(source1), source2, source3)`.
  46. */
  47. function extend(source) {
  48. let rest = Array.slice(arguments, 1);
  49. rest.unshift(Object.create(source));
  50. return merge.apply(null, rest);
  51. }
  52. exports.extend = extend;
  53. function has(obj, key) obj.hasOwnProperty(key);
  54. exports.has = has;
  55. function each(obj, fn) {
  56. for (let key in obj) has(obj, key) && fn(obj[key], key, obj);
  57. }
  58. exports.each = each;
  59. /**
  60. * Like `merge`, except no property descriptors are manipulated, for use
  61. * with platform objects. Identical to underscore's `extend`. Useful for
  62. * merging XPCOM objects
  63. */
  64. function safeMerge(source) {
  65. Array.slice(arguments, 1).forEach(function onEach (obj) {
  66. for (let prop in obj) source[prop] = obj[prop];
  67. });
  68. return source;
  69. }
  70. exports.safeMerge = safeMerge;
  71. /*
  72. * Returns a copy of the object without blacklisted properties
  73. */
  74. function omit(source, ...values) {
  75. let copy = {};
  76. let keys = flatten(values);
  77. for (let prop in source)
  78. if (!~keys.indexOf(prop))
  79. copy[prop] = source[prop];
  80. return copy;
  81. }
  82. exports.omit = omit;