lodash.result
Advanced tools
Comparing version 3.0.0 to 3.1.0
135
index.js
/** | ||
* lodash 3.0.0 (Custom Build) <https://lodash.com/> | ||
* lodash 3.1.0 (Custom Build) <https://lodash.com/> | ||
* Build: `lodash modern modularize exports="npm" -o ./` | ||
* Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/> | ||
* Based on Underscore.js 1.7.0 <http://underscorejs.org/LICENSE> | ||
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE> | ||
* Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors | ||
* Available under MIT license <https://lodash.com/license> | ||
*/ | ||
var isFunction = require('lodash.isfunction'); | ||
var baseGet = require('lodash._baseget'), | ||
baseSlice = require('lodash._baseslice'), | ||
toPath = require('lodash._topath'), | ||
isArray = require('lodash.isarray'), | ||
isFunction = require('lodash.isfunction'); | ||
/** Used to match property names within property paths. */ | ||
var reIsDeepProp = /\.|\[(?:[^[\]]+|(["'])(?:(?!\1)[^\n\\]|\\.)*?)\1\]/, | ||
reIsPlainProp = /^\w*$/; | ||
/** | ||
* 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. | ||
* Checks if `value` is a property name and not a property path. | ||
* | ||
* @private | ||
* @param {*} value The value to check. | ||
* @param {Object} [object] The object to query keys on. | ||
* @returns {boolean} Returns `true` if `value` is a property name, else `false`. | ||
*/ | ||
function isKey(value, object) { | ||
var type = typeof value; | ||
if ((type == 'string' && reIsPlainProp.test(value)) || type == 'number') { | ||
return true; | ||
} | ||
if (isArray(value)) { | ||
return false; | ||
} | ||
var result = !reIsDeepProp.test(value); | ||
return result || (object != null && value in toObject(object)); | ||
} | ||
/** | ||
* Converts `value` to an object if it is not one. | ||
* | ||
* @private | ||
* @param {*} value The value to process. | ||
* @returns {Object} Returns the object. | ||
*/ | ||
function toObject(value) { | ||
return isObject(value) ? value : Object(value); | ||
} | ||
/** | ||
* Gets the last element of `array`. | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @category Array | ||
* @param {Array} array The array to query. | ||
* @returns {*} Returns the last element of `array`. | ||
* @example | ||
* | ||
* _.last([1, 2, 3]); | ||
* // => 3 | ||
*/ | ||
function last(array) { | ||
var length = array ? array.length : 0; | ||
return length ? array[length - 1] : undefined; | ||
} | ||
/** | ||
* Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`. | ||
* (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`) | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @category Lang | ||
* @param {*} value The value to check. | ||
* @returns {boolean} Returns `true` if `value` is an object, else `false`. | ||
* @example | ||
* | ||
* _.isObject({}); | ||
* // => true | ||
* | ||
* _.isObject([1, 2, 3]); | ||
* // => true | ||
* | ||
* _.isObject(1); | ||
* // => false | ||
*/ | ||
function isObject(value) { | ||
// Avoid a V8 JIT bug in Chrome 19-20. | ||
// See https://code.google.com/p/v8/issues/detail?id=2291 for more details. | ||
var type = typeof value; | ||
return type == 'function' || (!!value && type == 'object'); | ||
} | ||
/** | ||
* 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; | ||
} | ||
module.exports = result; |
Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/> | ||
Based on Underscore.js 1.7.0, copyright 2009-2015 Jeremy Ashkenas, | ||
Based on Underscore.js, copyright 2009-2015 Jeremy Ashkenas, | ||
DocumentCloud and Investigative Reporters & Editors <http://underscorejs.org/> | ||
@@ -4,0 +4,0 @@ |
{ | ||
"name": "lodash.result", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "The modern build of lodash’s `_.result` as a module.", | ||
@@ -20,4 +20,8 @@ "homepage": "https://lodash.com/", | ||
"dependencies": { | ||
"lodash._baseget": "^3.0.0", | ||
"lodash._baseslice": "^3.0.0", | ||
"lodash._topath": "^3.0.0", | ||
"lodash.isarray": "^3.0.0", | ||
"lodash.isfunction": "^3.0.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# lodash.result v3.0.0 | ||
# lodash.result v3.1.0 | ||
@@ -20,2 +20,2 @@ The [modern build](https://github.com/lodash/lodash/wiki/Build-Differences) of [lodash’s](https://lodash.com/) `_.result` exported as a [Node.js](http://nodejs.org/)/[io.js](https://iojs.org/) module. | ||
See the [documentation](https://lodash.com/docs#result) or [package source](https://github.com/lodash/lodash/blob/3.0.0-npm-packages/lodash.result) for more details. | ||
See the [documentation](https://lodash.com/docs#result) or [package source](https://github.com/lodash/lodash/blob/3.1.0-npm-packages/lodash.result) for more details. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6847
129
5
+ Addedlodash._baseget@^3.0.0
+ Addedlodash._baseslice@^3.0.0
+ Addedlodash._topath@^3.0.0
+ Addedlodash.isarray@^3.0.0
+ Addedlodash._baseget@3.7.2(transitive)
+ Addedlodash._baseslice@3.0.3(transitive)
+ Addedlodash._topath@3.8.1(transitive)
+ Addedlodash.isarray@3.0.4(transitive)