-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
150 lines (134 loc) · 3.56 KB
/
Copy pathindex.js
File metadata and controls
150 lines (134 loc) · 3.56 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
/*
* Module dependencies.
*/
var drop = require('@ndhoule/drop');
var rest = require('@ndhoule/rest');
var has = Object.prototype.hasOwnProperty;
var objToString = Object.prototype.toString;
/**
* Returns `true` if a value is an object, otherwise `false`.
*
* @name isObject
* @api private
* @param {*} val The value to test.
* @return {boolean}
*/
// TODO: Move to a library
var isObject = function isObject(value) {
return Boolean(value) && typeof value === 'object';
};
/**
* Returns `true` if a value is a plain object, otherwise `false`.
*
* @name isPlainObject
* @api private
* @param {*} val The value to test.
* @return {boolean}
*/
// TODO: Move to a library
var isPlainObject = function isPlainObject(value) {
return Boolean(value) && objToString.call(value) === '[object Object]';
};
/**
* Assigns a key-value pair to a target object when the value assigned is owned,
* and where target[key] is undefined.
*
* @name shallowCombiner
* @api private
* @param {Object} target
* @param {Object} source
* @param {*} value
* @param {string} key
*/
var shallowCombiner = function shallowCombiner(target, source, value, key) {
if (has.call(source, key) && target[key] === undefined) {
target[key] = value;
}
return source;
};
/**
* Assigns a key-value pair to a target object when the value assigned is owned,
* and where target[key] is undefined; also merges objects recursively.
*
* @name deepCombiner
* @api private
* @param {Object} target
* @param {Object} source
* @param {*} value
* @param {string} key
* @return {Object}
*/
var deepCombiner = function(target, source, value, key) {
if (has.call(source, key)) {
if (isPlainObject(target[key]) && isPlainObject(value)) {
target[key] = defaultsDeep(target[key], value);
} else if (target[key] === undefined) {
target[key] = value;
}
}
return source;
};
/**
* TODO: Document
*
* @name defaultsWith
* @api private
* @param {Function} combiner
* @param {Object} target
* @param {...Object} sources
* @return {Object} Return the input `target`.
*/
var defaultsWith = function(combiner, target /*, ...sources */) {
if (!isObject(target)) {
return target;
}
combiner = combiner || shallowCombiner;
var sources = drop(2, arguments);
for (var i = 0; i < sources.length; i += 1) {
for (var key in sources[i]) {
combiner(target, sources[i], sources[i][key], key);
}
}
return target;
};
/**
* Copies owned, enumerable properties from a source object(s) to a target
* object when the value of that property on the source object is `undefined`.
* Recurses on objects.
*
* @name defaultsDeep
* @api public
* @param {Object} target
* @param {...Object} sources
* @return {Object} The input `target`.
*/
var defaultsDeep = function defaultsDeep(target /*, sources */) {
// TODO: Replace with `partial` call?
return defaultsWith.apply(null, [deepCombiner, target].concat(rest(arguments)));
};
/**
* Copies owned, enumerable properties from a source object(s) to a target
* object when the value of that property on the source object is `undefined`.
*
* @name defaults
* @api public
* @param {Object} target
* @param {...Object} sources
* @return {Object}
* @example
* var a = { a: 1 };
* var b = { a: 2, b: 2 };
*
* defaults(a, b);
* console.log(a); //=> { a: 1, b: 2 }
*/
var defaults = function(target /*, ...sources */) {
// TODO: Replace with `partial` call?
return defaultsWith.apply(null, [null, target].concat(rest(arguments)));
};
/*
* Exports.
*/
module.exports = defaults;
module.exports.deep = defaultsDeep;