diff --git a/build.js b/build.js index 39fcdb193..45c5a6ff3 100755 --- a/build.js +++ b/build.js @@ -151,7 +151,7 @@ 'reject': ['filter', 'identity', 'isEqual', 'keys'], 'rest': [], 'result': ['isFunction'], - 'runInContext': [], + 'runInContext': ['extend'], 'shuffle': ['forEach'], 'size': ['keys'], 'some': ['identity', 'isArray', 'isEqual', 'keys'], @@ -972,15 +972,16 @@ if (snippet) { if (funcName == 'runInContext') { source = source.replace(snippet, function() { - return snippet.replace(/^[\s\S]+?\n( *).+?context *=.+(\n[\s\S]+?\n)\1return lodash[\s\S]+$/, function() { - return arguments[2].replace(/^ {4}/gm, ' '); - }); + return snippet + .replace(/^[\s\S]+?function runInContext[\s\S]+?context *= *context.+| *return lodash[\s\S]+$/g, '') + .replace(/^ {4}/gm, ' '); }); source = source .replace(/context/g, 'window') .replace(/(?:\n +\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)?\n *var Array *=[\s\S]+?;\n/, '') - .replace(/^ *var lodash *= *runInContext.+\n+/m, ''); + .replace(/(return *|= *)_([;)])/g, '$1lodash$2') + .replace(/^ *var _ *=.+\n+/m, ''); } else { source = source.replace(snippet, ''); diff --git a/dist/lodash.compat.js b/dist/lodash.compat.js index 6f639b829..c28c89fab 100644 --- a/dist/lodash.compat.js +++ b/dist/lodash.compat.js @@ -123,7 +123,7 @@ * @returns {Function} Returns the `lodash` function. */ function runInContext(context) { - context || (context = window); + context = context ? _.extend(createObject(window), context) : window; /** Native constructor references */ var Array = context.Array, @@ -623,10 +623,8 @@ : partialArgs; } if (this instanceof bound) { - // ensure `new bound` is an instance of `bound` and `func` - noop.prototype = func.prototype; - thisBinding = new noop; - noop.prototype = null; + // ensure `new bound` is an instance of `func` + thisBinding = createObject(func.prototype); // mimic the constructor's `return` behavior // http://es5.github.com/#x13.2.2 @@ -750,6 +748,20 @@ ); } + /** + * Creates a new object that inherits from the given `prototype` object. + * + * @private + * @param {Object} prototype The prototype object. + * @returns {Object} Returns the new object. + */ + function createObject(prototype) { + noop.prototype = prototype; + var result = new noop; + noop.prototype = null; + return result; + } + /** * A function compiled to iterate `arguments` objects, arrays, objects, and * strings consistenly across environments, executing the `callback` for each @@ -5161,7 +5173,7 @@ /*--------------------------------------------------------------------------*/ // expose Lo-Dash - var lodash = runInContext(); + var _ = runInContext(); // 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) { @@ -5169,27 +5181,27 @@ // case Lo-Dash was injected by a third-party script and not intended to be // loaded as a module. The global assignment can be reverted in the Lo-Dash // module via its `noConflict()` method. - window._ = lodash; + window._ = _; // define as an anonymous module so, through path mapping, it can be // referenced as the "underscore" module define(function() { - return lodash; + return _; }); } // check for `exports` after `define` in case a build optimizer adds an `exports` object else if (freeExports) { // in Node.js or RingoJS v0.8.0+ if (freeModule) { - (freeModule.exports = lodash)._ = lodash; + (freeModule.exports = _)._ = _; } // in Narwhal or RingoJS v0.7.0- else { - freeExports._ = lodash; + freeExports._ = _; } } else { // in a browser or Rhino - window._ = lodash; + window._ = _; } }(this)); diff --git a/dist/lodash.compat.min.js b/dist/lodash.compat.min.js index d9edf97f9..b615d7ff4 100644 --- a/dist/lodash.compat.min.js +++ b/dist/lodash.compat.min.js @@ -4,40 +4,41 @@ * Build: `lodash -o ./dist/lodash.compat.js` * Underscore.js 1.4.4 underscorejs.org/LICENSE */ -;(function(n){function t(i){function a(n){return n&&typeof n=="object"&&n.__wrapped__?n:this instanceof a?(this.__wrapped__=n,void 0):new a(n)}function R(n,t,r){t||(t=0);var e=n.length,u=e-t>=(r||p);if(u){var o={};for(r=t-1;++rt||typeof n=="undefined")return 1; -if(nu;u++)r+="j='"+t.k[u]+"';if(","constructor"==t.k[u]&&(r+="!(g&&g.prototype===n)&&"),r+="i.call(n,j)){"+t.h+"}"; -return(t.b||t.i)&&(r+="}"),r+=t.c+";return u",Bt("f,i,k,l,m,r,p","return function("+n+"){"+r+"}")(C,Jt,H,Sr,et,B,rr)}function K(n){return"\\"+F[n]}function L(n){return Er[n]}function M(n){return typeof n.toString!="function"&&typeof(n+"")=="string"}function U(){}function V(n,t,r){t||(t=0),typeof r=="undefined"&&(r=n?n.length:0);var e=-1;r=r-t||0;for(var u=Nt(0>r?0:r);++er?er(0,u+r):r)||0,typeof u=="number"?i=-1<(et(n)?n.indexOf(t,r):dt(n,t,r)):wr(n,function(n){return++eu&&(u=i)}}else t=!t&&et(n)?P:C(t,r),wr(n,function(n,r,o){r=t(n,r,o),r>e&&(e=r,u=n)});return u}function vt(n,t,r,e){var u=3>arguments.length;if(t=C(t,e,4),Sr(n)){var i=-1,f=n.length;for(u&&(r=n[++i]);++iarguments.length;if(typeof i!="number")var a=kr(n),i=a.length;else hr&&et(n)&&(u=n.split(""));return t=C(t,e,4),lt(n,function(n,e,c){e=a?a[--i]:--i,r=f?(f=o,u[e]):t(r,u[e],e,c)}),r}function ht(n,t,r){var e;if(t=C(t,r),Sr(n)){r=-1;for(var u=n.length;++rr?er(0,u+r):r||0)-1;else if(r)return e=bt(n,t),n[e]===t?e:-1;for(;++e>>1,r(n[e])dt(a,p))&&((r||c)&&a.push(p),f.push(e))}return f}function wt(n,t){return cr||Yt&&2/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:d,variable:"",imports:{_:a}}; -var _r={a:"q,w,h",l:"var a=arguments,b=0,c=typeof h=='number'?2:a.length;while(++b":">",'"':""","'":"'"},Ar=Y(Er),Ir=T(_r,{l:_r.l.replace(";",";if(c>3&&typeof a[c-2]=='function'){var d=f(a[--c-1],a[c--],2);}else if(c>2&&typeof a[c-1]=='function'){d=a[--c];}"),h:"u[j]=d?d(u[j],n[j]):n[j]"}),Nr=T(_r);nt(/x/)&&(nt=function(n){return n instanceof Bt||Xt.call(n)==E});var $r=Ht?function(n){if(!n||typeof n!="object")return o;var t=n.valueOf,r=typeof t=="function"&&(r=Ht(t))&&Ht(r);return r?n==r||Ht(n)==r&&!H(n):J(n)}:J,qr=pt,Br=at;return ar&&f&&typeof setImmediate=="function"&&(xt=wt(setImmediate,i)),a.after=function(n,t){return 1>n?t():function(){return 1>--n?t.apply(this,arguments):void 0 -}},a.assign=Ir,a.at=function(n){var t=-1,r=Vt.apply(Ct,V(arguments,1)),e=r.length,u=Nt(e);for(hr&&et(n)&&(n=n.split(""));++tdt(a,c)){i&&a.push(c);for(var p=r;--p;)if(!(e[p]||(e[p]=R(t[p],0,100)))(c))continue n;f.push(c)}}return f},a.invert=Y,a.invoke=function(n,t){var r=V(arguments,2),e=-1,u=typeof t=="function",o=n?n.length:0,i=Nt(typeof o=="number"?o:0);return lt(n,function(n){i[++e]=(u?t:n[t]).apply(n,r)}),i},a.keys=kr,a.map=pt,a.max=st,a.memoize=function(n,t){var r={};return function(){var e=(t?t.apply(this,arguments):arguments[0])+""; -return Jt.call(r,e)?r[e]:r[e]=n.apply(this,arguments)}},a.merge=ut,a.min=function(n,t,r){var e=1/0,u=e;if(!t&&Sr(n)){r=-1;for(var o=n.length;++rdt(o,r,1))&&(u[r]=n) -}),u},a.once=function(n){var t,r;return function(){return t?r:(t=e,r=n.apply(this,arguments),n=u,r)}},a.pairs=function(n){for(var t=-1,r=kr(n),e=r.length,u=Nt(e);++tr?er(0,e+r):ur(r,e-1))+1);e--;)if(n[e]===t)return e;return-1},a.mixin=St,a.noConflict=function(){return i._=Kt,this -},a.random=function(n,t){return n==u&&t==u&&(t=1),n=+n||0,t==u&&(t=n,n=0),n+Gt(or()*((+t||0)-n+1))},a.reduce=vt,a.reduceRight=gt,a.result=function(n,t){var e=n?n[t]:r;return nt(e)?n[t]():e},a.runInContext=t,a.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:kr(n).length},a.some=ht,a.sortedIndex=bt,a.template=function(n,t,u){var o=a.templateSettings;n||(n=""),u=Nr({},u,o);var i,f=Nr({},u.imports,o.imports),o=kr(f),f=ot(f),c=0,l=u.interpolate||_,p="__p+='",l=Dt((u.escape||_).source+"|"+l.source+"|"+(l===d?m:_).source+"|"+(u.evaluate||_).source+"|$","g"); -n.replace(l,function(t,r,u,o,f,a){return u||(u=o),p+=n.slice(c,a).replace(j,K),r&&(p+="'+__e("+r+")+'"),f&&(i=e,p+="';"+f+";__p+='"),u&&(p+="'+((__t=("+u+"))==null?'':__t)+'"),c=a+t.length,t}),p+="';\n",l=u=u.variable,l||(u="obj",p="with("+u+"){"+p+"}"),p=(i?p.replace(v,""):p).replace(g,"$1").replace(h,"$1;"),p="function("+u+"){"+(l?"":u+"||("+u+"={});")+"var __t,__p='',__e=_.escape"+(i?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+p+"return __p}";try{var s=Bt(o,"return "+p).apply(r,f) -}catch(y){throw y.source=p,y}return t?s(t):(s.source=p,s)},a.unescape=function(n){return n==u?"":(n+"").replace(s,G)},a.uniqueId=function(n){var t=++c;return(n==u?"":n+"")+t},a.all=ft,a.any=ht,a.detect=ct,a.foldl=vt,a.foldr=gt,a.include=it,a.inject=vt,Or(a,function(n,t){a.prototype[t]||(a.prototype[t]=function(){var t=[this.__wrapped__];return Qt.apply(t,arguments),n.apply(a,t)})}),a.first=yt,a.last=function(n,t,r){if(n){var e=0,o=n.length;if(typeof t!="number"&&t!=u){var i=o;for(t=C(t,r);i--&&t(n[i],i,n);)e++ -}else if(e=t,e==u||r)return n[o-1];return V(n,er(0,o-e))}},a.take=yt,a.head=yt,Or(a,function(n,t){a.prototype[t]||(a.prototype[t]=function(t,r){var e=n(this.__wrapped__,t,r);return t==u||r&&typeof t!="function"?e:new a(e)})}),a.VERSION="1.0.1",a.prototype.toString=function(){return this.__wrapped__+""},a.prototype.value=kt,a.prototype.valueOf=kt,wr(["join","pop","shift"],function(n){var t=Ct[n];a.prototype[n]=function(){return t.apply(this.__wrapped__,arguments)}}),wr(["push","reverse","sort","unshift"],function(n){var t=Ct[n]; -a.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this}}),wr(["concat","slice","splice"],function(n){var t=Ct[n];a.prototype[n]=function(){return new a(t.apply(this.__wrapped__,arguments))}}),pr&&wr(["pop","shift","splice"],function(n){var t=Ct[n],r="splice"==n;a.prototype[n]=function(){var n=this.__wrapped__,e=t.apply(n,arguments);return 0===n.length&&delete n[0],r?new a(e):e}}),a}var r,e=!0,u=null,o=!1,i=typeof exports=="object"&&exports,f=typeof module=="object"&&module&&module.exports==i&&module,a=typeof global=="object"&&global; -a.global===a&&(n=a);var c=0,l={},p=30,s=/&(?:amp|lt|gt|quot|#39);/g,v=/\b__p\+='';/g,g=/\b(__p\+=)''\+/g,h=/(__e\(.*?\)|\b__t\))\+'';/g,y=/\w*$/,m=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,d=/<%=([\s\S]+?)%>/g,_=/($^)/,b=/[&<>"']/g,j=/['\n\r\t\u2028\u2029\\]/g,w="constructor hasOwnProperty isPrototypeOf propertyIsEnumerable toLocaleString toString valueOf".split(" "),x="[object Arguments]",O="[object Array]",S="[object Boolean]",k="[object Date]",E="[object Function]",A="[object Number]",I="[object Object]",N="[object RegExp]",$="[object String]",q={}; -q[E]=o,q[x]=q[O]=q[S]=q[k]=q[A]=q[I]=q[N]=q[$]=e;var B={"boolean":o,"function":e,object:e,number:o,string:o,undefined:o},F={"\\":"\\","'":"'","\n":"n","\r":"r"," ":"t","\u2028":"u2028","\u2029":"u2029"},R=t();typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=R,define(function(){return R})):i?f?(f.exports=R)._=R:i._=R:n._=R})(this); \ No newline at end of file +;(function(n){function t(e){function o(n){return n&&typeof n=="object"&&n.__wrapped__?n:this instanceof o?(this.__wrapped__=n,void 0):new o(n)}function B(n,t,r){t||(t=0);var e=n.length,u=e-t>=(r||a);if(u){var o={};for(r=t-1;++rt||typeof n=="undefined")return 1; +if(nu;u++)e+="j='"+t.k[u]+"';if(","constructor"==t.k[u]&&(e+="!(g&&g.prototype===n)&&"),e+="i.call(n,j)){"+t.h+"}"}return(t.b||t.i)&&(e+="}"),e+=t.c+";return u",r("f,i,k,l,m,r,p","return function("+n+"){"+e+"}")(D,Ht,G,Or,rt,N,tr)}function C(n){return M.prototype=n,n=new M,M.prototype=null,n}function T(n){return"\\"+$[n]}function K(n){return kr[n]}function L(n){return typeof n.toString!="function"&&typeof(n+"")=="string"}function M(){}function U(n,t,r){t||(t=0),typeof r=="undefined"&&(r=n?n.length:0); +var e=-1;r=r-t||0;for(var u=It(0>r?0:r);++er?rr(0,u+r):r)||0,typeof u=="number"?o=-1<(rt(n)?n.indexOf(t,r):mt(n,t,r)):jr(n,function(n){return++eu&&(u=i)}}else t=!t&&rt(n)?F:D(t,r),jr(n,function(n,r,o){r=t(n,r,o),r>e&&(e=r,u=n)});return u}function st(n,t,r,e){var u=3>arguments.length;if(t=D(t,e,4),Or(n)){var o=-1,i=n.length;for(u&&(r=n[++o]);++oarguments.length;if(typeof o!="number")var f=Sr(n),o=f.length;else gr&&rt(n)&&(u=n.split(""));return t=D(t,e,4),ct(n,function(n,e,a){e=f?f[--o]:--o,r=i?(i=!1,u[e]):t(r,u[e],e,a) +}),r}function gt(n,t,r){var e;if(t=D(t,r),Or(n)){r=-1;for(var u=n.length;++rr?rr(0,u+r):r||0)-1;else if(r)return e=_t(n,t),n[e]===t?e:-1;for(;++e>>1,r(n[e])mt(f,l))&&((r||a)&&f.push(l),i.push(e))}return i}function jt(n,t){return ar||Xt&&2/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:h,variable:"",imports:{_:o}}; +var dr={a:"q,w,h",l:"var a=arguments,b=0,c=typeof h=='number'?2:a.length;while(++b":">",'"':""","'":"'"},Er=X(kr),Ar=z(dr,{l:dr.l.replace(";",";if(c>3&&typeof a[c-2]=='function'){var d=f(a[--c-1],a[c--],2);}else if(c>2&&typeof a[c-1]=='function'){d=a[--c];}"),h:"u[j]=d?d(u[j],n[j]):n[j]"}),Ir=z(dr);Z(/x/)&&(Z=function(n){return n instanceof qt||Wt.call(n)==O});var Nr=Gt?function(n){if(!n||typeof n!="object")return!1;var t=n.valueOf,r=typeof t=="function"&&(r=Gt(t))&&Gt(r);return r?n==r||Gt(n)==r&&!G(n):H(n)}:H,$r=lt,qr=ft;return fr&&u&&typeof setImmediate=="function"&&(wt=jt(setImmediate,e)),o.after=function(n,t){return 1>n?t():function(){return 1>--n?t.apply(this,arguments):void 0 +}},o.assign=Ar,o.at=function(n){var t=-1,r=Ut.apply(zt,U(arguments,1)),e=r.length,u=It(e);for(gr&&rt(n)&&(n=n.split(""));++tmt(a,c)){i&&a.push(c);for(var p=r;--p;)if(!(e[p]||(e[p]=B(t[p],0,100)))(c))continue n;f.push(c)}}return f},o.invert=X,o.invoke=function(n,t){var r=U(arguments,2),e=-1,u=typeof t=="function",o=n?n.length:0,i=It(typeof o=="number"?o:0);return ct(n,function(n){i[++e]=(u?t:n[t]).apply(n,r)}),i},o.keys=Sr,o.map=lt,o.max=pt,o.memoize=function(n,t){var r={};return function(){var e=(t?t.apply(this,arguments):arguments[0])+""; +return Ht.call(r,e)?r[e]:r[e]=n.apply(this,arguments)}},o.merge=et,o.min=function(n,t,r){var e=1/0,u=e;if(!t&&Or(n)){r=-1;for(var o=n.length;++rmt(o,r,1))&&(u[r]=n) +}),u},o.once=function(n){var t,r;return function(){return t?r:(t=!0,r=n.apply(this,arguments),n=null,r)}},o.pairs=function(n){for(var t=-1,r=Sr(n),e=r.length,u=It(e);++tr?rr(0,e+r):er(r,e-1))+1);e--;)if(n[e]===t)return e;return-1},o.mixin=Ot,o.noConflict=function(){return e._=Tt,this +},o.random=function(n,t){return null==n&&null==t&&(t=1),n=+n||0,null==t&&(t=n,n=0),n+Vt(ur()*((+t||0)-n+1))},o.reduce=st,o.reduceRight=vt,o.result=function(n,t){var e=n?n[t]:r;return Z(e)?n[t]():e},o.runInContext=t,o.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:Sr(n).length},o.some=gt,o.sortedIndex=_t,o.template=function(n,t,e){var u=o.templateSettings;n||(n=""),e=Ir({},e,u);var i,f=Ir({},e.imports,u.imports),u=Sr(f),f=ut(f),a=0,c=e.interpolate||y,v="__p+='",c=Pt((e.escape||y).source+"|"+c.source+"|"+(c===h?g:y).source+"|"+(e.evaluate||y).source+"|$","g"); +n.replace(c,function(t,r,e,u,o,f){return e||(e=u),v+=n.slice(a,f).replace(d,T),r&&(v+="'+__e("+r+")+'"),o&&(i=!0,v+="';"+o+";__p+='"),e&&(v+="'+((__t=("+e+"))==null?'':__t)+'"),a=f+t.length,t}),v+="';\n",c=e=e.variable,c||(e="obj",v="with("+e+"){"+v+"}"),v=(i?v.replace(l,""):v).replace(p,"$1").replace(s,"$1;"),v="function("+e+"){"+(c?"":e+"||("+e+"={});")+"var __t,__p='',__e=_.escape"+(i?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+v+"return __p}";try{var m=qt(u,"return "+v).apply(r,f) +}catch(_){throw _.source=v,_}return t?m(t):(m.source=v,m)},o.unescape=function(n){return null==n?"":(n+"").replace(c,V)},o.uniqueId=function(n){var t=++i;return(null==n?"":n+"")+t},o.all=it,o.any=gt,o.detect=at,o.foldl=st,o.foldr=vt,o.include=ot,o.inject=st,xr(o,function(n,t){o.prototype[t]||(o.prototype[t]=function(){var t=[this.__wrapped__];return Jt.apply(t,arguments),n.apply(o,t)})}),o.first=ht,o.last=function(n,t,r){if(n){var e=0,u=n.length;if(typeof t!="number"&&null!=t){var o=u;for(t=D(t,r);o--&&t(n[o],o,n);)e++ +}else if(e=t,null==e||r)return n[u-1];return U(n,rr(0,u-e))}},o.take=ht,o.head=ht,xr(o,function(n,t){o.prototype[t]||(o.prototype[t]=function(t,r){var e=n(this.__wrapped__,t,r);return null==t||r&&typeof t!="function"?e:new o(e)})}),o.VERSION="1.0.1",o.prototype.toString=function(){return this.__wrapped__+""},o.prototype.value=St,o.prototype.valueOf=St,jr(["join","pop","shift"],function(n){var t=zt[n];o.prototype[n]=function(){return t.apply(this.__wrapped__,arguments)}}),jr(["push","reverse","sort","unshift"],function(n){var t=zt[n]; +o.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this}}),jr(["concat","slice","splice"],function(n){var t=zt[n];o.prototype[n]=function(){return new o(t.apply(this.__wrapped__,arguments))}}),lr&&jr(["pop","shift","splice"],function(n){var t=zt[n],r="splice"==n;o.prototype[n]=function(){var n=this.__wrapped__,e=t.apply(n,arguments);return 0===n.length&&delete n[0],r?new o(e):e}}),o}var r,e=typeof exports=="object"&&exports,u=typeof module=="object"&&module&&module.exports==e&&module,o=typeof global=="object"&&global; +o.global===o&&(n=o);var i=0,f={},a=30,c=/&(?:amp|lt|gt|quot|#39);/g,l=/\b__p\+='';/g,p=/\b(__p\+=)''\+/g,s=/(__e\(.*?\)|\b__t\))\+'';/g,v=/\w*$/,g=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,h=/<%=([\s\S]+?)%>/g,y=/($^)/,m=/[&<>"']/g,d=/['\n\r\t\u2028\u2029\\]/g,_="constructor hasOwnProperty isPrototypeOf propertyIsEnumerable toLocaleString toString valueOf".split(" "),b="[object Arguments]",j="[object Array]",w="[object Boolean]",x="[object Date]",O="[object Function]",S="[object Number]",k="[object Object]",E="[object RegExp]",A="[object String]",I={}; +I[O]=!1,I[b]=I[j]=I[w]=I[x]=I[S]=I[k]=I[E]=I[A]=!0;var N={"boolean":!1,"function":!0,object:!0,number:!1,string:!1,undefined:!1},$={"\\":"\\","'":"'","\n":"n","\r":"r"," ":"t","\u2028":"u2028","\u2029":"u2029"},q=t();typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=q,define(function(){return q})):e?u?(u.exports=q)._=q:e._=q:n._=q})(this); \ No newline at end of file diff --git a/dist/lodash.js b/dist/lodash.js index a327fc3ce..15759ab89 100644 --- a/dist/lodash.js +++ b/dist/lodash.js @@ -117,7 +117,7 @@ * @returns {Function} Returns the `lodash` function. */ function runInContext(context) { - context || (context = window); + context = context ? _.extend(createObject(window), context) : window; /** Native constructor references */ var Array = context.Array, @@ -505,10 +505,8 @@ : partialArgs; } if (this instanceof bound) { - // ensure `new bound` is an instance of `bound` and `func` - noop.prototype = func.prototype; - thisBinding = new noop; - noop.prototype = null; + // ensure `new bound` is an instance of `func` + thisBinding = createObject(func.prototype); // mimic the constructor's `return` behavior // http://es5.github.com/#x13.2.2 @@ -627,6 +625,20 @@ ); } + /** + * Creates a new object that inherits from the given `prototype` object. + * + * @private + * @param {Object} prototype The prototype object. + * @returns {Object} Returns the new object. + */ + function createObject(prototype) { + noop.prototype = prototype; + var result = new noop; + noop.prototype = null; + return result; + } + /** * A function compiled to iterate `arguments` objects, arrays, objects, and * strings consistenly across environments, executing the `callback` for each @@ -4986,7 +4998,7 @@ /*--------------------------------------------------------------------------*/ // expose Lo-Dash - var lodash = runInContext(); + var _ = runInContext(); // 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) { @@ -4994,27 +5006,27 @@ // case Lo-Dash was injected by a third-party script and not intended to be // loaded as a module. The global assignment can be reverted in the Lo-Dash // module via its `noConflict()` method. - window._ = lodash; + window._ = _; // define as an anonymous module so, through path mapping, it can be // referenced as the "underscore" module define(function() { - return lodash; + return _; }); } // check for `exports` after `define` in case a build optimizer adds an `exports` object else if (freeExports) { // in Node.js or RingoJS v0.8.0+ if (freeModule) { - (freeModule.exports = lodash)._ = lodash; + (freeModule.exports = _)._ = _; } // in Narwhal or RingoJS v0.7.0- else { - freeExports._ = lodash; + freeExports._ = _; } } else { // in a browser or Rhino - window._ = lodash; + window._ = _; } }(this)); diff --git a/dist/lodash.min.js b/dist/lodash.min.js index c389ce17f..ecee079ed 100644 --- a/dist/lodash.min.js +++ b/dist/lodash.min.js @@ -4,37 +4,37 @@ * Build: `lodash modern -o ./dist/lodash.js` * Underscore.js 1.4.4 underscorejs.org/LICENSE */ -;(function(n){function t(e){function o(n){return n&&typeof n=="object"&&n.__wrapped__?n:this instanceof o?(this.__wrapped__=n,void 0):new o(n)}function $(n,t,r){t||(t=0);var e=n.length,u=e-t>=(r||a);if(u){var o={};for(r=t-1;++rt||typeof n=="undefined")return 1; -if(nr?0:r);++er?Jt(0,u+r):r)||0,typeof u=="number"?o=-1<(W(n)?n.indexOf(t,r):pt(n,t,r)):Zt(n,function(n){return++eu&&(u=i)}}else t=!t&&W(n)?I:F(t,r),Zt(n,function(n,r,o){r=t(n,r,o),r>e&&(e=r,u=n)});return u}function it(n,t,r,e){var u=3>arguments.length;if(t=F(t,e,4),rr(n)){var o=-1,i=n.length;for(u&&(r=n[++o]);++oarguments.length;if(typeof u!="number")var i=er(n),u=i.length;return t=F(t,e,4),et(n,function(e,f,a){f=i?i[--u]:--u,r=o?(o=!1,n[f]):t(r,n[f],f,a)}),r}function at(n,t,r){var e;if(t=F(t,r),rr(n)){r=-1;for(var u=n.length;++rr?Jt(0,u+r):r||0)-1;else if(r)return e=vt(n,t),n[e]===t?e:-1;for(;++e>>1,r(n[e])pt(f,l))&&((r||a)&&f.push(l),i.push(e))}return i}function ht(n,t){return Wt||Mt&&2/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:h,variable:"",imports:{_:o}};var jt={a:"q,w,h",l:"var a=arguments,b=0,c=typeof h=='number'?2:a.length;while(++b":">",'"':""","'":"'"},or=G(ur),ir=R(jt,{l:jt.l.replace(";",";if(c>3&&typeof a[c-2]=='function'){var d=f(a[--c-1],a[c--],2);}else if(c>2&&typeof a[c-1]=='function'){d=a[--c];}"),h:"u[j]=d?d(u[j],n[j]):n[j]"}),fr=R(jt),ar=function(n){if(!n||typeof n!="object")return!1;var t=n.valueOf,r=typeof t=="function"&&(r=zt(t))&&zt(r);if(r)n=n==r||zt(n)==r&&!K(n);else{var e=!1;!n||typeof n!="object"||K(n)?n=e:(t=n.constructor,!J(t)||t instanceof t?(nr(n,function(n,t){e=t -}),n=!1===e||Ct.call(n,e)):n=e)}return n};return Ot&&u&&typeof setImmediate=="function"&&(yt=ht(setImmediate,e)),o.after=function(n,t){return 1>n?t():function(){return 1>--n?t.apply(this,arguments):void 0}},o.assign=ir,o.at=function(n){for(var t=-1,r=Rt.apply(Nt,P(arguments,1)),e=r.length,u=bt(e);++tpt(a,c)){i&&a.push(c);for(var p=r;--p;)if(!(e[p]||(e[p]=$(t[p],0,100)))(c))continue n;f.push(c)}}return f},o.invert=G,o.invoke=function(n,t){var r=P(arguments,2),e=-1,u=typeof t=="function",o=n?n.length:0,i=bt(typeof o=="number"?o:0); -return et(n,function(n){i[++e]=(u?t:n[t]).apply(n,r)}),i},o.keys=er,o.map=ut,o.max=ot,o.memoize=function(n,t){var r={};return function(){var e=(t?t.apply(this,arguments):arguments[0])+"";return Ct.call(r,e)?r[e]:r[e]=n.apply(this,arguments)}},o.merge=X,o.min=function(n,t,r){var e=1/0,u=e;if(!t&&rr(n)){r=-1;for(var o=n.length;++rpt(o,r,1))&&(u[r]=n)}),u},o.once=function(n){var t,r;return function(){return t?r:(t=!0,r=n.apply(this,arguments),n=null,r)}},o.pairs=function(n){for(var t=-1,r=er(n),e=r.length,u=bt(e);++tr?Jt(0,e+r):Lt(r,e-1))+1);e--;)if(n[e]===t)return e;return-1},o.mixin=dt,o.noConflict=function(){return e._=It,this},o.random=function(n,t){return null==n&&null==t&&(t=1),n=+n||0,null==t&&(t=n,n=0),n+Dt(Qt()*((+t||0)-n+1))},o.reduce=it,o.reduceRight=ft,o.result=function(n,t){var e=n?n[t]:r;return J(e)?n[t]():e},o.runInContext=t,o.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:er(n).length},o.some=at,o.sortedIndex=vt,o.template=function(n,t,e){var u=o.templateSettings; -n||(n=""),e=fr({},e,u);var i,f=fr({},e.imports,u.imports),u=er(f),f=Y(f),a=0,c=e.interpolate||y,v="__p+='",c=kt((e.escape||y).source+"|"+c.source+"|"+(c===h?g:y).source+"|"+(e.evaluate||y).source+"|$","g");n.replace(c,function(t,r,e,u,o,f){return e||(e=u),v+=n.slice(a,f).replace(d,D),r&&(v+="'+__e("+r+")+'"),o&&(i=!0,v+="';"+o+";__p+='"),e&&(v+="'+((__t=("+e+"))==null?'':__t)+'"),a=f+t.length,t}),v+="';\n",c=e=e.variable,c||(e="obj",v="with("+e+"){"+v+"}"),v=(i?v.replace(l,""):v).replace(p,"$1").replace(s,"$1;"),v="function("+e+"){"+(c?"":e+"||("+e+"={});")+"var __t,__p='',__e=_.escape"+(i?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+v+"return __p}"; -try{var m=xt(u,"return "+v).apply(r,f)}catch(_){throw _.source=v,_}return t?m(t):(m.source=v,m)},o.unescape=function(n){return null==n?"":(n+"").replace(c,T)},o.uniqueId=function(n){var t=++i;return(null==n?"":n+"")+t},o.all=nt,o.any=at,o.detect=rt,o.foldl=it,o.foldr=ft,o.include=Z,o.inject=it,tr(o,function(n,t){o.prototype[t]||(o.prototype[t]=function(){var t=[this.__wrapped__];return Pt.apply(t,arguments),n.apply(o,t)})}),o.first=ct,o.last=function(n,t,r){if(n){var e=0,u=n.length;if(typeof t!="number"&&null!=t){var o=u; -for(t=F(t,r);o--&&t(n[o],o,n);)e++}else if(e=t,null==e||r)return n[u-1];return P(n,Jt(0,u-e))}},o.take=ct,o.head=ct,tr(o,function(n,t){o.prototype[t]||(o.prototype[t]=function(t,r){var e=n(this.__wrapped__,t,r);return null==t||r&&typeof t!="function"?e:new o(e)})}),o.VERSION="1.0.1",o.prototype.toString=function(){return this.__wrapped__+""},o.prototype.value=_t,o.prototype.valueOf=_t,Zt(["join","pop","shift"],function(n){var t=Nt[n];o.prototype[n]=function(){return t.apply(this.__wrapped__,arguments) -}}),Zt(["push","reverse","sort","unshift"],function(n){var t=Nt[n];o.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this}}),Zt(["concat","slice","splice"],function(n){var t=Nt[n];o.prototype[n]=function(){return new o(t.apply(this.__wrapped__,arguments))}}),o}var r,e=typeof exports=="object"&&exports,u=typeof module=="object"&&module&&module.exports==e&&module,o=typeof global=="object"&&global;o.global===o&&(n=o);var i=0,f={},a=30,c=/&(?:amp|lt|gt|quot|#39);/g,l=/\b__p\+='';/g,p=/\b(__p\+=)''\+/g,s=/(__e\(.*?\)|\b__t\))\+'';/g,v=/\w*$/,g=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,h=/<%=([\s\S]+?)%>/g,y=/($^)/,m=/[&<>"']/g,d=/['\n\r\t\u2028\u2029\\]/g,_="[object Arguments]",b="[object Array]",j="[object Boolean]",w="[object Date]",x="[object Number]",O="[object Object]",S="[object RegExp]",A="[object String]",k={"[object Function]":!1}; +;(function(n){function t(e){function o(n){return n&&typeof n=="object"&&n.__wrapped__?n:this instanceof o?(this.__wrapped__=n,void 0):new o(n)}function I(n,t,r){t||(t=0);var e=n.length,u=e-t>=(r||a);if(u){var o={};for(r=t-1;++rt||typeof n=="undefined")return 1; +if(nr?0:r);++er?Qt(0,u+r):r)||0,typeof u=="number"?o=-1<(Y(n)?n.indexOf(t,r):vt(n,t,r)):tr(n,function(n){return++eu&&(u=i)}}else t=!t&&Y(n)?q:R(t,r),tr(n,function(n,r,o){r=t(n,r,o),r>e&&(e=r,u=n)});return u}function at(n,t,r,e){var u=3>arguments.length;if(t=R(t,e,4),ur(n)){var o=-1,i=n.length;for(u&&(r=n[++o]);++oarguments.length;if(typeof u!="number")var i=or(n),u=i.length;return t=R(t,e,4),ot(n,function(e,f,a){f=i?i[--u]:--u,r=o?(o=!1,n[f]):t(r,n[f],f,a)}),r}function lt(n,t,r){var e;if(t=R(t,r),ur(n)){r=-1;for(var u=n.length;++rr?Qt(0,u+r):r||0)-1;else if(r)return e=ht(n,t),n[e]===t?e:-1;for(;++e>>1,r(n[e])vt(f,l))&&((r||a)&&f.push(l),i.push(e))}return i}function mt(n,t){return Yt||Vt&&2/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:h,variable:"",imports:{_:o}};var xt={a:"q,w,h",l:"var a=arguments,b=0,c=typeof h=='number'?2:a.length;while(++b":">",'"':""","'":"'"},fr=J(ir),ar=D(xt,{l:xt.l.replace(";",";if(c>3&&typeof a[c-2]=='function'){var d=f(a[--c-1],a[c--],2);}else if(c>2&&typeof a[c-1]=='function'){d=a[--c];}"),h:"u[j]=d?d(u[j],n[j]):n[j]"}),cr=D(xt),lr=function(n){if(!n||typeof n!="object")return!1;var t=n.valueOf,r=typeof t=="function"&&(r=Pt(t))&&Pt(r);if(r)n=n==r||Pt(n)==r&&!U(n);else{var e=!1;!n||typeof n!="object"||U(n)?n=e:(t=n.constructor,!Q(t)||t instanceof t?(rr(n,function(n,t){e=t +}),n=!1===e||Tt.call(n,e)):n=e)}return n};return At&&u&&typeof setImmediate=="function"&&(dt=mt(setImmediate,e)),o.after=function(n,t){return 1>n?t():function(){return 1>--n?t.apply(this,arguments):void 0}},o.assign=ar,o.at=function(n){for(var t=-1,r=zt.apply(It,K(arguments,1)),e=r.length,u=wt(e);++tvt(a,c)){i&&a.push(c);for(var p=r;--p;)if(!(e[p]||(e[p]=I(t[p],0,100)))(c))continue n;f.push(c)}}return f},o.invert=J,o.invoke=function(n,t){var r=K(arguments,2),e=-1,u=typeof t=="function",o=n?n.length:0,i=wt(typeof o=="number"?o:0); +return ot(n,function(n){i[++e]=(u?t:n[t]).apply(n,r)}),i},o.keys=or,o.map=it,o.max=ft,o.memoize=function(n,t){var r={};return function(){var e=(t?t.apply(this,arguments):arguments[0])+"";return Tt.call(r,e)?r[e]:r[e]=n.apply(this,arguments)}},o.merge=Z,o.min=function(n,t,r){var e=1/0,u=e;if(!t&&ur(n)){r=-1;for(var o=n.length;++rvt(o,r,1))&&(u[r]=n)}),u},o.once=function(n){var t,r;return function(){return t?r:(t=!0,r=n.apply(this,arguments),n=null,r)}},o.pairs=function(n){for(var t=-1,r=or(n),e=r.length,u=wt(e);++tr?Qt(0,e+r):Wt(r,e-1))+1);e--;)if(n[e]===t)return e;return-1},o.mixin=bt,o.noConflict=function(){return e._=Bt,this},o.random=function(n,t){return null==n&&null==t&&(t=1),n=+n||0,null==t&&(t=n,n=0),n+Ct(Xt()*((+t||0)-n+1))},o.reduce=at,o.reduceRight=ct,o.result=function(n,t){var e=n?n[t]:r;return Q(e)?n[t]():e},o.runInContext=t,o.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:or(n).length},o.some=lt,o.sortedIndex=ht,o.template=function(n,t,e){var u=o.templateSettings; +n||(n=""),e=cr({},e,u);var i,f=cr({},e.imports,u.imports),u=or(f),f=nt(f),a=0,c=e.interpolate||y,v="__p+='",c=Nt((e.escape||y).source+"|"+c.source+"|"+(c===h?g:y).source+"|"+(e.evaluate||y).source+"|$","g");n.replace(c,function(t,r,e,u,o,f){return e||(e=u),v+=n.slice(a,f).replace(d,C),r&&(v+="'+__e("+r+")+'"),o&&(i=!0,v+="';"+o+";__p+='"),e&&(v+="'+((__t=("+e+"))==null?'':__t)+'"),a=f+t.length,t}),v+="';\n",c=e=e.variable,c||(e="obj",v="with("+e+"){"+v+"}"),v=(i?v.replace(l,""):v).replace(p,"$1").replace(s,"$1;"),v="function("+e+"){"+(c?"":e+"||("+e+"={});")+"var __t,__p='',__e=_.escape"+(i?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+v+"return __p}"; +try{var m=St(u,"return "+v).apply(r,f)}catch(_){throw _.source=v,_}return t?m(t):(m.source=v,m)},o.unescape=function(n){return null==n?"":(n+"").replace(c,M)},o.uniqueId=function(n){var t=++i;return(null==n?"":n+"")+t},o.all=rt,o.any=lt,o.detect=ut,o.foldl=at,o.foldr=ct,o.include=tt,o.inject=at,er(o,function(n,t){o.prototype[t]||(o.prototype[t]=function(){var t=[this.__wrapped__];return Kt.apply(t,arguments),n.apply(o,t)})}),o.first=pt,o.last=function(n,t,r){if(n){var e=0,u=n.length;if(typeof t!="number"&&null!=t){var o=u; +for(t=R(t,r);o--&&t(n[o],o,n);)e++}else if(e=t,null==e||r)return n[u-1];return K(n,Qt(0,u-e))}},o.take=pt,o.head=pt,er(o,function(n,t){o.prototype[t]||(o.prototype[t]=function(t,r){var e=n(this.__wrapped__,t,r);return null==t||r&&typeof t!="function"?e:new o(e)})}),o.VERSION="1.0.1",o.prototype.toString=function(){return this.__wrapped__+""},o.prototype.value=jt,o.prototype.valueOf=jt,tr(["join","pop","shift"],function(n){var t=It[n];o.prototype[n]=function(){return t.apply(this.__wrapped__,arguments) +}}),tr(["push","reverse","sort","unshift"],function(n){var t=It[n];o.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this}}),tr(["concat","slice","splice"],function(n){var t=It[n];o.prototype[n]=function(){return new o(t.apply(this.__wrapped__,arguments))}}),o}var r,e=typeof exports=="object"&&exports,u=typeof module=="object"&&module&&module.exports==e&&module,o=typeof global=="object"&&global;o.global===o&&(n=o);var i=0,f={},a=30,c=/&(?:amp|lt|gt|quot|#39);/g,l=/\b__p\+='';/g,p=/\b(__p\+=)''\+/g,s=/(__e\(.*?\)|\b__t\))\+'';/g,v=/\w*$/,g=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,h=/<%=([\s\S]+?)%>/g,y=/($^)/,m=/[&<>"']/g,d=/['\n\r\t\u2028\u2029\\]/g,_="[object Arguments]",b="[object Array]",j="[object Boolean]",w="[object Date]",x="[object Number]",O="[object Object]",S="[object RegExp]",A="[object String]",k={"[object Function]":!1}; k[_]=k[b]=k[j]=k[w]=k[x]=k[O]=k[S]=k[A]=!0;var E={"boolean":!1,"function":!0,object:!0,number:!1,string:!1,undefined:!1},N={"\\":"\\","'":"'","\n":"n","\r":"r"," ":"t","\u2028":"u2028","\u2029":"u2029"},$=t();typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=$,define(function(){return $})):e?u?(u.exports=$)._=$:e._=$:n._=$})(this); \ No newline at end of file diff --git a/dist/lodash.underscore.js b/dist/lodash.underscore.js index 24f5fe269..944ab58bd 100644 --- a/dist/lodash.underscore.js +++ b/dist/lodash.underscore.js @@ -333,10 +333,8 @@ : partialArgs; } if (this instanceof bound) { - // ensure `new bound` is an instance of `bound` and `func` - noop.prototype = func.prototype; - thisBinding = new noop; - noop.prototype = null; + // ensure `new bound` is an instance of `func` + thisBinding = createObject(func.prototype); // mimic the constructor's `return` behavior // http://es5.github.com/#x13.2.2 @@ -406,6 +404,20 @@ return func; } + /** + * Creates a new object that inherits from the given `prototype` object. + * + * @private + * @param {Object} prototype The prototype object. + * @returns {Object} Returns the new object. + */ + function createObject(prototype) { + noop.prototype = prototype; + var result = new noop; + noop.prototype = null; + return result; + } + /** * A function compiled to iterate `arguments` objects, arrays, objects, and * strings consistenly across environments, executing the `callback` for each diff --git a/lodash.js b/lodash.js index abab37c6e..dc20d84c5 100644 --- a/lodash.js +++ b/lodash.js @@ -122,7 +122,7 @@ * @returns {Function} Returns the `lodash` function. */ function runInContext(context) { - context || (context = window); + context = context ? _.extend(createObject(window), context) : window; /** Native constructor references */ var Array = context.Array, @@ -630,10 +630,8 @@ : partialArgs; } if (this instanceof bound) { - // ensure `new bound` is an instance of `bound` and `func` - noop.prototype = func.prototype; - thisBinding = new noop; - noop.prototype = null; + // ensure `new bound` is an instance of `func` + thisBinding = createObject(func.prototype); // mimic the constructor's `return` behavior // http://es5.github.com/#x13.2.2 @@ -757,6 +755,20 @@ ); } + /** + * Creates a new object that inherits from the given `prototype` object. + * + * @private + * @param {Object} prototype The prototype object. + * @returns {Object} Returns the new object. + */ + function createObject(prototype) { + noop.prototype = prototype; + var result = new noop; + noop.prototype = null; + return result; + } + /** * A function compiled to iterate `arguments` objects, arrays, objects, and * strings consistenly across environments, executing the `callback` for each @@ -5172,7 +5184,7 @@ /*--------------------------------------------------------------------------*/ // expose Lo-Dash - var lodash = runInContext(); + var _ = runInContext(); // 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) { @@ -5180,27 +5192,27 @@ // case Lo-Dash was injected by a third-party script and not intended to be // loaded as a module. The global assignment can be reverted in the Lo-Dash // module via its `noConflict()` method. - window._ = lodash; + window._ = _; // define as an anonymous module so, through path mapping, it can be // referenced as the "underscore" module define(function() { - return lodash; + return _; }); } // check for `exports` after `define` in case a build optimizer adds an `exports` object else if (freeExports) { // in Node.js or RingoJS v0.8.0+ if (freeModule) { - (freeModule.exports = lodash)._ = lodash; + (freeModule.exports = _)._ = _; } // in Narwhal or RingoJS v0.7.0- else { - freeExports._ = lodash; + freeExports._ = _; } } else { // in a browser or Rhino - window._ = lodash; + window._ = _; } }(this)); diff --git a/test/test.js b/test/test.js index 8ad6f19cb..545b3c437 100644 --- a/test/test.js +++ b/test/test.js @@ -2114,6 +2114,25 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.runInContext'); + + (function() { + test('should not require a fully populated `context` object', function() { + var pass = false; + + var lodash = _.runInContext({ + 'setTimeout': function(callback) { + callback(); + } + }); + + lodash.delay(function() { pass = true; }, 32); + ok(pass); + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.shuffle'); (function() { @@ -2406,26 +2425,11 @@ var callCount = 0, dateCount = 0; - var context = { - 'Array': Array, - 'Boolean': Boolean, - 'Function': Function, - 'Object': Object, - 'Math': Math, - 'Number': Number, - 'RegExp': RegExp, - 'String': String, - 'clearTimeout': clearTimeout, - 'isFinite': isFinite, - 'isNaN': isNaN, - 'setTimeout': setTimeout - }; - - var lodash = _.runInContext(_.extend(context, { + var lodash = _.runInContext({ 'Date': function() { return ++dateCount < 3 ? new Date : Object(Infinity); } - })); + }); var throttled = lodash.throttle(function() { callCount++;