mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 11:27:50 +00:00
Expose _.isObjectLike and _.toPath.
This commit is contained in:
119
lodash.js
119
lodash.js
@@ -1110,17 +1110,6 @@
|
|||||||
return typeof value == 'number' && value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
|
return typeof value == 'number' && value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if `value` is object-like.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {*} value The value to check.
|
|
||||||
* @returns {boolean} Returns `true` if `value` is object-like, else `false`.
|
|
||||||
*/
|
|
||||||
function isObjectLike(value) {
|
|
||||||
return !!value && typeof value == 'object';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used by `trimmedLeftIndex` and `trimmedRightIndex` to determine if a
|
* Used by `trimmedLeftIndex` and `trimmedRightIndex` to determine if a
|
||||||
* character code is whitespace.
|
* character code is whitespace.
|
||||||
@@ -2202,7 +2191,7 @@
|
|||||||
* @returns {*} Returns the resolved value.
|
* @returns {*} Returns the resolved value.
|
||||||
*/
|
*/
|
||||||
function baseGet(object, path) {
|
function baseGet(object, path) {
|
||||||
path = isKey(path, object) ? [path + ''] : toPath(path);
|
path = isKey(path, object) ? [path + ''] : baseToPath(path);
|
||||||
|
|
||||||
var index = 0,
|
var index = 0,
|
||||||
length = path.length;
|
length = path.length;
|
||||||
@@ -2691,7 +2680,7 @@
|
|||||||
splice.call(array, index, 1);
|
splice.call(array, index, 1);
|
||||||
}
|
}
|
||||||
else if (!isKey(index, array)) {
|
else if (!isKey(index, array)) {
|
||||||
var path = toPath(index),
|
var path = baseToPath(index),
|
||||||
object = parent(array, path);
|
object = parent(array, path);
|
||||||
|
|
||||||
if (object != null) {
|
if (object != null) {
|
||||||
@@ -2730,7 +2719,7 @@
|
|||||||
* @returns {Object} Returns `object`.
|
* @returns {Object} Returns `object`.
|
||||||
*/
|
*/
|
||||||
function baseSet(object, path, value, customizer) {
|
function baseSet(object, path, value, customizer) {
|
||||||
path = isKey(path, object) ? [path + ''] : toPath(path);
|
path = isKey(path, object) ? [path + ''] : baseToPath(path);
|
||||||
|
|
||||||
var index = -1,
|
var index = -1,
|
||||||
length = path.length,
|
length = path.length,
|
||||||
@@ -2898,6 +2887,18 @@
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The base implementation of `_.toPath` which only converts `value` to a
|
||||||
|
* path if it's not one.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {*} value The value to process.
|
||||||
|
* @returns {Array} Returns the property path array.
|
||||||
|
*/
|
||||||
|
function baseToPath(value) {
|
||||||
|
return isArray(value) ? value : stringToPath(value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `_.uniq`.
|
* The base implementation of `_.uniq`.
|
||||||
*
|
*
|
||||||
@@ -2970,7 +2971,7 @@
|
|||||||
* @returns {boolean} Returns `true` if the property is deleted, else `false`.
|
* @returns {boolean} Returns `true` if the property is deleted, else `false`.
|
||||||
*/
|
*/
|
||||||
function baseUnset(object, path) {
|
function baseUnset(object, path) {
|
||||||
path = isKey(path, object) ? [path + ''] : toPath(path);
|
path = isKey(path, object) ? [path + ''] : baseToPath(path);
|
||||||
object = parent(object, path);
|
object = parent(object, path);
|
||||||
var key = last(path);
|
var key = last(path);
|
||||||
return (object != null && has(object, key)) ? delete object[key] : true;
|
return (object != null && has(object, key)) ? delete object[key] : true;
|
||||||
@@ -3969,7 +3970,7 @@
|
|||||||
}
|
}
|
||||||
var result = hasFunc(object, path);
|
var result = hasFunc(object, path);
|
||||||
if (!result && !isKey(path)) {
|
if (!result && !isKey(path)) {
|
||||||
path = toPath(path);
|
path = baseToPath(path);
|
||||||
object = parent(object, path);
|
object = parent(object, path);
|
||||||
if (object != null) {
|
if (object != null) {
|
||||||
path = last(path);
|
path = last(path);
|
||||||
@@ -4084,7 +4085,7 @@
|
|||||||
*/
|
*/
|
||||||
function invokePath(object, path, args) {
|
function invokePath(object, path, args) {
|
||||||
if (!isKey(path, object)) {
|
if (!isKey(path, object)) {
|
||||||
path = toPath(path);
|
path = baseToPath(path);
|
||||||
object = parent(object, path);
|
object = parent(object, path);
|
||||||
path = last(path);
|
path = last(path);
|
||||||
}
|
}
|
||||||
@@ -4357,6 +4358,21 @@
|
|||||||
};
|
};
|
||||||
}());
|
}());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts `string` to a property path array.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {string} string The string to process.
|
||||||
|
* @returns {Array} Returns the property path array.
|
||||||
|
*/
|
||||||
|
function stringToPath(string) {
|
||||||
|
var result = [];
|
||||||
|
baseToString(string).replace(rePropName, function(match, number, quote, string) {
|
||||||
|
result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match));
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts `value` to a function if it's not one.
|
* Converts `value` to a function if it's not one.
|
||||||
*
|
*
|
||||||
@@ -4379,24 +4395,6 @@
|
|||||||
return nativeFloor(value) || 0;
|
return nativeFloor(value) || 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts `value` to a property path array if it's not one.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {*} value The value to process.
|
|
||||||
* @returns {Array} Returns the property path array.
|
|
||||||
*/
|
|
||||||
function toPath(value) {
|
|
||||||
if (isArray(value)) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
var result = [];
|
|
||||||
baseToString(value).replace(rePropName, function(match, number, quote, string) {
|
|
||||||
result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match));
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a clone of `wrapper`.
|
* Creates a clone of `wrapper`.
|
||||||
*
|
*
|
||||||
@@ -8435,7 +8433,10 @@
|
|||||||
* _.isObject([1, 2, 3]);
|
* _.isObject([1, 2, 3]);
|
||||||
* // => true
|
* // => true
|
||||||
*
|
*
|
||||||
* _.isObject(1);
|
* _.isObject(_.noop);
|
||||||
|
* // => true
|
||||||
|
*
|
||||||
|
* _.isObject(null);
|
||||||
* // => false
|
* // => false
|
||||||
*/
|
*/
|
||||||
function isObject(value) {
|
function isObject(value) {
|
||||||
@@ -8445,6 +8446,33 @@
|
|||||||
return !!value && (type == 'object' || type == 'function');
|
return !!value && (type == 'object' || type == 'function');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if `value` is object-like. A value is object-like if it's not `null`
|
||||||
|
* and has a `typeof` result of "object".
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @memberOf _
|
||||||
|
* @category Lang
|
||||||
|
* @param {*} value The value to check.
|
||||||
|
* @returns {boolean} Returns `true` if `value` is object-like, else `false`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* _.isObjectLike({});
|
||||||
|
* // => true
|
||||||
|
*
|
||||||
|
* _.isObjectLike([1, 2, 3]);
|
||||||
|
* // => true
|
||||||
|
*
|
||||||
|
* _.isObjectLike(_.noop);
|
||||||
|
* // => false
|
||||||
|
*
|
||||||
|
* _.isObjectLike(null);
|
||||||
|
* // => false
|
||||||
|
*/
|
||||||
|
function isObjectLike(value) {
|
||||||
|
return !!value && typeof value == 'object';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs a deep comparison between `object` and `source` to determine if
|
* Performs a deep comparison between `object` and `source` to determine if
|
||||||
* `object` contains equivalent property values.
|
* `object` contains equivalent property values.
|
||||||
@@ -9759,7 +9787,7 @@
|
|||||||
*/
|
*/
|
||||||
function result(object, path, defaultValue) {
|
function result(object, path, defaultValue) {
|
||||||
if (!isKey(path, object)) {
|
if (!isKey(path, object)) {
|
||||||
path = toPath(path);
|
path = baseToPath(path);
|
||||||
var result = get(object, path);
|
var result = get(object, path);
|
||||||
object = parent(object, path);
|
object = parent(object, path);
|
||||||
} else {
|
} else {
|
||||||
@@ -11392,6 +11420,21 @@
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts `value` to a property path array.
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @memberOf _
|
||||||
|
* @category Utility
|
||||||
|
* @param {*} value The value to process.
|
||||||
|
* @returns {Array} Returns the new property path array.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
function toPath(value) {
|
||||||
|
return isArray(value) ? copyArray(value) : stringToPath(value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a unique ID. If `prefix` is provided the ID is appended to it.
|
* Generates a unique ID. If `prefix` is provided the ID is appended to it.
|
||||||
*
|
*
|
||||||
@@ -11777,6 +11820,7 @@
|
|||||||
lodash.thru = thru;
|
lodash.thru = thru;
|
||||||
lodash.times = times;
|
lodash.times = times;
|
||||||
lodash.toArray = toArray;
|
lodash.toArray = toArray;
|
||||||
|
lodash.toPath = toPath;
|
||||||
lodash.toPlainObject = toPlainObject;
|
lodash.toPlainObject = toPlainObject;
|
||||||
lodash.transform = transform;
|
lodash.transform = transform;
|
||||||
lodash.union = union;
|
lodash.union = union;
|
||||||
@@ -11862,6 +11906,7 @@
|
|||||||
lodash.isNull = isNull;
|
lodash.isNull = isNull;
|
||||||
lodash.isNumber = isNumber;
|
lodash.isNumber = isNumber;
|
||||||
lodash.isObject = isObject;
|
lodash.isObject = isObject;
|
||||||
|
lodash.isObjectLike = isObjectLike;
|
||||||
lodash.isPlainObject = isPlainObject;
|
lodash.isPlainObject = isPlainObject;
|
||||||
lodash.isRegExp = isRegExp;
|
lodash.isRegExp = isRegExp;
|
||||||
lodash.isString = isString;
|
lodash.isString = isString;
|
||||||
|
|||||||
11
test/test.js
11
test/test.js
@@ -8182,11 +8182,11 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should not error on host objects (test in IE)', 17, function() {
|
test('should not error on host objects (test in IE)', 18, function() {
|
||||||
var funcs = [
|
var funcs = [
|
||||||
'isArguments', 'isArray', 'isArrayLike', 'isBoolean', 'isDate',
|
'isArguments', 'isArray', 'isArrayLike', 'isBoolean', 'isDate', 'isElement',
|
||||||
'isElement', 'isError', 'isFinite', 'isFunction', 'isNaN', 'isNil',
|
'isError', 'isFinite', 'isFunction', 'isNaN', 'isNil', 'isNull', 'isNumber',
|
||||||
'isNull', 'isNumber', 'isObject', 'isRegExp', 'isString', 'isUndefined'
|
'isObject', 'isObjectLike', 'isRegExp', 'isString', 'isUndefined'
|
||||||
];
|
];
|
||||||
|
|
||||||
_.each(funcs, function(methodName) {
|
_.each(funcs, function(methodName) {
|
||||||
@@ -17374,6 +17374,7 @@
|
|||||||
'isNull',
|
'isNull',
|
||||||
'isNumber',
|
'isNumber',
|
||||||
'isObject',
|
'isObject',
|
||||||
|
'isObjectLike',
|
||||||
'isPlainObject',
|
'isPlainObject',
|
||||||
'isRegExp',
|
'isRegExp',
|
||||||
'isString',
|
'isString',
|
||||||
@@ -17600,7 +17601,7 @@
|
|||||||
|
|
||||||
var acceptFalsey = _.difference(allMethods, rejectFalsey);
|
var acceptFalsey = _.difference(allMethods, rejectFalsey);
|
||||||
|
|
||||||
test('should accept falsey arguments', 230, function() {
|
test('should accept falsey arguments', 232, function() {
|
||||||
var emptyArrays = _.map(falsey, _.constant([]));
|
var emptyArrays = _.map(falsey, _.constant([]));
|
||||||
|
|
||||||
_.each(acceptFalsey, function(methodName) {
|
_.each(acceptFalsey, function(methodName) {
|
||||||
|
|||||||
Reference in New Issue
Block a user