mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-02 16:17:50 +00:00
Bump to v3.7.0.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import assignWith from '../internal/assignWith';
|
||||
import baseAssign from '../internal/baseAssign';
|
||||
import createAssigner from '../internal/createAssigner';
|
||||
|
||||
@@ -8,13 +9,17 @@ import createAssigner from '../internal/createAssigner';
|
||||
* The `customizer` is bound to `thisArg` and invoked with five arguments:
|
||||
* (objectValue, sourceValue, key, object, source).
|
||||
*
|
||||
* **Note:** This method mutates `object` and is based on
|
||||
* [`Object.assign`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.assign).
|
||||
*
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias extend
|
||||
* @category Object
|
||||
* @param {Object} object The destination object.
|
||||
* @param {...Object} [sources] The source objects.
|
||||
* @param {Function} [customizer] The function to customize assigning values.
|
||||
* @param {Function} [customizer] The function to customize assigned values.
|
||||
* @param {*} [thisArg] The `this` binding of `customizer`.
|
||||
* @returns {Object} Returns `object`.
|
||||
* @example
|
||||
@@ -24,12 +29,16 @@ import createAssigner from '../internal/createAssigner';
|
||||
*
|
||||
* // using a customizer callback
|
||||
* var defaults = _.partialRight(_.assign, function(value, other) {
|
||||
* return typeof value == 'undefined' ? other : value;
|
||||
* return _.isUndefined(value) ? other : value;
|
||||
* });
|
||||
*
|
||||
* defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' });
|
||||
* // => { 'user': 'barney', 'age': 36 }
|
||||
*/
|
||||
var assign = createAssigner(baseAssign);
|
||||
var assign = createAssigner(function(object, source, customizer) {
|
||||
return customizer
|
||||
? assignWith(object, source, customizer)
|
||||
: baseAssign(object, source);
|
||||
});
|
||||
|
||||
export default assign;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import baseCopy from '../internal/baseCopy';
|
||||
import baseAssign from '../internal/baseAssign';
|
||||
import baseCreate from '../internal/baseCreate';
|
||||
import isIterateeCall from '../internal/isIterateeCall';
|
||||
import keys from './keys';
|
||||
|
||||
/**
|
||||
* Creates an object that inherits from the given `prototype` object. If a
|
||||
@@ -42,7 +41,7 @@ function create(prototype, properties, guard) {
|
||||
if (guard && isIterateeCall(prototype, properties, guard)) {
|
||||
properties = null;
|
||||
}
|
||||
return properties ? baseCopy(properties, result, keys(properties)) : result;
|
||||
return properties ? baseAssign(result, properties) : result;
|
||||
}
|
||||
|
||||
export default create;
|
||||
|
||||
@@ -7,6 +7,8 @@ import restParam from '../function/restParam';
|
||||
* object for all destination properties that resolve to `undefined`. Once a
|
||||
* property is set, additional values of the same property are ignored.
|
||||
*
|
||||
* **Note:** This method mutates `object`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Object
|
||||
|
||||
@@ -4,7 +4,7 @@ import createForIn from '../internal/createForIn';
|
||||
/**
|
||||
* Iterates over own and inherited enumerable properties of an object invoking
|
||||
* `iteratee` for each property. The `iteratee` is bound to `thisArg` and invoked
|
||||
* with three arguments: (value, key, object). Iterator functions may exit
|
||||
* with three arguments: (value, key, object). Iteratee functions may exit
|
||||
* iteration early by explicitly returning `false`.
|
||||
*
|
||||
* @static
|
||||
|
||||
@@ -4,7 +4,7 @@ import createForOwn from '../internal/createForOwn';
|
||||
/**
|
||||
* Iterates over own enumerable properties of an object invoking `iteratee`
|
||||
* for each property. The `iteratee` is bound to `thisArg` and invoked with
|
||||
* three arguments: (value, key, object). Iterator functions may exit iteration
|
||||
* three arguments: (value, key, object). Iteratee functions may exit iteration
|
||||
* early by explicitly returning `false`.
|
||||
*
|
||||
* @static
|
||||
|
||||
33
object/get.js
Normal file
33
object/get.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import baseGet from '../internal/baseGet';
|
||||
import toPath from '../internal/toPath';
|
||||
|
||||
/**
|
||||
* Gets the property value of `path` on `object`. If the resolved value is
|
||||
* `undefined` the `defaultValue` is used in its place.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Object
|
||||
* @param {Object} object The object to query.
|
||||
* @param {Array|string} path The path of the property to get.
|
||||
* @param {*} [defaultValue] The value returned if the resolved value is `undefined`.
|
||||
* @returns {*} Returns the resolved value.
|
||||
* @example
|
||||
*
|
||||
* var object = { 'a': [{ 'b': { 'c': 3 } }] };
|
||||
*
|
||||
* _.get(object, 'a[0].b.c');
|
||||
* // => 3
|
||||
*
|
||||
* _.get(object, ['a', '0', 'b', 'c']);
|
||||
* // => 3
|
||||
*
|
||||
* _.get(object, 'a.b.c', 'default');
|
||||
* // => 'default'
|
||||
*/
|
||||
function get(object, path, defaultValue) {
|
||||
var result = object == null ? undefined : baseGet(object, toPath(path), path + '');
|
||||
return result === undefined ? defaultValue : result;
|
||||
}
|
||||
|
||||
export default get;
|
||||
@@ -1,3 +1,9 @@
|
||||
import baseGet from '../internal/baseGet';
|
||||
import baseSlice from '../internal/baseSlice';
|
||||
import isKey from '../internal/isKey';
|
||||
import last from '../array/last';
|
||||
import toPath from '../internal/toPath';
|
||||
|
||||
/** Used for native method references. */
|
||||
var objectProto = Object.prototype;
|
||||
|
||||
@@ -5,24 +11,39 @@ var objectProto = Object.prototype;
|
||||
var hasOwnProperty = objectProto.hasOwnProperty;
|
||||
|
||||
/**
|
||||
* Checks if `key` exists as a direct property of `object` instead of an
|
||||
* inherited property.
|
||||
* Checks if `path` is a direct property.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Object
|
||||
* @param {Object} object The object to inspect.
|
||||
* @param {string} key The key to check.
|
||||
* @returns {boolean} Returns `true` if `key` is a direct property, else `false`.
|
||||
* @param {Object} object The object to query.
|
||||
* @param {Array|string} path The path to check.
|
||||
* @returns {boolean} Returns `true` if `path` is a direct property, else `false`.
|
||||
* @example
|
||||
*
|
||||
* var object = { 'a': 1, 'b': 2, 'c': 3 };
|
||||
* var object = { 'a': { 'b': { 'c': 3 } } };
|
||||
*
|
||||
* _.has(object, 'b');
|
||||
* _.has(object, 'a');
|
||||
* // => true
|
||||
*
|
||||
* _.has(object, 'a.b.c');
|
||||
* // => true
|
||||
*
|
||||
* _.has(object, ['a', 'b', 'c']);
|
||||
* // => true
|
||||
*/
|
||||
function has(object, key) {
|
||||
return object ? hasOwnProperty.call(object, key) : false;
|
||||
function has(object, path) {
|
||||
if (object == null) {
|
||||
return false;
|
||||
}
|
||||
var result = hasOwnProperty.call(object, path);
|
||||
if (!result && !isKey(path)) {
|
||||
path = toPath(path);
|
||||
object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
|
||||
path = last(path);
|
||||
result = object != null && hasOwnProperty.call(object, path);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default has;
|
||||
|
||||
@@ -16,7 +16,7 @@ var nativeKeys = isNative(nativeKeys = Object.keys) && nativeKeys;
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Object
|
||||
* @param {Object} object The object to inspect.
|
||||
* @param {Object} object The object to query.
|
||||
* @returns {Array} Returns the array of property names.
|
||||
* @example
|
||||
*
|
||||
@@ -39,7 +39,7 @@ var keys = !nativeKeys ? shimKeys : function(object) {
|
||||
length = object.length;
|
||||
}
|
||||
if ((typeof Ctor == 'function' && Ctor.prototype === object) ||
|
||||
(typeof object != 'function' && (length && isLength(length)))) {
|
||||
(typeof object != 'function' && isLength(length))) {
|
||||
return shimKeys(object);
|
||||
}
|
||||
return isObject(object) ? nativeKeys(object) : [];
|
||||
|
||||
@@ -19,7 +19,7 @@ var hasOwnProperty = objectProto.hasOwnProperty;
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Object
|
||||
* @param {Object} object The object to inspect.
|
||||
* @param {Object} object The object to query.
|
||||
* @returns {Array} Returns the array of property names.
|
||||
* @example
|
||||
*
|
||||
|
||||
@@ -15,7 +15,7 @@ import createAssigner from '../internal/createAssigner';
|
||||
* @category Object
|
||||
* @param {Object} object The destination object.
|
||||
* @param {...Object} [sources] The source objects.
|
||||
* @param {Function} [customizer] The function to customize merging properties.
|
||||
* @param {Function} [customizer] The function to customize assigned values.
|
||||
* @param {*} [thisArg] The `this` binding of `customizer`.
|
||||
* @returns {Object} Returns `object`.
|
||||
* @example
|
||||
|
||||
@@ -7,7 +7,7 @@ import keys from './keys';
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Object
|
||||
* @param {Object} object The object to inspect.
|
||||
* @param {Object} object The object to query.
|
||||
* @returns {Array} Returns the new array of key-value pairs.
|
||||
* @example
|
||||
*
|
||||
|
||||
@@ -1,41 +1,49 @@
|
||||
import baseGet from '../internal/baseGet';
|
||||
import baseSlice from '../internal/baseSlice';
|
||||
import isFunction from '../lang/isFunction';
|
||||
import isKey from '../internal/isKey';
|
||||
import last from '../array/last';
|
||||
import toPath from '../internal/toPath';
|
||||
|
||||
/**
|
||||
* Resolves the value of property `key` on `object`. If the value of `key` is
|
||||
* a function it is invoked with the `this` binding of `object` and its result
|
||||
* is returned, else the property value is returned. If the property value is
|
||||
* `undefined` the `defaultValue` is used in its place.
|
||||
* This method is like `_.get` except that if the resolved value is a function
|
||||
* it is invoked with the `this` binding of its parent object and its result
|
||||
* is returned.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Object
|
||||
* @param {Object} object The object to query.
|
||||
* @param {string} key The key of the property to resolve.
|
||||
* @param {*} [defaultValue] The value returned if the property value
|
||||
* resolves to `undefined`.
|
||||
* @param {Array|string} path The path of the property to resolve.
|
||||
* @param {*} [defaultValue] The value returned if the resolved value is `undefined`.
|
||||
* @returns {*} Returns the resolved value.
|
||||
* @example
|
||||
*
|
||||
* var object = { 'user': 'fred', 'age': _.constant(40) };
|
||||
* var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };
|
||||
*
|
||||
* _.result(object, 'user');
|
||||
* // => 'fred'
|
||||
* _.result(object, 'a[0].b.c1');
|
||||
* // => 3
|
||||
*
|
||||
* _.result(object, 'age');
|
||||
* // => 40
|
||||
* _.result(object, 'a[0].b.c2');
|
||||
* // => 4
|
||||
*
|
||||
* _.result(object, 'status', 'busy');
|
||||
* // => 'busy'
|
||||
* _.result(object, 'a.b.c', 'default');
|
||||
* // => 'default'
|
||||
*
|
||||
* _.result(object, 'status', _.constant('busy'));
|
||||
* // => 'busy'
|
||||
* _.result(object, 'a.b.c', _.constant('default'));
|
||||
* // => 'default'
|
||||
*/
|
||||
function result(object, key, defaultValue) {
|
||||
var value = object == null ? undefined : object[key];
|
||||
if (typeof value == 'undefined') {
|
||||
value = defaultValue;
|
||||
function result(object, path, defaultValue) {
|
||||
var result = object == null ? undefined : object[path];
|
||||
if (result === undefined) {
|
||||
if (object != null && !isKey(path, object)) {
|
||||
path = toPath(path);
|
||||
object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
|
||||
result = object == null ? undefined : object[last(path)];
|
||||
}
|
||||
result = result === undefined ? defaultValue : result;
|
||||
}
|
||||
return isFunction(value) ? value.call(object) : value;
|
||||
return isFunction(result) ? result.call(object) : result;
|
||||
}
|
||||
|
||||
export default result;
|
||||
|
||||
55
object/set.js
Normal file
55
object/set.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import isIndex from '../internal/isIndex';
|
||||
import isKey from '../internal/isKey';
|
||||
import isObject from '../lang/isObject';
|
||||
import toPath from '../internal/toPath';
|
||||
|
||||
/**
|
||||
* Sets the property value of `path` on `object`. If a portion of `path`
|
||||
* does not exist it is created.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Object
|
||||
* @param {Object} object The object to augment.
|
||||
* @param {Array|string} path The path of the property to set.
|
||||
* @param {*} value The value to set.
|
||||
* @returns {Object} Returns `object`.
|
||||
* @example
|
||||
*
|
||||
* var object = { 'a': [{ 'b': { 'c': 3 } }] };
|
||||
*
|
||||
* _.set(object, 'a[0].b.c', 4);
|
||||
* console.log(object.a[0].b.c);
|
||||
* // => 4
|
||||
*
|
||||
* _.set(object, 'x[0].y.z', 5);
|
||||
* console.log(object.x[0].y.z);
|
||||
* // => 5
|
||||
*/
|
||||
function set(object, path, value) {
|
||||
if (object == null) {
|
||||
return object;
|
||||
}
|
||||
var pathKey = (path + '');
|
||||
path = (object[pathKey] != null || isKey(path, object)) ? [pathKey] : toPath(path);
|
||||
|
||||
var index = -1,
|
||||
length = path.length,
|
||||
endIndex = length - 1,
|
||||
nested = object;
|
||||
|
||||
while (nested != null && ++index < length) {
|
||||
var key = path[index];
|
||||
if (isObject(nested)) {
|
||||
if (index == endIndex) {
|
||||
nested[key] = value;
|
||||
} else if (nested[key] == null) {
|
||||
nested[key] = isIndex(path[index + 1]) ? [] : {};
|
||||
}
|
||||
}
|
||||
nested = nested[key];
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
export default set;
|
||||
@@ -12,7 +12,7 @@ import isTypedArray from '../lang/isTypedArray';
|
||||
* `accumulator` object which is the result of running each of its own enumerable
|
||||
* properties through `iteratee`, with each invocation potentially mutating
|
||||
* the `accumulator` object. The `iteratee` is bound to `thisArg` and invoked
|
||||
* with four arguments: (accumulator, value, key, object). Iterator functions
|
||||
* with four arguments: (accumulator, value, key, object). Iteratee functions
|
||||
* may exit iteration early by explicitly returning `false`.
|
||||
*
|
||||
* @static
|
||||
|
||||
Reference in New Issue
Block a user