'use strict'; /** @module overrider */ /** * Mixes members of all `sources` into `target`, handling getters and setters properly. * * Any number of `sources` objects may be given and each is copied in turn. * * @example * var overrider = require('overrider'); * var target = { a: 1 }, source1 = { b: 2 }, source2 = { c: 3 }; * target === overrider(target, source1, source2); // true * // target object now has a, b, and c; source objects untouched * * @param {object} object - The target object to receive sources. * @param {...object} [sources] - Object(s) containing members to copy to `target`. (Omitting is a no-op.) * @returns {object} The target object (`target`) */ function overrider(target, sources) { // eslint-disable-line no-unused-vars for (var i = 1; i < arguments.length; ++i) { mixIn.call(target, arguments[i]); } return target; } /** * Mix `this` members into `target`. * * @example * // A. Simple usage (using .call): * var mixInTo = require('overrider').mixInTo; * var target = { a: 1 }, source = { b: 2 }; * target === overrider.mixInTo.call(source, target); // true * // target object now has both a and b; source object untouched * * @example * // B. Semantic usage (when the source hosts the method): * var mixInTo = require('overrider').mixInTo; * var target = { a: 1 }, source = { b: 2, mixInTo: mixInTo }; * target === source.mixInTo(target); // true * // target object now has both a and b; source object untouched * * @this {object} Target. * @param target * @returns {object} The target object (`target`) * @memberOf module:overrider */ function mixInTo(target) { var descriptor; for (var key in this) { if ((descriptor = Object.getOwnPropertyDescriptor(this, key))) { Object.defineProperty(target, key, descriptor); } } return target; } /** * Mix `source` members into `this`. * * @example * // A. Simple usage (using .call): * var mixIn = require('overrider').mixIn; * var target = { a: 1 }, source = { b: 2 }; * target === overrider.mixIn.call(target, source) // true * // target object now has both a and b; source object untouched * * @example * // B. Semantic usage (when the target hosts the method): * var mixIn = require('overrider').mixIn; * var target = { a: 1, mixIn: mixIn }, source = { b: 2 }; * target === target.mixIn(source) // true * // target now has both a and b (and mixIn); source untouched * * @param source * @returns {object} The target object (`this`) * @memberOf overrider * @memberOf module:overrider */ function mixIn(source) { var descriptor; for (var key in source) { if ((descriptor = Object.getOwnPropertyDescriptor(source, key))) { Object.defineProperty(this, key, descriptor); } } return this; } overrider.mixInTo = mixInTo; overrider.mixIn = mixIn; module.exports = overrider;