diff --git a/README.md b/README.md index a4c0a6a10..afa47f7ca 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# lodash-amd v4.16.1 +# lodash-amd v4.16.2 The [Lodash](https://lodash.com/) library exported as [AMD](https://github.com/amdjs/amdjs-api/wiki/AMD) modules. @@ -27,4 +27,4 @@ require({ }); ``` -See the [package source](https://github.com/lodash/lodash/tree/4.16.1-amd) for more details. +See the [package source](https://github.com/lodash/lodash/tree/4.16.2-amd) for more details. diff --git a/_arraySample.js b/_arraySample.js index a299252b6..f371a8dc9 100644 --- a/_arraySample.js +++ b/_arraySample.js @@ -4,8 +4,7 @@ define(['./_baseRandom'], function(baseRandom) { var undefined; /** - * A specialized version of `_.sample` for arrays without support for iteratee - * shorthands. + * A specialized version of `_.sample` for arrays. * * @private * @param {Array} array The array to sample. diff --git a/_arraySampleSize.js b/_arraySampleSize.js index bc4c20f9e..6369db847 100644 --- a/_arraySampleSize.js +++ b/_arraySampleSize.js @@ -1,4 +1,4 @@ -define(['./_arrayShuffle', './_baseClamp'], function(arrayShuffle, baseClamp) { +define(['./_copyArray', './_shuffleSelf'], function(copyArray, shuffleSelf) { /** * A specialized version of `_.sampleSize` for arrays. @@ -9,9 +9,7 @@ define(['./_arrayShuffle', './_baseClamp'], function(arrayShuffle, baseClamp) { * @returns {Array} Returns the random elements. */ function arraySampleSize(array, n) { - var result = arrayShuffle(array); - result.length = baseClamp(n, 0, result.length); - return result; + return shuffleSelf(copyArray(array), n); } return arraySampleSize; diff --git a/_baseCreate.js b/_baseCreate.js index 64687dd98..872dc5ba3 100644 --- a/_baseCreate.js +++ b/_baseCreate.js @@ -1,5 +1,8 @@ define(['./isObject'], function(isObject) { + /** Used as a safe reference for `undefined` in pre-ES5 environments. */ + var undefined; + /** Built-in value references. */ var objectCreate = Object.create; @@ -8,12 +11,24 @@ define(['./isObject'], function(isObject) { * properties to the created object. * * @private - * @param {Object} prototype The object to inherit from. + * @param {Object} proto The object to inherit from. * @returns {Object} Returns the new object. */ - function baseCreate(proto) { - return isObject(proto) ? objectCreate(proto) : {}; - } + var baseCreate = (function() { + function object() {} + return function(proto) { + if (!isObject(proto)) { + return {}; + } + if (objectCreate) { + return objectCreate(proto); + } + object.prototype = prototype; + var result = new object; + object.prototype = undefined; + return result; + }; + }()); return baseCreate; }); diff --git a/_baseDelay.js b/_baseDelay.js index 9344e48ec..507035180 100644 --- a/_baseDelay.js +++ b/_baseDelay.js @@ -3,7 +3,7 @@ define([], function() { /** Used as a safe reference for `undefined` in pre-ES5 environments. */ var undefined; - /** Used as the `TypeError` message for "Functions" methods. */ + /** Error message constants. */ var FUNC_ERROR_TEXT = 'Expected a function'; /** diff --git a/_baseSample.js b/_baseSample.js new file mode 100644 index 000000000..9961f4a5b --- /dev/null +++ b/_baseSample.js @@ -0,0 +1,15 @@ +define(['./_arraySample', './values'], function(arraySample, values) { + + /** + * The base implementation of `_.sample`. + * + * @private + * @param {Array|Object} collection The collection to sample. + * @returns {*} Returns the random element. + */ + function baseSample(collection) { + return arraySample(values(collection)); + } + + return baseSample; +}); diff --git a/_baseSampleSize.js b/_baseSampleSize.js new file mode 100644 index 000000000..a42907674 --- /dev/null +++ b/_baseSampleSize.js @@ -0,0 +1,16 @@ +define(['./_shuffleSelf', './values'], function(shuffleSelf, values) { + + /** + * The base implementation of `_.sampleSize` without param guards. + * + * @private + * @param {Array|Object} collection The collection to sample. + * @param {number} n The number of elements to sample. + * @returns {Array} Returns the random elements. + */ + function baseSampleSize(collection, n) { + return shuffleSelf(values(collection), n); + } + + return baseSampleSize; +}); diff --git a/_baseShuffle.js b/_baseShuffle.js new file mode 100644 index 000000000..6f04d87c3 --- /dev/null +++ b/_baseShuffle.js @@ -0,0 +1,15 @@ +define(['./_shuffleSelf', './values'], function(shuffleSelf, values) { + + /** + * The base implementation of `_.shuffle`. + * + * @private + * @param {Array|Object} collection The collection to shuffle. + * @returns {Array} Returns the new shuffled array. + */ + function baseShuffle(collection) { + return shuffleSelf(values(collection)); + } + + return baseShuffle; +}); diff --git a/_cloneBuffer.js b/_cloneBuffer.js index b14187d19..0be60ad68 100644 --- a/_cloneBuffer.js +++ b/_cloneBuffer.js @@ -1,4 +1,20 @@ -define([], function() { +define(['./_root'], function(root) { + + /** Used as a safe reference for `undefined` in pre-ES5 environments. */ + var undefined; + + /** Detect free variable `exports`. */ + var freeExports = typeof exports == 'object' && exports && !exports.nodeType && exports; + + /** Detect free variable `module`. */ + var freeModule = freeExports && typeof module == 'object' && module && !module.nodeType && module; + + /** Detect the popular CommonJS extension `module.exports`. */ + var moduleExports = freeModule && freeModule.exports === freeExports; + + /** Built-in value references. */ + var Buffer = moduleExports ? root.Buffer : undefined, + allocUnsafe = Buffer ? Buffer.allocUnsafe : undefined; /** * Creates a clone of `buffer`. @@ -12,7 +28,9 @@ define([], function() { if (isDeep) { return buffer.slice(); } - var result = new buffer.constructor(buffer.length); + var length = buffer.length, + result = allocUnsafe ? allocUnsafe(length) : new buffer.constructor(length); + buffer.copy(result); return result; } diff --git a/_createFlow.js b/_createFlow.js index 1a2ac9cb0..8ff1b6be7 100644 --- a/_createFlow.js +++ b/_createFlow.js @@ -6,7 +6,7 @@ define(['./_LodashWrapper', './_flatRest', './_getData', './_getFuncName', './is /** Used as the size to enable large array optimizations. */ var LARGE_ARRAY_SIZE = 200; - /** Used as the `TypeError` message for "Functions" methods. */ + /** Error message constants. */ var FUNC_ERROR_TEXT = 'Expected a function'; /** Used to compose bitmasks for function metadata. */ diff --git a/_createWrap.js b/_createWrap.js index 6c5fb128c..4282340b5 100644 --- a/_createWrap.js +++ b/_createWrap.js @@ -3,7 +3,7 @@ define(['./_baseSetData', './_createBind', './_createCurry', './_createHybrid', /** Used as a safe reference for `undefined` in pre-ES5 environments. */ var undefined; - /** Used as the `TypeError` message for "Functions" methods. */ + /** Error message constants. */ var FUNC_ERROR_TEXT = 'Expected a function'; /** Used to compose bitmasks for function metadata. */ diff --git a/_shuffleSelf.js b/_shuffleSelf.js index 7bcc41317..bc9eee0a2 100644 --- a/_shuffleSelf.js +++ b/_shuffleSelf.js @@ -1,24 +1,30 @@ -define(['./_baseRandom'], function(baseRandom) { +define(['./_baseClamp', './_baseRandom'], function(baseClamp, baseRandom) { + + /** Used as a safe reference for `undefined` in pre-ES5 environments. */ + var undefined; /** - * A specialized version of `arrayShuffle` which mutates `array`. + * A specialized version of `_.shuffle` which mutates and sets the size of `array`. * * @private * @param {Array} array The array to shuffle. + * @param {number} [size=array.length] The size of `array`. * @returns {Array} Returns `array`. */ - function shuffleSelf(array) { + function shuffleSelf(array, size) { var index = -1, length = array.length, lastIndex = length - 1; - while (++index < length) { + size = size === undefined ? length : baseClamp(size, 0, length); + while (++index < size) { var rand = baseRandom(index, lastIndex), value = array[rand]; array[rand] = array[index]; array[index] = value; } + array.length = size; return array; } diff --git a/after.js b/after.js index 1a0ce6bf6..8804682dc 100644 --- a/after.js +++ b/after.js @@ -1,6 +1,6 @@ define(['./toInteger'], function(toInteger) { - /** Used as the `TypeError` message for "Functions" methods. */ + /** Error message constants. */ var FUNC_ERROR_TEXT = 'Expected a function'; /** diff --git a/before.js b/before.js index c72884d5f..c6e3561b9 100644 --- a/before.js +++ b/before.js @@ -3,7 +3,7 @@ define(['./toInteger'], function(toInteger) { /** Used as a safe reference for `undefined` in pre-ES5 environments. */ var undefined; - /** Used as the `TypeError` message for "Functions" methods. */ + /** Error message constants. */ var FUNC_ERROR_TEXT = 'Expected a function'; /** diff --git a/cond.js b/cond.js index 6b3bee705..5a59da2ff 100644 --- a/cond.js +++ b/cond.js @@ -1,6 +1,6 @@ define(['./_apply', './_arrayMap', './_baseIteratee', './_baseRest'], function(apply, arrayMap, baseIteratee, baseRest) { - /** Used as the `TypeError` message for "Functions" methods. */ + /** Error message constants. */ var FUNC_ERROR_TEXT = 'Expected a function'; /** diff --git a/debounce.js b/debounce.js index 2973d9779..07a0a7db9 100644 --- a/debounce.js +++ b/debounce.js @@ -3,7 +3,7 @@ define(['./isObject', './now', './toNumber'], function(isObject, now, toNumber) /** Used as a safe reference for `undefined` in pre-ES5 environments. */ var undefined; - /** Used as the `TypeError` message for "Functions" methods. */ + /** Error message constants. */ var FUNC_ERROR_TEXT = 'Expected a function'; /* Built-in method references for those with the same name as other `lodash` methods. */ diff --git a/isNative.js b/isNative.js index f66f5f30d..0befcc831 100644 --- a/isNative.js +++ b/isNative.js @@ -1,5 +1,8 @@ define(['./_baseIsNative', './_isMaskable'], function(baseIsNative, isMaskable) { + /** Error message constants. */ + var CORE_ERROR_TEXT = 'Unsupported core-js use. Try https://github.com/es-shims.'; + /** * Checks if `value` is a pristine native function. * @@ -28,7 +31,7 @@ define(['./_baseIsNative', './_isMaskable'], function(baseIsNative, isMaskable) */ function isNative(value) { if (isMaskable(value)) { - throw new Error('This method is not supported with core-js. Try https://github.com/es-shims.'); + throw new Error(CORE_ERROR_TEXT); } return baseIsNative(value); } diff --git a/main.js b/main.js index d0d136edd..592b9f537 100644 --- a/main.js +++ b/main.js @@ -13,13 +13,14 @@ var undefined; /** Used as the semantic version number. */ - var VERSION = '4.16.1'; + var VERSION = '4.16.2'; /** Used as the size to enable large array optimizations. */ var LARGE_ARRAY_SIZE = 200; - /** Used as the `TypeError` message for "Functions" methods. */ - var FUNC_ERROR_TEXT = 'Expected a function'; + /** Error message constants. */ + var CORE_ERROR_TEXT = 'Unsupported core-js use. Try https://github.com/es-shims.', + FUNC_ERROR_TEXT = 'Expected a function'; /** Used to stand-in for `undefined` hash values. */ var HASH_UNDEFINED = '__lodash_hash_undefined__'; @@ -1468,6 +1469,7 @@ var Buffer = moduleExports ? context.Buffer : undefined, Symbol = context.Symbol, Uint8Array = context.Uint8Array, + allocUnsafe = Buffer ? Buffer.allocUnsafe : undefined, defineProperty = Object.defineProperty, getPrototype = overArg(Object.getPrototypeOf, Object), iteratorSymbol = Symbol ? Symbol.iterator : undefined, @@ -1654,6 +1656,30 @@ return new LodashWrapper(value); } + /** + * The base implementation of `_.create` without support for assigning + * properties to the created object. + * + * @private + * @param {Object} proto The object to inherit from. + * @returns {Object} Returns the new object. + */ + var baseCreate = (function() { + function object() {} + return function(proto) { + if (!isObject(proto)) { + return {}; + } + if (objectCreate) { + return objectCreate(proto); + } + object.prototype = prototype; + var result = new object; + object.prototype = undefined; + return result; + }; + }()); + /** * The function whose prototype chain sequence wrappers inherit from. * @@ -2374,8 +2400,7 @@ } /** - * A specialized version of `_.sample` for arrays without support for iteratee - * shorthands. + * A specialized version of `_.sample` for arrays. * * @private * @param {Array} array The array to sample. @@ -2395,9 +2420,7 @@ * @returns {Array} Returns the random elements. */ function arraySampleSize(array, n) { - var result = arrayShuffle(array); - result.length = baseClamp(n, 0, result.length); - return result; + return shuffleSelf(copyArray(array), n); } /** @@ -2687,18 +2710,6 @@ return true; } - /** - * The base implementation of `_.create` without support for assigning - * properties to the created object. - * - * @private - * @param {Object} prototype The object to inherit from. - * @returns {Object} Returns the new object. - */ - function baseCreate(proto) { - return isObject(proto) ? objectCreate(proto) : {}; - } - /** * The base implementation of `_.delay` and `_.defer` which accepts `args` * to provide to `func`. @@ -3890,6 +3901,29 @@ return setToString(overRest(func, start, identity), func + ''); } + /** + * The base implementation of `_.sample`. + * + * @private + * @param {Array|Object} collection The collection to sample. + * @returns {*} Returns the random element. + */ + function baseSample(collection) { + return arraySample(values(collection)); + } + + /** + * The base implementation of `_.sampleSize` without param guards. + * + * @private + * @param {Array|Object} collection The collection to sample. + * @param {number} n The number of elements to sample. + * @returns {Array} Returns the random elements. + */ + function baseSampleSize(collection, n) { + return shuffleSelf(values(collection), n); + } + /** * The base implementation of `_.set`. * @@ -3960,6 +3994,17 @@ }); }; + /** + * The base implementation of `_.shuffle`. + * + * @private + * @param {Array|Object} collection The collection to shuffle. + * @returns {Array} Returns the new shuffled array. + */ + function baseShuffle(collection) { + return shuffleSelf(values(collection)); + } + /** * The base implementation of `_.slice` without an iteratee call guard. * @@ -4422,7 +4467,9 @@ if (isDeep) { return buffer.slice(); } - var result = new buffer.constructor(buffer.length); + var length = buffer.length, + result = allocUnsafe ? allocUnsafe(length) : new buffer.constructor(length); + buffer.copy(result); return result; } @@ -6534,24 +6581,27 @@ } /** - * A specialized version of `arrayShuffle` which mutates `array`. + * A specialized version of `_.shuffle` which mutates and sets the size of `array`. * * @private * @param {Array} array The array to shuffle. + * @param {number} [size=array.length] The size of `array`. * @returns {Array} Returns `array`. */ - function shuffleSelf(array) { + function shuffleSelf(array, size) { var index = -1, length = array.length, lastIndex = length - 1; - while (++index < length) { + size = size === undefined ? length : baseClamp(size, 0, length); + while (++index < size) { var rand = baseRandom(index, lastIndex), value = array[rand]; array[rand] = array[index]; array[index] = value; } + array.length = size; return array; } @@ -9629,7 +9679,8 @@ * // => 2 */ function sample(collection) { - return arraySample(isArrayLike(collection) ? collection : values(collection)); + var func = isArray(collection) ? arraySample : baseSample; + return func(collection); } /** @@ -9658,7 +9709,8 @@ } else { n = toInteger(n); } - return arraySampleSize(isArrayLike(collection) ? collection : values(collection), n); + var func = isArray(collection) ? arraySampleSize : baseSampleSize; + return func(collection, n); } /** @@ -9677,10 +9729,8 @@ * // => [4, 1, 3, 2] */ function shuffle(collection) { - return shuffleSelf(isArrayLike(collection) - ? copyArray(collection) - : values(collection) - ); + var func = isArray(collection) ? arrayShuffle : baseShuffle; + return func(collection); } /** @@ -11779,7 +11829,7 @@ */ function isNative(value) { if (isMaskable(value)) { - throw new Error('This method is not supported with core-js. Try https://github.com/es-shims.'); + throw new Error(CORE_ERROR_TEXT); } return baseIsNative(value); } @@ -14272,7 +14322,7 @@ } else if (radix) { radix = +radix; } - return nativeParseInt(toString(string), radix || 0); + return nativeParseInt(toString(string).replace(reTrimStart, ''), radix || 0); } /** diff --git a/memoize.js b/memoize.js index 81406ddae..bd2c0cbe8 100644 --- a/memoize.js +++ b/memoize.js @@ -1,6 +1,6 @@ define(['./_MapCache'], function(MapCache) { - /** Used as the `TypeError` message for "Functions" methods. */ + /** Error message constants. */ var FUNC_ERROR_TEXT = 'Expected a function'; /** diff --git a/negate.js b/negate.js index 53a6c94e7..8b4a53781 100644 --- a/negate.js +++ b/negate.js @@ -1,6 +1,6 @@ define([], function() { - /** Used as the `TypeError` message for "Functions" methods. */ + /** Error message constants. */ var FUNC_ERROR_TEXT = 'Expected a function'; /** diff --git a/package.json b/package.json index dad352364..13ef05820 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lodash-amd", - "version": "4.16.1", + "version": "4.16.2", "description": "Lodash exported as AMD modules.", "keywords": "amd, modules, stdlib, util", "homepage": "https://lodash.com/custom-builds", diff --git a/parseInt.js b/parseInt.js index a014efec8..243224c0a 100644 --- a/parseInt.js +++ b/parseInt.js @@ -1,5 +1,8 @@ define(['./_root', './toString'], function(root, toString) { + /** Used to match leading and trailing whitespace. */ + var reTrimStart = /^\s+/; + /* Built-in method references for those with the same name as other `lodash` methods. */ var nativeParseInt = root.parseInt; @@ -33,7 +36,7 @@ define(['./_root', './toString'], function(root, toString) { } else if (radix) { radix = +radix; } - return nativeParseInt(toString(string), radix || 0); + return nativeParseInt(toString(string).replace(reTrimStart, ''), radix || 0); } return parseInt; diff --git a/rest.js b/rest.js index c910372b2..979c3ff7a 100644 --- a/rest.js +++ b/rest.js @@ -3,7 +3,7 @@ define(['./_baseRest', './toInteger'], function(baseRest, toInteger) { /** Used as a safe reference for `undefined` in pre-ES5 environments. */ var undefined; - /** Used as the `TypeError` message for "Functions" methods. */ + /** Error message constants. */ var FUNC_ERROR_TEXT = 'Expected a function'; /** diff --git a/sample.js b/sample.js index 3bfe87b9e..b33133785 100644 --- a/sample.js +++ b/sample.js @@ -1,4 +1,4 @@ -define(['./_arraySample', './isArrayLike', './values'], function(arraySample, isArrayLike, values) { +define(['./_arraySample', './_baseSample', './isArray'], function(arraySample, baseSample, isArray) { /** * Gets a random element from `collection`. @@ -15,7 +15,8 @@ define(['./_arraySample', './isArrayLike', './values'], function(arraySample, is * // => 2 */ function sample(collection) { - return arraySample(isArrayLike(collection) ? collection : values(collection)); + var func = isArray(collection) ? arraySample : baseSample; + return func(collection); } return sample; diff --git a/sampleSize.js b/sampleSize.js index 6e8157db6..3e0372995 100644 --- a/sampleSize.js +++ b/sampleSize.js @@ -1,4 +1,4 @@ -define(['./_arraySampleSize', './isArrayLike', './_isIterateeCall', './toInteger', './values'], function(arraySampleSize, isArrayLike, isIterateeCall, toInteger, values) { +define(['./_arraySampleSize', './_baseSampleSize', './isArray', './_isIterateeCall', './toInteger'], function(arraySampleSize, baseSampleSize, isArray, isIterateeCall, toInteger) { /** Used as a safe reference for `undefined` in pre-ES5 environments. */ var undefined; @@ -29,7 +29,8 @@ define(['./_arraySampleSize', './isArrayLike', './_isIterateeCall', './toInteger } else { n = toInteger(n); } - return arraySampleSize(isArrayLike(collection) ? collection : values(collection), n); + var func = isArray(collection) ? arraySampleSize : baseSampleSize; + return func(collection, n); } return sampleSize; diff --git a/shuffle.js b/shuffle.js index 5035e189e..99ac3acb9 100644 --- a/shuffle.js +++ b/shuffle.js @@ -1,4 +1,4 @@ -define(['./_copyArray', './isArrayLike', './_shuffleSelf', './values'], function(copyArray, isArrayLike, shuffleSelf, values) { +define(['./_arrayShuffle', './_baseShuffle', './isArray'], function(arrayShuffle, baseShuffle, isArray) { /** * Creates an array of shuffled values, using a version of the @@ -16,10 +16,8 @@ define(['./_copyArray', './isArrayLike', './_shuffleSelf', './values'], function * // => [4, 1, 3, 2] */ function shuffle(collection) { - return shuffleSelf(isArrayLike(collection) - ? copyArray(collection) - : values(collection) - ); + var func = isArray(collection) ? arrayShuffle : baseShuffle; + return func(collection); } return shuffle; diff --git a/spread.js b/spread.js index a2f2aa758..d4e6b8c72 100644 --- a/spread.js +++ b/spread.js @@ -3,7 +3,7 @@ define(['./_apply', './_arrayPush', './_baseRest', './_castSlice', './toInteger' /** Used as a safe reference for `undefined` in pre-ES5 environments. */ var undefined; - /** Used as the `TypeError` message for "Functions" methods. */ + /** Error message constants. */ var FUNC_ERROR_TEXT = 'Expected a function'; /* Built-in method references for those with the same name as other `lodash` methods. */ diff --git a/throttle.js b/throttle.js index 2697a1177..45bf343be 100644 --- a/throttle.js +++ b/throttle.js @@ -1,6 +1,6 @@ define(['./debounce', './isObject'], function(debounce, isObject) { - /** Used as the `TypeError` message for "Functions" methods. */ + /** Error message constants. */ var FUNC_ERROR_TEXT = 'Expected a function'; /**