addon-sdk/source/lib/sdk/util/object.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial