-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (35 loc) · 847 Bytes
/
index.js
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
/*!
* put-value <https://github.com/tunnckoCore/put-value>
*
* Copyright (c) 2015 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var lazy = require('lazy-cache')(require)
var isObject = lazy('isobject')
var hasOwn = lazy('has-own-deep')
var assign = lazy('assign-deep')
var kindOf = lazy('kind-of')
var set = lazy('set-value')
module.exports = function putValue (obj, key, value) {
if (!isObject()(obj)) {
return {}
}
var type = kindOf()(key)
if (type !== 'string' && type !== 'object') {
return obj
}
if (type === 'object') {
return assign()(obj, key)
}
if (key.indexOf('.') === -1) {
if (obj.hasOwnProperty(key)) {
obj[key] = value
}
return obj
}
if (hasOwn()(obj, key)) {
set()(obj, key, value)
}
return obj
}