mergefcn.js (3129B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isObject = require( '@stdlib/assert/is-object' ); 24 var deepMerge = require( './deepmerge.js' ); 25 26 27 // MAIN // 28 29 /** 30 * Returns a merge function based on provided options. 31 * 32 * @private 33 * @param {Options} opts - function options 34 * @param {number} options.level - merge level 35 * @param {boolean} options.copy - boolean indicating whether to deep copy merged values 36 * @param {(boolean|Function)} options.override - defines the merge strategy 37 * @param {boolean} options.extend - boolean indicating whether new properties can be added to the target object 38 * @returns {Function} merge function 39 * 40 * @example 41 * var merge = mergefcn({ 42 * 'level': Number.POSITIVE_INFINITY, 43 * 'copy': true, 44 * 'override': true, 45 * 'extend': true 46 * }); 47 * // returns <Function> 48 */ 49 function mergefcn( opts ) { 50 return merge; 51 52 /** 53 * Merges objects into a target object. Note that the target object is mutated. 54 * 55 * @private 56 * @param {Object} target - target object 57 * @param {...Object} source - source objects (i.e., objects to be merged into the target object) 58 * @throws {Error} must provide a target object and one or more source objects 59 * @throws {TypeError} first argument must be an object 60 * @throws {TypeError} source arguments must be objects 61 * @returns {Object} merged (target) object 62 * 63 * @example 64 * var target = { 65 * 'a': 'beep' 66 * }; 67 * var source = { 68 * 'a': 'boop', 69 * 'b': 'bap' 70 * }; 71 * 72 * var out = merge( target, source ); 73 * // returns {'a':'boop', 'b':'bap'} 74 */ 75 function merge( target ) { 76 var nargs; 77 var arg; 78 var src; 79 var i; 80 81 nargs = arguments.length - 1; 82 if ( nargs < 1 ) { 83 throw new Error( 'insufficient input arguments. Must provide both a target object and one or more source objects.' ); 84 } 85 if ( !isObject( target ) ) { 86 throw new TypeError( 'invalid argument. First argument must be an object. Value: `' + target + '`.' ); 87 } 88 src = new Array( nargs ); 89 for ( i = 0; i < nargs; i++ ) { 90 arg = arguments[ i+1 ]; 91 92 // WARNING: this is a porous check. Buffers, Numbers, Booleans, Strings, Dates, RegExp, custom class instances,... will all pass. 93 if ( !isObject( arg ) ) { 94 throw new TypeError( 'invalid argument. A merge source must be an object. Value: `' + arg + '`.' ); 95 } 96 src[ i ] = arg; 97 } 98 for ( i = 0; i < nargs; i++ ) { 99 deepMerge( target, src[ i ], opts.level, opts.copy, opts.override, opts.extend ); // eslint-disable-line max-len 100 } 101 return target; 102 } 103 } 104 105 106 // EXPORTS // 107 108 module.exports = mergefcn;