diff --git a/lodash.js b/lodash.js index cb7c16b94..f919a2a4e 100644 --- a/lodash.js +++ b/lodash.js @@ -3669,7 +3669,7 @@ lastCalled = 0; return function(key, value) { - var stamp = now ? now() : 0, + var stamp = now(), remaining = HOT_SPAN - (stamp - lastCalled); lastCalled = stamp; @@ -6396,6 +6396,24 @@ /*------------------------------------------------------------------------*/ + /** + * Gets the number of milliseconds that have elapsed since the Unix epoch + * (1 January 1970 00:00:00 UTC). + * + * @static + * @memberOf _ + * @category Date + * @example + * + * _.defer(function(stamp) { console.log(_.now() - stamp); }, _.now()); + * // => logs the number of milliseconds it took for the deferred function to be invoked + */ + var now = nativeNow || function() { + return new Date().getTime(); + }; + + /*------------------------------------------------------------------------*/ + /** * The opposite of `_.before`; this method creates a function that invokes * `func` only after it is called `n` times. @@ -8665,6 +8683,44 @@ : pickByArray(object, baseFlatten(arguments, false, false, 1)); } + /** + * 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. + * + * @static + * @memberOf _ + * @category Object + * @param {Object} object The object to inspect. + * @param {string} key The name of the property to resolve. + * @param {*} [defaultValue] The value returned if the property value + * resolves to `undefined`. + * @returns {*} Returns the resolved value. + * @example + * + * var object = { 'user': 'fred', 'age': _.constant(40) }; + * + * _.result(object, 'user'); + * // => 'fred' + * + * _.result(object, 'age'); + * // => 40 + * + * _.result(object, 'status', 'busy'); + * // => 'busy' + * + * _.result(object, 'status', _.constant('busy')); + * // => 'busy' + */ + function result(object, key, defaultValue) { + var value = object == null ? undefined : object[key]; + if (typeof value == 'undefined') { + value = defaultValue; + } + return isFunction(value) ? value.call(object) : value; + } + /** * An alternative to `_.reduce`; this method transforms `object` to a new * `accumulator` object which is the result of running each of its own @@ -8769,6 +8825,70 @@ /*------------------------------------------------------------------------*/ + /** + * Produces a random number between `min` and `max` (inclusive). If only one + * argument is provided a number between `0` and the given number is returned. + * If `floating` is `true`, or either `min` or `max` are floats, a floating-point + * number is returned instead of an integer. + * + * @static + * @memberOf _ + * @category Number + * @param {number} [min=0] The minimum possible value. + * @param {number} [max=1] The maximum possible value. + * @param {boolean} [floating=false] Specify returning a floating-point number. + * @returns {number} Returns the random number. + * @example + * + * _.random(0, 5); + * // => an integer between 0 and 5 + * + * _.random(5); + * // => also an integer between 0 and 5 + * + * _.random(5, true); + * // => a floating-point number between 0 and 5 + * + * _.random(1.2, 5.2); + * // => a floating-point number between 1.2 and 5.2 + */ + function random(min, max, floating) { + if (floating && isIterateeCall(min, max, floating)) { + max = floating = null; + } + var noMin = min == null, + noMax = max == null; + + if (floating == null) { + if (noMax && typeof min == 'boolean') { + floating = min; + min = 1; + } + else if (typeof max == 'boolean') { + floating = max; + noMax = true; + } + } + if (noMin && noMax) { + max = 1; + noMax = false; + } + min = +min || 0; + if (noMax) { + max = min; + min = 0; + } else { + max = +max || 0; + } + if (floating || min % 1 || max % 1) { + var rand = nativeRandom(); + return nativeMin(min + (rand * (max - min + parseFloat('1e-' + (String(rand).length - 1)))), max); + } + return baseRandom(min, max); + } + + /*------------------------------------------------------------------------*/ + /** * Converts `string` to camel case. * See [Wikipedia](http://en.wikipedia.org/wiki/CamelCase) for more details. @@ -9043,6 +9163,44 @@ return string ? (string + createPad(string, length, chars)) : string; } + /** + * Converts `string` to an integer of the specified radix. If `radix` is + * `undefined` or `0`, a `radix` of `10` is used unless `value` is a hexadecimal, + * in which case a `radix` of `16` is used. + * + * **Note:** This method aligns with the ES5 implementation of `parseInt`. + * See the [ES5 spec](http://es5.github.io/#E) for more details. + * + * @static + * @memberOf _ + * @category String + * @param {string} string The string to parse. + * @param {number} [radix] The radix to interpret `value` by. + * @param- {Object} [guard] Enables use as a callback for functions like `_.map`. + * @returns {number} Returns the converted integer. + * @example + * + * _.parseInt('08'); + * // => 8 + */ + function parseInt(string, radix, guard) { + if (guard && isIterateeCall(string, radix, guard)) { + radix = 0; + } + return nativeParseInt(string, radix); + } + // Fallback for environments with pre-ES5 implementations. + if (nativeParseInt(whitespace + '08') != 8) { + parseInt = function(string, radix, guard) { + // Firefox < 21 and Opera < 15 follow ES3 for `parseInt` and + // Chrome fails to trim leading whitespace characters. + // See https://code.google.com/p/v8/issues/detail?id=3109. + string = trim(string); + radix = (guard && isIterateeCall(string, radix, guard)) ? 0 : +radix; + return nativeParseInt(string, radix || (reHexPrefix.test(string) ? 16 : 10)); + }; + } + /** * Repeats the given string `n` times. * @@ -9864,60 +10022,6 @@ // No operation performed. } - /** - * Gets the number of milliseconds that have elapsed since the Unix epoch - * (1 January 1970 00:00:00 UTC). - * - * @static - * @memberOf _ - * @category Utility - * @example - * - * _.defer(function(stamp) { console.log(_.now() - stamp); }, _.now()); - * // => logs the number of milliseconds it took for the deferred function to be invoked - */ - var now = nativeNow || function() { - return new Date().getTime(); - }; - - /** - * Converts `value` to an integer of the specified radix. If `radix` is - * `undefined` or `0`, a `radix` of `10` is used unless `value` is a hexadecimal, - * in which case a `radix` of `16` is used. - * - * **Note:** This method aligns with the ES5 implementation of `parseInt`. - * See the [ES5 spec](http://es5.github.io/#E) for more details. - * - * @static - * @memberOf _ - * @category Utility - * @param {string} value The value to parse. - * @param {number} [radix] The radix to interpret `value` by. - * @param- {Object} [guard] Enables use as a callback for functions like `_.map`. - * @returns {number} Returns the converted integer. - * @example - * - * _.parseInt('08'); - * // => 8 - */ - function parseInt(value, radix, guard) { - if (guard && isIterateeCall(value, radix, guard)) { - radix = 0; - } - return nativeParseInt(value, radix); - } - // Fallback for environments with pre-ES5 implementations. - if (nativeParseInt(whitespace + '08') != 8) { - parseInt = function(value, radix, guard) { - // Firefox < 21 and Opera < 15 follow ES3 for `parseInt` and - // Chrome fails to trim leading whitespace characters. - // See https://code.google.com/p/v8/issues/detail?id=3109. - value = trim(value); - radix = (guard && isIterateeCall(value, radix, guard)) ? 0 : +radix; - return nativeParseInt(value, radix || (reHexPrefix.test(value) ? 16 : 10)); - }; - } - /** * Creates a "_.pluck" style function which returns the property value * of `key` on a given object. @@ -9974,68 +10078,6 @@ }; } - /** - * Produces a random number between `min` and `max` (inclusive). If only one - * argument is provided a number between `0` and the given number is returned. - * If `floating` is `true`, or either `min` or `max` are floats, a floating-point - * number is returned instead of an integer. - * - * @static - * @memberOf _ - * @category Utility - * @param {number} [min=0] The minimum possible value. - * @param {number} [max=1] The maximum possible value. - * @param {boolean} [floating=false] Specify returning a floating-point number. - * @returns {number} Returns the random number. - * @example - * - * _.random(0, 5); - * // => an integer between 0 and 5 - * - * _.random(5); - * // => also an integer between 0 and 5 - * - * _.random(5, true); - * // => a floating-point number between 0 and 5 - * - * _.random(1.2, 5.2); - * // => a floating-point number between 1.2 and 5.2 - */ - function random(min, max, floating) { - if (floating && isIterateeCall(min, max, floating)) { - max = floating = null; - } - var noMin = min == null, - noMax = max == null; - - if (floating == null) { - if (noMax && typeof min == 'boolean') { - floating = min; - min = 1; - } - else if (typeof max == 'boolean') { - floating = max; - noMax = true; - } - } - if (noMin && noMax) { - max = 1; - noMax = false; - } - min = +min || 0; - if (noMax) { - max = min; - min = 0; - } else { - max = +max || 0; - } - if (floating || min % 1 || max % 1) { - var rand = nativeRandom(); - return nativeMin(min + (rand * (max - min + parseFloat('1e-' + (String(rand).length - 1)))), max); - } - return baseRandom(min, max); - } - /** * Creates an array of numbers (positive and/or negative) progressing from * `start` up to, but not including, `end`. If `start` is less than `end` a @@ -10094,44 +10136,6 @@ return result; } - /** - * 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. - * - * @static - * @memberOf _ - * @category Utility - * @param {Object} object The object to inspect. - * @param {string} key The name of the property to resolve. - * @param {*} [defaultValue] The value returned if the property value - * resolves to `undefined`. - * @returns {*} Returns the resolved value. - * @example - * - * var object = { 'user': 'fred', 'age': _.constant(40) }; - * - * _.result(object, 'user'); - * // => 'fred' - * - * _.result(object, 'age'); - * // => 40 - * - * _.result(object, 'status', 'busy'); - * // => 'busy' - * - * _.result(object, 'status', _.constant('busy')); - * // => 'busy' - */ - function result(object, key, defaultValue) { - var value = object == null ? undefined : object[key]; - if (typeof value == 'undefined') { - value = defaultValue; - } - return isFunction(value) ? value.call(object) : value; - } - /** * Invokes the iteratee function `n` times, returning an array of the results * of each invocation. The `iteratee` is bound to `thisArg` and invoked with diff --git a/test/test.js b/test/test.js index b678997a3..849593c60 100644 --- a/test/test.js +++ b/test/test.js @@ -11875,7 +11875,7 @@ dateCount = 0; var getTime = function() { - return ++dateCount < 3 ? +new Date : Infinity; + return ++dateCount == 5 ? Infinity : +new Date; }; var lodash = _.runInContext(_.assign({}, root, { @@ -12135,7 +12135,7 @@ dateCount = 0; var getTime = function() { - return ++dateCount < 2 ? +new Date : +new Date(2012, 3, 23, 23, 27, 18); + return ++dateCount === 4 ? +new Date(2012, 3, 23, 23, 27, 18) : +new Date; }; var lodash = _.runInContext(_.assign({}, root, {