2015-05-20 07:06:59 +08:00
|
|
|
(function() {
|
2017-09-27 02:33:08 +08:00
|
|
|
var isPlainObject, plus, splitKeyPath;
|
2015-05-20 07:06:59 +08:00
|
|
|
|
|
|
|
splitKeyPath = function(keyPath) {
|
|
|
|
var char, i, keyPathArray, startIndex, _i, _len;
|
|
|
|
startIndex = 0;
|
|
|
|
keyPathArray = [];
|
|
|
|
if (keyPath == null) {
|
|
|
|
return keyPathArray;
|
|
|
|
}
|
|
|
|
for (i = _i = 0, _len = keyPath.length; _i < _len; i = ++_i) {
|
|
|
|
char = keyPath[i];
|
|
|
|
if (char === '.' && (i === 0 || keyPath[i - 1] !== '\\')) {
|
|
|
|
keyPathArray.push(keyPath.substring(startIndex, i));
|
|
|
|
startIndex = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
keyPathArray.push(keyPath.substr(startIndex, keyPath.length));
|
|
|
|
return keyPathArray;
|
|
|
|
};
|
|
|
|
isPlainObject = function(value) {
|
|
|
|
return _.isObject(value) && !_.isArray(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
plus = {
|
|
|
|
remove: function(array, element) {
|
|
|
|
var index;
|
|
|
|
index = array.indexOf(element);
|
|
|
|
if (index >= 0) {
|
|
|
|
array.splice(index, 1);
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
},
|
|
|
|
deepClone: function(object) {
|
|
|
|
if (_.isArray(object)) {
|
|
|
|
return object.map(function(value) {
|
|
|
|
return plus.deepClone(value);
|
|
|
|
});
|
|
|
|
} else if (_.isObject(object) && !_.isFunction(object)) {
|
2017-09-27 02:33:08 +08:00
|
|
|
return _.mapObject(object, function(value) {
|
2016-12-15 10:35:02 +08:00
|
|
|
return plus.deepClone(value);
|
2017-09-27 02:33:08 +08:00
|
|
|
});
|
2015-05-20 07:06:59 +08:00
|
|
|
} else {
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
deepExtend: function(target) {
|
|
|
|
var i, key, object, result, _i, _len, _ref;
|
|
|
|
result = target;
|
|
|
|
i = 0;
|
|
|
|
while (++i < arguments.length) {
|
|
|
|
object = arguments[i];
|
|
|
|
if (isPlainObject(result) && isPlainObject(object)) {
|
|
|
|
_ref = Object.keys(object);
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
key = _ref[_i];
|
|
|
|
result[key] = plus.deepExtend(result[key], object[key]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = plus.deepClone(object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
valueForKeyPath: function(object, keyPath) {
|
|
|
|
var key, keys, _i, _len;
|
|
|
|
keys = splitKeyPath(keyPath);
|
|
|
|
for (_i = 0, _len = keys.length; _i < _len; _i++) {
|
|
|
|
key = keys[_i];
|
|
|
|
object = object[key];
|
|
|
|
if (object == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
},
|
|
|
|
setValueForKeyPath: function(object, keyPath, value) {
|
|
|
|
var key, keys;
|
|
|
|
keys = splitKeyPath(keyPath);
|
|
|
|
while (keys.length > 1) {
|
|
|
|
key = keys.shift();
|
|
|
|
if (object[key] == null) {
|
|
|
|
object[key] = {};
|
|
|
|
}
|
|
|
|
object = object[key];
|
|
|
|
}
|
|
|
|
if (value != null) {
|
2017-09-27 02:33:08 +08:00
|
|
|
return (object[keys.shift()] = value);
|
2015-05-20 07:06:59 +08:00
|
|
|
} else {
|
|
|
|
return delete object[keys.shift()];
|
|
|
|
}
|
2017-09-27 02:33:08 +08:00
|
|
|
},
|
|
|
|
};
|
2015-05-20 07:06:59 +08:00
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
module.exports = plus;
|
|
|
|
}.call(this));
|