mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-14 12:47:49 +00:00
Add _.runInContext.
Former-commit-id: f427f7d4704fb1b6af578b095e417ee25ca029e7
This commit is contained in:
165
lodash.js
165
lodash.js
@@ -20,22 +20,15 @@
|
|||||||
window = freeGlobal;
|
window = freeGlobal;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Used for array and object method references */
|
|
||||||
var arrayRef = [],
|
|
||||||
objectRef = {};
|
|
||||||
|
|
||||||
/** Used to generate unique IDs */
|
/** Used to generate unique IDs */
|
||||||
var idCounter = 0;
|
var idCounter = 0;
|
||||||
|
|
||||||
/** Used internally to indicate various things */
|
/** Used internally to indicate various things */
|
||||||
var indicatorObject = objectRef;
|
var indicatorObject = {};
|
||||||
|
|
||||||
/** Used by `cachedContains` as the default size when optimizations are enabled for large arrays */
|
/** Used by `cachedContains` as the default size when optimizations are enabled for large arrays */
|
||||||
var largeArraySize = 30;
|
var largeArraySize = 30;
|
||||||
|
|
||||||
/** Used to restore the original `_` reference in `noConflict` */
|
|
||||||
var oldDash = window._;
|
|
||||||
|
|
||||||
/** Used to match HTML entities */
|
/** Used to match HTML entities */
|
||||||
var reEscapedHtml = /&(?:amp|lt|gt|quot|#39);/g;
|
var reEscapedHtml = /&(?:amp|lt|gt|quot|#39);/g;
|
||||||
|
|
||||||
@@ -47,13 +40,6 @@
|
|||||||
/** Used to match regexp flags from their coerced string values */
|
/** Used to match regexp flags from their coerced string values */
|
||||||
var reFlags = /\w*$/;
|
var reFlags = /\w*$/;
|
||||||
|
|
||||||
/** Used to detect if a method is native */
|
|
||||||
var reNative = RegExp('^' +
|
|
||||||
(objectRef.valueOf + '')
|
|
||||||
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
||||||
.replace(/valueOf|for [^\]]+/g, '.+?') + '$'
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to match ES6 template delimiters
|
* Used to match ES6 template delimiters
|
||||||
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-7.8.6
|
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-7.8.6
|
||||||
@@ -81,25 +67,6 @@
|
|||||||
/** Used to make template sourceURLs easier to identify */
|
/** Used to make template sourceURLs easier to identify */
|
||||||
var templateCounter = 0;
|
var templateCounter = 0;
|
||||||
|
|
||||||
/** Native method shortcuts */
|
|
||||||
var ceil = Math.ceil,
|
|
||||||
concat = arrayRef.concat,
|
|
||||||
floor = Math.floor,
|
|
||||||
getPrototypeOf = reNative.test(getPrototypeOf = Object.getPrototypeOf) && getPrototypeOf,
|
|
||||||
hasOwnProperty = objectRef.hasOwnProperty,
|
|
||||||
push = arrayRef.push,
|
|
||||||
toString = objectRef.toString;
|
|
||||||
|
|
||||||
/* Native method shortcuts for methods with the same name as other `lodash` methods */
|
|
||||||
var nativeBind = reNative.test(nativeBind = slice.bind) && nativeBind,
|
|
||||||
nativeIsArray = reNative.test(nativeIsArray = Array.isArray) && nativeIsArray,
|
|
||||||
nativeIsFinite = window.isFinite,
|
|
||||||
nativeIsNaN = window.isNaN,
|
|
||||||
nativeKeys = reNative.test(nativeKeys = Object.keys) && nativeKeys,
|
|
||||||
nativeMax = Math.max,
|
|
||||||
nativeMin = Math.min,
|
|
||||||
nativeRandom = Math.random;
|
|
||||||
|
|
||||||
/** `Object#toString` result shortcuts */
|
/** `Object#toString` result shortcuts */
|
||||||
var argsClass = '[object Arguments]',
|
var argsClass = '[object Arguments]',
|
||||||
arrayClass = '[object Array]',
|
arrayClass = '[object Array]',
|
||||||
@@ -111,8 +78,97 @@
|
|||||||
regexpClass = '[object RegExp]',
|
regexpClass = '[object RegExp]',
|
||||||
stringClass = '[object String]';
|
stringClass = '[object String]';
|
||||||
|
|
||||||
|
/** Used to identify object classifications that `_.clone` supports */
|
||||||
|
var cloneableClasses = {};
|
||||||
|
cloneableClasses[funcClass] = false;
|
||||||
|
cloneableClasses[argsClass] = cloneableClasses[arrayClass] =
|
||||||
|
cloneableClasses[boolClass] = cloneableClasses[dateClass] =
|
||||||
|
cloneableClasses[numberClass] = cloneableClasses[objectClass] =
|
||||||
|
cloneableClasses[regexpClass] = cloneableClasses[stringClass] = true;
|
||||||
|
|
||||||
|
/** Used to determine if values are of the language type Object */
|
||||||
|
var objectTypes = {
|
||||||
|
'boolean': false,
|
||||||
|
'function': true,
|
||||||
|
'object': true,
|
||||||
|
'number': false,
|
||||||
|
'string': false,
|
||||||
|
'undefined': false
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Used to escape characters for inclusion in compiled string literals */
|
||||||
|
var stringEscapes = {
|
||||||
|
'\\': '\\',
|
||||||
|
"'": "'",
|
||||||
|
'\n': 'n',
|
||||||
|
'\r': 'r',
|
||||||
|
'\t': 't',
|
||||||
|
'\u2028': 'u2028',
|
||||||
|
'\u2029': 'u2029'
|
||||||
|
};
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new `lodash` function using the given `context` object.
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @memberOf _
|
||||||
|
* @category Utilities
|
||||||
|
* @param {Object} context The context object.
|
||||||
|
* @returns {Function} Returns the `lodash` function.
|
||||||
|
*/
|
||||||
|
function runInContext(context) {
|
||||||
|
context || (context = window);
|
||||||
|
|
||||||
|
/** Native constructor references */
|
||||||
|
var Array = context.Array,
|
||||||
|
Boolean = context.Boolean,
|
||||||
|
Date = context.Date,
|
||||||
|
Function = context.Function,
|
||||||
|
Object = context.Object,
|
||||||
|
Number = context.Number,
|
||||||
|
Math = context.Math,
|
||||||
|
RegExp = context.RegExp,
|
||||||
|
String = context.String;
|
||||||
|
|
||||||
|
/** Used for `Array`, `Math`, and `Object` method references */
|
||||||
|
var arrayRef = Array.prototype,
|
||||||
|
objectRef = Object.prototype;
|
||||||
|
|
||||||
|
/** Used to restore the original `_` reference in `noConflict` */
|
||||||
|
var oldDash = context._;
|
||||||
|
|
||||||
|
/** Used to detect if a method is native */
|
||||||
|
var reNative = RegExp('^' +
|
||||||
|
(objectRef.valueOf + '')
|
||||||
|
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
||||||
|
.replace(/valueOf|for [^\]]+/g, '.+?') + '$'
|
||||||
|
);
|
||||||
|
|
||||||
|
/** Native method shortcuts */
|
||||||
|
var ceil = Math.ceil,
|
||||||
|
clearTimeout = context.clearTimeout,
|
||||||
|
concat = arrayRef.concat,
|
||||||
|
floor = Math.floor,
|
||||||
|
getPrototypeOf = reNative.test(getPrototypeOf = Object.getPrototypeOf) && getPrototypeOf,
|
||||||
|
hasOwnProperty = objectRef.hasOwnProperty,
|
||||||
|
push = arrayRef.push,
|
||||||
|
setTimeout = context.setTimeout,
|
||||||
|
toString = objectRef.toString;
|
||||||
|
|
||||||
|
/* Native method shortcuts for methods with the same name as other `lodash` methods */
|
||||||
|
var nativeBind = reNative.test(nativeBind = slice.bind) && nativeBind,
|
||||||
|
nativeIsArray = reNative.test(nativeIsArray = Array.isArray) && nativeIsArray,
|
||||||
|
nativeIsFinite = context.isFinite,
|
||||||
|
nativeIsNaN = context.isNaN,
|
||||||
|
nativeKeys = reNative.test(nativeKeys = Object.keys) && nativeKeys,
|
||||||
|
nativeMax = Math.max,
|
||||||
|
nativeMin = Math.min,
|
||||||
|
nativeRandom = Math.random;
|
||||||
|
|
||||||
/** Detect various environments */
|
/** Detect various environments */
|
||||||
var isIeOpera = !!window.attachEvent,
|
var isIeOpera = !!context.attachEvent,
|
||||||
isV8 = nativeBind && !/\n|true/.test(nativeBind + isIeOpera);
|
isV8 = nativeBind && !/\n|true/.test(nativeBind + isIeOpera);
|
||||||
|
|
||||||
/* Detect if `Function#bind` exists and is inferred to be fast (all but V8) */
|
/* Detect if `Function#bind` exists and is inferred to be fast (all but V8) */
|
||||||
@@ -193,14 +249,6 @@
|
|||||||
var noNodeClass = toString.call(document) == objectClass && !({ 'toString': 0 } + '');
|
var noNodeClass = toString.call(document) == objectClass && !({ 'toString': 0 } + '');
|
||||||
} catch(e) { }
|
} catch(e) { }
|
||||||
|
|
||||||
/** Used to identify object classifications that `_.clone` supports */
|
|
||||||
var cloneableClasses = {};
|
|
||||||
cloneableClasses[funcClass] = false;
|
|
||||||
cloneableClasses[argsClass] = cloneableClasses[arrayClass] =
|
|
||||||
cloneableClasses[boolClass] = cloneableClasses[dateClass] =
|
|
||||||
cloneableClasses[numberClass] = cloneableClasses[objectClass] =
|
|
||||||
cloneableClasses[regexpClass] = cloneableClasses[stringClass] = true;
|
|
||||||
|
|
||||||
/** Used to lookup a built-in constructor by [[Class]] */
|
/** Used to lookup a built-in constructor by [[Class]] */
|
||||||
var ctorByClass = {};
|
var ctorByClass = {};
|
||||||
ctorByClass[arrayClass] = Array;
|
ctorByClass[arrayClass] = Array;
|
||||||
@@ -211,27 +259,6 @@
|
|||||||
ctorByClass[regexpClass] = RegExp;
|
ctorByClass[regexpClass] = RegExp;
|
||||||
ctorByClass[stringClass] = String;
|
ctorByClass[stringClass] = String;
|
||||||
|
|
||||||
/** Used to determine if values are of the language type Object */
|
|
||||||
var objectTypes = {
|
|
||||||
'boolean': false,
|
|
||||||
'function': true,
|
|
||||||
'object': true,
|
|
||||||
'number': false,
|
|
||||||
'string': false,
|
|
||||||
'undefined': false
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Used to escape characters for inclusion in compiled string literals */
|
|
||||||
var stringEscapes = {
|
|
||||||
'\\': '\\',
|
|
||||||
"'": "'",
|
|
||||||
'\n': 'n',
|
|
||||||
'\r': 'r',
|
|
||||||
'\t': 't',
|
|
||||||
'\u2028': 'u2028',
|
|
||||||
'\u2029': 'u2029'
|
|
||||||
};
|
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -4240,7 +4267,7 @@
|
|||||||
}
|
}
|
||||||
// use `setImmediate` if it's available in Node.js
|
// use `setImmediate` if it's available in Node.js
|
||||||
if (isV8 && freeModule && typeof setImmediate == 'function') {
|
if (isV8 && freeModule && typeof setImmediate == 'function') {
|
||||||
defer = bind(setImmediate, window);
|
defer = bind(setImmediate, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -4523,7 +4550,7 @@
|
|||||||
* var lodash = _.noConflict();
|
* var lodash = _.noConflict();
|
||||||
*/
|
*/
|
||||||
function noConflict() {
|
function noConflict() {
|
||||||
window._ = oldDash;
|
context._ = oldDash;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -5016,6 +5043,7 @@
|
|||||||
lodash.reduce = reduce;
|
lodash.reduce = reduce;
|
||||||
lodash.reduceRight = reduceRight;
|
lodash.reduceRight = reduceRight;
|
||||||
lodash.result = result;
|
lodash.result = result;
|
||||||
|
lodash.runInContext = runInContext;
|
||||||
lodash.size = size;
|
lodash.size = size;
|
||||||
lodash.some = some;
|
lodash.some = some;
|
||||||
lodash.sortedIndex = sortedIndex;
|
lodash.sortedIndex = sortedIndex;
|
||||||
@@ -5127,8 +5155,13 @@
|
|||||||
lodash._each = each;
|
lodash._each = each;
|
||||||
lodash._iteratorTemplate = iteratorTemplate;
|
lodash._iteratorTemplate = iteratorTemplate;
|
||||||
|
|
||||||
|
return lodash;
|
||||||
|
}
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
var lodash = runInContext();
|
||||||
|
|
||||||
// expose Lo-Dash
|
// expose Lo-Dash
|
||||||
// some AMD build optimizers, like r.js, check for specific condition patterns like the following:
|
// some AMD build optimizers, like r.js, check for specific condition patterns like the following:
|
||||||
if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
|
if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
|
||||||
|
|||||||
Reference in New Issue
Block a user