aboutsummaryrefslogtreecommitdiff
path: root/node_modules/es5-ext/object/assign-deep.js
blob: 8034a038e24f3e6e6381b75807e74b4fd6b832d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"use strict";

var includes      = require("../array/#/contains")
  , uniq          = require("../array/#/uniq")
  , objForEach    = require("./for-each")
  , isPlainObject = require("./is-plain-object")
  , ensureValue   = require("./valid-value");

var isArray = Array.isArray, slice = Array.prototype.slice;

var deepAssign = function (source, target) {
	if (isPlainObject(source)) {
		if (!isPlainObject(target)) return target;
		objForEach(target, function (value, key) {
			source[key] = deepAssign(source[key], value);
		});
		return source;
	}
	if (isArray(source)) {
		if (!isArray(target)) return target;
		target.forEach(function (item) {
			if (!includes.call(source, item)) source.push(item);
		});
		return source;
	}
	return target;
};

module.exports = function (target /*, ...objects*/) {
	return uniq
		.call([ensureValue(target)].concat(slice.call(arguments, 1).map(ensureValue)))
		.reduce(deepAssign);
};