Ensure "Functions" methods throw if not provided a function and remove Adobe JS engine fix from compareAscending for modern builds.

Former-commit-id: 85f2c3579c33440d3f7351f5a0dfb1c09cd0cc7a
This commit is contained in:
John-David Dalton
2013-08-08 23:11:13 -07:00
parent 25c91b398a
commit 8fb9557520
9 changed files with 787 additions and 524 deletions

View File

@@ -124,7 +124,11 @@
return -1;
}
}
return ai < bi ? -1 : 1;
// The JS engine embedded in Adobe applications like InDesign has a buggy
// `Array#sort` implementation that causes it, under certain circumstances,
// to return the same value for `a` and `b`.
// See https://github.com/jashkenas/underscore/pull/1247
return ai < bi ? -1 : (ai > bi ? 1 : 0);
}
/**
@@ -176,8 +180,7 @@
floor = Math.floor,
hasOwnProperty = objectProto.hasOwnProperty,
push = arrayRef.push,
toString = objectProto.toString,
unshift = arrayRef.unshift;
toString = objectProto.toString;
/* Native method shortcuts for methods with the same name as other `lodash` methods */
var nativeBind = reNative.test(nativeBind = toString.bind) && nativeBind,
@@ -590,60 +593,59 @@
}
/**
* Creates a function that, when called, invokes `func` with the `this` binding
* of `thisArg` and prepends any `partialArgs` to the arguments provided to the
* bound function.
* Creates a function that, when called, either curries or invokes `func`
* with an optional `this` binding and partially applied arguments.
*
* @private
* @param {Function|String} func The function to bind or the method name.
* @param {Mixed} thisArg The `this` binding of `func`.
* @param {Array} partialArgs An array of arguments to be prepended to those provided to the new function.
* @param {Array} partialRightArgs An array of arguments to be appended to those provided to the new function.
* @param {Boolean} [isPartial=false] A flag to indicate performing only partial application.
* @param {Boolean} [isAlt=false] A flag to indicate `_.bindKey` or `_.partialRight` behavior.
* @param {Function|String} func The function or method name to reference.
* @param {Number} bitmask The bitmask of method flags to compose.
* The bitmask may be composed of the following flags:
* 1 - `_.bind`
* 2 - `_.bindKey`
* 4 - `_.curry`
* 8 - `_.partial`
* 16 - `_.partialRight`
* @param {Array} [partialArgs] An array of arguments to prepend to those
* provided to the new function.
* @param {Array} [partialRightArgs] An array of arguments to append to those
* provided to the new function.
* @param {Mixed} [thisArg] The `this` binding of `func`.
* @param {Number} [arity] The arity of `func`.
* @returns {Function} Returns the new bound function.
*/
function createBound(func, thisArg, partialArgs, partialRightArgs, isPartial, isAlt) {
var isBindKey = isAlt && !isPartial,
isFunc = isFunction(func);
function createBound(func, bitmask, partialArgs, partialRightArgs, thisArg, arity) {
var isBind = bitmask & 1,
isBindKey = bitmask & 2,
isCurry = bitmask & 4,
isPartialRight = bitmask & 16;
// throw if `func` is not a function when not behaving as `_.bindKey`
if (!isFunc && !isBindKey) {
if (!isBindKey && !isFunction(func)) {
throw new TypeError;
}
var bindData = func && func.__bindData__;
if (bindData) {
if (isBind && !(bindData[1] & 1)) {
bindData[4] = thisArg;
}
if (isCurry && !(bindData[1] & 4)) {
bindData[5] = arity;
}
if (partialArgs) {
push.apply(bindData[2] || (bindData[2] = []), partialArgs);
}
if (partialRightArgs) {
push.apply(bindData[3] || (bindData[3] = []), partialRightArgs);
}
bindData[1] |= bitmask;
return createBound.apply(null, bindData);
}
// use `Function#bind` if it exists and is fast
// (in V8 `Function#bind` is slower except when partially applied)
if (!isPartial && !isAlt && !partialRightArgs.length && (support.fastBind || (nativeBind && partialArgs.length))) {
var args = [func, thisArg];
push.apply(args, partialArgs);
var bound = nativeBind.call.apply(nativeBind, args);
}
else {
bound = function() {
// `Function#bind` spec
// http://es5.github.io/#x15.3.4.5
var args = arguments,
thisBinding = isPartial ? this : thisArg;
if (isBindKey) {
func = thisArg[key];
}
if (partialArgs.length || partialRightArgs.length) {
unshift.apply(args, partialArgs);
push.apply(args, partialRightArgs);
}
if (this instanceof bound) {
// ensure `new bound` is an instance of `func`
thisBinding = createObject(func.prototype);
// mimic the constructor's `return` behavior
// http://es5.github.io/#x13.2.2
var result = func.apply(thisBinding, args);
return isObject(result) ? result : thisBinding;
}
return func.apply(thisBinding, args);
};
if (isBind && !(isBindKey || isCurry || isPartialRight) &&
(support.fastBind || (nativeBind && partialArgs.length))) {;
}
// take a snapshot of `arguments` before juggling
bindData = nativeSlice.call(arguments);
if (isBindKey) {
var key = thisArg;
thisArg = func;
@@ -3063,7 +3065,7 @@
*/
function range(start, end, step) {
start = +start || 0;
step = +step || 1;
step = (+step || 1);
if (end == null) {
end = start;
@@ -3398,6 +3400,9 @@
* // `renderNotes` is run once, after all notes have saved
*/
function after(n, func) {
if (!isFunction(func)) {
throw new TypeError;
}
return function() {
if (--n < 1) {
return func.apply(this, arguments);
@@ -3428,7 +3433,7 @@
* // => 'hi moe'
*/
function bind(func, thisArg) {
return createBound(func, thisArg, nativeSlice.call(arguments, 2), []);
return createBound(func, 9, nativeSlice.call(arguments, 2), null, thisArg);
}
/**
@@ -3498,7 +3503,14 @@
* // => 'Hiya Jerome!'
*/
function compose() {
var funcs = arguments;
var funcs = arguments,
length = funcs.length || 1;
while (length--) {
if (!isFunction(funcs[length])) {
throw new TypeError;
}
}
return function() {
var args = arguments,
length = funcs.length;
@@ -3616,32 +3628,9 @@
timeoutId = null,
trailing = true;
function clear() {
clearTimeout(maxTimeoutId);
clearTimeout(timeoutId);
callCount = 0;
maxTimeoutId = timeoutId = null;
if (!isFunction(func)) {
throw new TypeError;
}
function delayed() {
var isCalled = trailing && (!leading || callCount > 1);
clear();
if (isCalled) {
if (maxWait !== false) {
lastCalled = new Date;
}
result = func.apply(thisArg, args);
}
}
function maxDelayed() {
clear();
if (trailing || (maxWait !== wait)) {
lastCalled = new Date;
result = func.apply(thisArg, args);
}
}
wait = nativeMax(0, wait || 0);
if (options === true) {
var leading = true;
@@ -3651,6 +3640,32 @@
maxWait = 'maxWait' in options && nativeMax(wait, options.maxWait || 0);
trailing = 'trailing' in options ? options.trailing : trailing;
}
var clear = function() {
clearTimeout(maxTimeoutId);
clearTimeout(timeoutId);
callCount = 0;
maxTimeoutId = timeoutId = null;
};
var delayed = function() {
var isCalled = trailing && (!leading || callCount > 1);
clear();
if (isCalled) {
if (maxWait !== false) {
lastCalled = new Date;
}
result = func.apply(thisArg, args);
}
};
var maxDelayed = function() {
clear();
if (trailing || (maxWait !== wait)) {
lastCalled = new Date;
result = func.apply(thisArg, args);
}
};
return function() {
args = arguments;
thisArg = this;
@@ -3703,6 +3718,9 @@
* // returns from the function before 'deferred' is logged
*/
function defer(func) {
if (!isFunction(func)) {
throw new TypeError;
}
var args = nativeSlice.call(arguments, 1);
return setTimeout(function() { func.apply(undefined, args); }, 1);
}
@@ -3725,6 +3743,9 @@
* // => 'logged later' (Appears after one second.)
*/
function delay(func, wait) {
if (!isFunction(func)) {
throw new TypeError;
}
var args = nativeSlice.call(arguments, 2);
return setTimeout(function() { func.apply(undefined, args); }, wait);
}
@@ -3780,6 +3801,9 @@
var ran,
result;
if (!isFunction(func)) {
throw new TypeError;
}
return function() {
if (ran) {
return result;
@@ -3812,7 +3836,7 @@
* // => 'hi moe'
*/
function partial(func) {
return createBound(func, null, nativeSlice.call(arguments, 1), [], true);
return createBound(func, 8, nativeSlice.call(arguments, 1));
}
/**
@@ -3886,6 +3910,9 @@
* // => 'before, hello moe, after'
*/
function wrap(value, wrapper) {
if (!isFunction(wrapper)) {
throw new TypeError;
}
return function() {
var args = [value];
push.apply(args, arguments);