From de8e6d717443f97bf72d6d404c7e69f2bb3ff3a7 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 19 Oct 2013 01:08:09 -0700 Subject: [PATCH] Add `_.create`. --- dist/lodash.compat.js | 126 +++++++++++++++++++++------------- dist/lodash.compat.min.js | 52 +++++++------- dist/lodash.js | 126 +++++++++++++++++++++------------- dist/lodash.min.js | 66 +++++++++--------- dist/lodash.underscore.js | 83 ++++++++++++++-------- dist/lodash.underscore.min.js | 42 ++++++------ lodash.js | 124 ++++++++++++++++++++------------- 7 files changed, 364 insertions(+), 255 deletions(-) diff --git a/dist/lodash.compat.js b/dist/lodash.compat.js index 250513b7f..0ba5896f4 100644 --- a/dist/lodash.compat.js +++ b/dist/lodash.compat.js @@ -1263,7 +1263,7 @@ // non `Object` object instances with different constructors are not equal if (ctorA != ctorB && !(isFunction(ctorA) && ctorA instanceof ctorA && isFunction(ctorB) && ctorB instanceof ctorB) && - (!nativeCreate || ('constructor' in a && 'constructor' in b)) + ('constructor' in a && 'constructor' in b) ) { return false; } @@ -1603,7 +1603,7 @@ } if (this instanceof bound) { // ensure `new bound` is an instance of `func` - thisBinding = createObject(func.prototype); + thisBinding = create(func.prototype); // mimic the constructor's `return` behavior // http://es5.github.io/#x13.2.2 @@ -1665,28 +1665,6 @@ ); } - /** - * Creates a new object with the specified `prototype`. - * - * @private - * @param {Object} prototype The prototype object. - * @returns {Object} Returns the new object. - */ - function createObject(prototype) { - return isObject(prototype) ? nativeCreate(prototype) : {}; - } - // fallback for browsers without `Object.create` - if (!nativeCreate) { - createObject = function(prototype) { - if (isObject(prototype)) { - noop.prototype = prototype; - var result = new noop; - noop.prototype = null; - } - return result || {}; - }; - } - /** * Used by `escape` to convert characters to HTML entities. * @@ -2076,6 +2054,50 @@ return baseClone(value, true, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1)); } + /** + * Creates a new object with the specified `prototype`. + * + * @static + * @memberOf _ + * @category Objects + * @param {Object} prototype The prototype object. + * @returns {Object} Returns the new object. + * @example + * + * function Shape() { + * this.x = 0; + * this.y = 0; + * } + * + * function Circle() { + * Shape.call(this); + * } + * + * Circle.prototype = _.create(Shape.prototype); + * Circle.prototype.constructor = Circle; + * + * var circle = new Circle; + * circle instanceof Circle + * // => true + * + * circle instanceof Shape + * // => true + */ + function create(prototype) { + return isObject(prototype) ? nativeCreate(prototype) : {}; + } + // fallback for browsers without `Object.create` + if (!nativeCreate) { + create = function(prototype) { + if (isObject(prototype)) { + noop.prototype = prototype; + var result = new noop; + noop.prototype = null; + } + return result || {}; + }; + } + /** * Assigns own enumerable properties of source object(s) to the destination * object for all destination properties that resolve to `undefined`. Once a @@ -2220,18 +2242,20 @@ * @returns {Object} Returns `object`. * @example * - * function Dog(name) { - * this.name = name; + * function Shape() { + * this.x = 0; + * this.y = 0; * } * - * Dog.prototype.bark = function() { - * console.log('Woof, woof!'); + * Shape.prototype.move = function(x, y) { + * this.x += x; + * this.y += y; * }; * - * _.forIn(new Dog('Dagny'), function(value, key) { + * _.forIn(new Shape, function(value, key) { * console.log(key); * }); - * // => logs 'bark' and 'name' (property order is not guaranteed across environments) + * // => logs 'x', 'y', and 'move' (property order is not guaranteed across environments) */ var forIn = createIterator(eachIteratorOptions, forOwnIteratorOptions, { 'useHas': false @@ -2250,18 +2274,20 @@ * @returns {Object} Returns `object`. * @example * - * function Dog(name) { - * this.name = name; + * function Shape() { + * this.x = 0; + * this.y = 0; * } * - * Dog.prototype.bark = function() { - * console.log('Woof, woof!'); + * Shape.prototype.move = function(x, y) { + * this.x += x; + * this.y += y; * }; * - * _.forInRight(new Dog('Dagny'), function(value, key) { + * _.forInRight(new Shape, function(value, key) { * console.log(key); * }); - * // => logs 'name' and 'bark' assuming `_.forIn ` logs 'bark' and 'name' + * // => logs 'move', 'y', and 'x' assuming `_.forIn ` logs 'x', 'y', and 'move' */ function forInRight(object, callback, thisArg) { var pairs = []; @@ -2703,18 +2729,18 @@ * @returns {boolean} Returns `true` if `value` is a plain object, else `false`. * @example * - * function Person(name, age) { - * this.name = name; - * this.age = age; + * function Shape() { + * this.x = 0; + * this.y = 0; * } * - * _.isPlainObject(new Person('fred', 40)); + * _.isPlainObject(new Shape); * // => false * * _.isPlainObject([1, 2, 3]); * // => false * - * _.isPlainObject({ 'name': 'fred', 'age': 40 }); + * _.isPlainObject({ 'x': 0, 'y': 0 }); * // => true */ var isPlainObject = !getPrototypeOf ? shimIsPlainObject : function(value) { @@ -2998,7 +3024,7 @@ * @static * @memberOf _ * @category Objects - * @param {Array|Object} collection The collection to iterate over. + * @param {Array|Object} object The object to iterate over. * @param {Function} [callback=identity] The function called per iteration. * @param {*} [accumulator] The custom accumulator value. * @param {*} [thisArg] The `this` binding of `callback`. @@ -3020,8 +3046,6 @@ */ function transform(object, callback, accumulator, thisArg) { var isArr = isArray(object); - callback = baseCreateCallback(callback, thisArg, 4); - if (accumulator == null) { if (isArr) { accumulator = []; @@ -3029,12 +3053,15 @@ var ctor = object && object.constructor, proto = ctor && ctor.prototype; - accumulator = createObject(proto); + accumulator = create(proto); } } - (isArr ? baseEach : forOwn)(object, function(value, index, object) { - return callback(accumulator, value, index, object); - }); + if (callback) { + callback = baseCreateCallback(callback, thisArg, 4); + (isArr ? baseEach : forOwn)(object, function(value, index, object) { + return callback(accumulator, value, index, object); + }); + } return accumulator; } @@ -6036,7 +6063,7 @@ * // => 8 */ var parseInt = nativeParseInt(whitespace + '08') == 8 ? nativeParseInt : function(value, radix) { - // Firefox and Opera still follow the ES3 specified implementation of `parseInt` + // Firefox < 21 and Opera < 15 follow the ES3 specified implementation of `parseInt` return nativeParseInt(isString(value) ? value.replace(reLeadingSpacesAndZeros, '') : value, radix || 0); }; @@ -6528,6 +6555,7 @@ lodash.compact = compact; lodash.compose = compose; lodash.countBy = countBy; + lodash.create = create; lodash.createCallback = createCallback; lodash.curry = curry; lodash.debounce = debounce; diff --git a/dist/lodash.compat.min.js b/dist/lodash.compat.min.js index 32d2f1d18..53acb1c42 100644 --- a/dist/lodash.compat.min.js +++ b/dist/lodash.compat.min.js @@ -8,47 +8,47 @@ }function f(n){return typeof n.toString!="function"&&typeof(n+"")=="string"}function c(){}function p(n){n.length=0,y.lengthe?0:e);++r=w&&l===n,h=u||g?i():c;if(g){var v=o(h);v?(l=t,h=v):(g=!1,h=u?h:(p(h),c))}for(;++a=w&&l===n,h=u||g?i():c;if(g){var v=o(h);v?(l=t,h=v):(g=!1,h=u?h:(p(h),c))}for(;++al(h,y))&&((u||g)&&h.push(y),c.push(v))}return g?(p(h.k),s(h)):u&&p(h),c}function lt(n){return function(t,e,r){var u={};if(e=y.createCallback(e,r,3),Je(t)){r=-1;for(var o=t.length;++rk;k++)r+="n='"+e.h[k]+"';if((!(r&&x[n])&&m.call(t,n))",e.j||(r+="||(!x[n]&&t[n]!==A[n])"),r+="){"+e.g+"}";r+="}"}return(e.b||We.nonEnumArgs)&&(r+="}"),r+=e.c+";return E",n("d,j,k,m,o,p,q,s,v,A,B,y,I,J,L",t+r+"}")(Z,K,le,de,_,yt,Je,xt,X.f,fe,Y,Ke,H,ce,ke) -}function pt(n){return wt(n)?Ie(n):{}}function st(n){return Xe[n]}function gt(){var t=(t=y.indexOf)===Tt?n:t;return t}function ht(n){var t,e;return!n||ke.call(n)!=J||(t=n.constructor,bt(t)&&!(t instanceof t))||!We.argsClass&&yt(n)||!We.nodeClass&&f(n)?!1:We.ownLast?(ur(n,function(n,t,r){return e=de.call(r,t),!1}),false!==e):(ur(n,function(n,t){e=t}),typeof e=="undefined"||de.call(n,e))}function vt(n){return Ye[n]}function yt(n){return n&&typeof n=="object"&&typeof n.length=="number"&&ke.call(n)==L||!1 +else if(r+="for(n in t){",e.j&&u.push("m.call(t, n)"),u.length&&(r+="if("+u.join("&&")+"){"),r+=e.g+";",u.length&&(r+="}"),r+="}",We.nonEnumShadows){for(r+="if(t!==A){var i=t.constructor,r=t===(i&&i.prototype),f=t===J?I:t===k?j:L.call(t),x=y[f];",k=0;7>k;k++)r+="n='"+e.h[k]+"';if((!(r&&x[n])&&m.call(t,n))",e.j||(r+="||(!x[n]&&t[n]!==A[n])"),r+="){"+e.g+"}";r+="}"}return(e.b||We.nonEnumArgs)&&(r+="}"),r+=e.c+";return E",n("d,j,k,m,o,p,q,s,v,A,B,y,I,J,L",t+r+"}")(Z,K,le,de,_,vt,Je,xt,X.f,fe,Y,Ke,H,ce,ke) +}function pt(n){return Xe[n]}function st(){var t=(t=y.indexOf)===Tt?n:t;return t}function gt(n){var t,e;return!n||ke.call(n)!=J||(t=n.constructor,bt(t)&&!(t instanceof t))||!We.argsClass&&vt(n)||!We.nodeClass&&f(n)?!1:We.ownLast?(ur(n,function(n,t,r){return e=de.call(r,t),!1}),false!==e):(ur(n,function(n,t){e=t}),typeof e=="undefined"||de.call(n,e))}function ht(n){return Ye[n]}function vt(n){return n&&typeof n=="object"&&typeof n.length=="number"&&ke.call(n)==L||!1}function yt(n){return wt(n)?Ie(n):{} }function mt(n,t,e){var r=He(n),u=r.length;for(t=Z(t,e,3);u--&&(e=r[u],false!==t(n[e],e,n)););return n}function dt(n){var t=[];return ur(n,function(n,e){bt(n)&&t.push(e)}),t.sort()}function _t(n){for(var t=-1,e=He(n),r=e.length,u={};++te?Pe(0,o+e):e)||0,Je(n)?a=-1e?Pe(0,o+e):e)||0,Je(n)?a=-1o&&(o=i)}}else t=!t&&xt(n)?r:y.createCallback(t,e,3),tr(n,function(n,e,r){e=t(n,e,r),e>u&&(u=e,o=n)});return o }function Nt(n,t,e,r){var u=3>arguments.length;if(t=Z(t,r,4),Je(n)){var o=-1,a=n.length;for(u&&(e=n[++o]);++oarguments.length;return t=Z(t,r,4),At(n,function(n,r,o){e=u?(u=!1,n):t(e,n,r,o)}),e}function Rt(n){var t=-1,e=n?n.length:0,r=Xt(typeof e=="number"?e:0);return It(n,function(n){var e=at(0,++t);r[t]=r[e],r[e]=n}),r}function Ft(n,t,e){var r;if(t=y.createCallback(t,e,3),Je(n)){e=-1;for(var u=n.length;++e=w&&u===n;if(f){var c=o(i);c?(u=t,i=c):f=!1}for(;++ru(i,c)&&l.push(c);return f&&s(i),l}function Lt(n,t,e){var r=0,u=n?n.length:0;if(typeof t!="number"&&null!=t){var o=-1;for(t=y.createCallback(t,e,3);++or?Pe(0,u+r):r||0}else if(r)return r=qt(t,e),t[r]===e?r:-1; +});return!!r}function $t(e){var r=-1,u=st(),a=e?e.length:0,i=tt(arguments,!0,!0,1),l=[],f=a>=w&&u===n;if(f){var c=o(i);c?(u=t,i=c):f=!1}for(;++ru(i,c)&&l.push(c);return f&&s(i),l}function Lt(n,t,e){var r=0,u=n?n.length:0;if(typeof t!="number"&&null!=t){var o=-1;for(t=y.createCallback(t,e,3);++or?Pe(0,u+r):r||0}else if(r)return r=qt(t,e),t[r]===e?r:-1; return n(t,e,r)}function zt(n,t,e){if(typeof t!="number"&&null!=t){var r=0,u=-1,o=n?n.length:0;for(t=y.createCallback(t,e,3);++u>>1,e(n[r])e?0:e);++t/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:B,variable:"",imports:{_:y}},Ie||(pt=function(n){if(wt(n)){c.prototype=n;var t=new c;c.prototype=null}return t||{}});var Ge=Oe?function(n,t){Q.value=t,Oe(n,"__bindData__",Q)}:c;We.argsClass||(yt=function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&de.call(n,"callee")||!1});var Je=Ae||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&ke.call(n)==T||!1 -},Me=ct({a:"z",e:"[]",i:"if(!(B[typeof z]))return E",g:"E.push(n)"}),He=Ne?function(n){return wt(n)?We.enumPrototypes&&typeof n=="function"||We.nonEnumArgs&&n.length&&yt(n)?Me(n):Ne(n):[]}:Me,Ue={a:"g,e,K",i:"e=e&&typeof K=='undefined'?e:d(e,K,3)",b:"typeof u=='number'",v:He,g:"if(e(t[n],n,g)===false)return E"},Ve={a:"z,H,l",i:"var a=arguments,b=0,c=typeof l=='number'?2:a.length;while(++b":">",'"':""","'":"'"},Ye=_t(Xe),Ze=ue("("+He(Ye).join("|")+")","g"),nr=ue("["+He(Xe).join("")+"]","g"),tr=ct(Ue),er=ct(Ve,{i:Ve.i.replace(";",";if(c>3&&typeof a[c-2]=='function'){var e=d(a[--c-1],a[c--],2)}else if(c>2&&typeof a[c-1]=='function'){e=a[--c]}"),g:"E[n]=e?e(E[n],t[n]):t[n]"}),rr=ct(Ve),ur=ct(Ue,Qe,{j:!1}),or=ct(Ue,Qe); -bt(/x/)&&(bt=function(n){return typeof n=="function"&&ke.call(n)==W});var ar=me?function(n){if(!n||ke.call(n)!=J||!We.argsClass&&yt(n))return!1;var t=n.valueOf,e=typeof t=="function"&&(e=me(t))&&me(e);return e?n==e||me(n)==e:ht(n)}:ht,ir=lt(function(n,t,e){de.call(n,e)?n[e]++:n[e]=1}),lr=lt(function(n,t,e){(de.call(n,e)?n[e]:n[e]=[]).push(t)}),fr=lt(function(n,t,e){n[e]=t}),cr=Bt;ze&&rt&&typeof je=="function"&&(Ht=function(n){if(!bt(n))throw new ae;return je.apply(e,arguments)});var pr=8==Fe(x+"08")?Fe:function(n,t){return Fe(xt(n)?n.replace(D,""):n,t||0) -};return y.after=function(n,t){if(!bt(t))throw new ae;return function(){return 1>--n?t.apply(this,arguments):void 0}},y.assign=er,y.at=function(n){var t=arguments,e=-1,r=tt(t,!0,!1,1),t=t[2]&&t[2][t[1]]===n?1:r.length,u=Xt(t);for(We.unindexedChars&&xt(n)&&(n=n.split(""));++e=w&&o(a?r[a]:v)}n:for(;++f(m?t(m,y):c(v,y))){for(a=u,(m||v).push(y);--a;)if(m=l[a],0>(m?t(m,y):c(r[a],y)))continue n;h.push(y)}}for(;u--;)(m=l[u])&&s(m);return p(l),p(v),h},y.invert=_t,y.invoke=function(n,t){var e=Le.call(arguments,2),r=-1,u=typeof t=="function",o=n?n.length:0,a=Xt(typeof o=="number"?o:0);return It(n,function(n){a[++r]=(u?t:n[t]).apply(n,e)}),a},y.keys=He,y.map=Bt,y.max=Dt,y.memoize=function(n,t){function e(){var r=e.cache,u=t?t.apply(this,arguments):b+arguments[0]; -return de.call(r,u)?r[u]:r[u]=n.apply(this,arguments)}if(!bt(n))throw new ae;return e.cache={},e},y.merge=function(n){var t=arguments,e=2;if(!wt(n))return n;if("number"!=typeof t[2]&&(e=t.length),3r(a,e))&&(o[e]=n)}),o},y.once=function(n){var t,e;if(!bt(n))throw new ae;return function(){return t?e:(t=!0,e=n.apply(this,arguments),n=null,e)}},y.pairs=function(n){for(var t=-1,e=He(n),r=e.length,u=Xt(r);++t/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:B,variable:"",imports:{_:y}};var Ge=Oe?function(n,t){Q.value=t,Oe(n,"__bindData__",Q)}:c;We.argsClass||(vt=function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&de.call(n,"callee")||!1});var Je=Ae||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&ke.call(n)==T||!1},Me=ct({a:"z",e:"[]",i:"if(!(B[typeof z]))return E",g:"E.push(n)"}),He=Ne?function(n){return wt(n)?We.enumPrototypes&&typeof n=="function"||We.nonEnumArgs&&n.length&&vt(n)?Me(n):Ne(n):[] +}:Me,Ue={a:"g,e,K",i:"e=e&&typeof K=='undefined'?e:d(e,K,3)",b:"typeof u=='number'",v:He,g:"if(e(t[n],n,g)===false)return E"},Ve={a:"z,H,l",i:"var a=arguments,b=0,c=typeof l=='number'?2:a.length;while(++b":">",'"':""","'":"'"},Ye=_t(Xe),Ze=ue("("+He(Ye).join("|")+")","g"),nr=ue("["+He(Xe).join("")+"]","g"),tr=ct(Ue),er=ct(Ve,{i:Ve.i.replace(";",";if(c>3&&typeof a[c-2]=='function'){var e=d(a[--c-1],a[c--],2)}else if(c>2&&typeof a[c-1]=='function'){e=a[--c]}"),g:"E[n]=e?e(E[n],t[n]):t[n]"}); +Ie||(yt=function(n){if(wt(n)){c.prototype=n;var t=new c;c.prototype=null}return t||{}});var rr=ct(Ve),ur=ct(Ue,Qe,{j:!1}),or=ct(Ue,Qe);bt(/x/)&&(bt=function(n){return typeof n=="function"&&ke.call(n)==W});var ar=me?function(n){if(!n||ke.call(n)!=J||!We.argsClass&&vt(n))return!1;var t=n.valueOf,e=typeof t=="function"&&(e=me(t))&&me(e);return e?n==e||me(n)==e:gt(n)}:gt,ir=lt(function(n,t,e){de.call(n,e)?n[e]++:n[e]=1}),lr=lt(function(n,t,e){(de.call(n,e)?n[e]:n[e]=[]).push(t)}),fr=lt(function(n,t,e){n[e]=t +}),cr=Bt;ze&&rt&&typeof je=="function"&&(Ht=function(n){if(!bt(n))throw new ae;return je.apply(e,arguments)});var pr=8==Fe(x+"08")?Fe:function(n,t){return Fe(xt(n)?n.replace(D,""):n,t||0)};return y.after=function(n,t){if(!bt(t))throw new ae;return function(){return 1>--n?t.apply(this,arguments):void 0}},y.assign=er,y.at=function(n){var t=arguments,e=-1,r=tt(t,!0,!1,1),t=t[2]&&t[2][t[1]]===n?1:r.length,u=Xt(t);for(We.unindexedChars&&xt(n)&&(n=n.split(""));++e=w&&o(a?r[a]:v)}n:for(;++f(m?t(m,y):c(v,y))){for(a=u,(m||v).push(y);--a;)if(m=l[a],0>(m?t(m,y):c(r[a],y)))continue n;h.push(y)}}for(;u--;)(m=l[u])&&s(m);return p(l),p(v),h},y.invert=_t,y.invoke=function(n,t){var e=Le.call(arguments,2),r=-1,u=typeof t=="function",o=n?n.length:0,a=Xt(typeof o=="number"?o:0); +return It(n,function(n){a[++r]=(u?t:n[t]).apply(n,e)}),a},y.keys=He,y.map=Bt,y.max=Dt,y.memoize=function(n,t){function e(){var r=e.cache,u=t?t.apply(this,arguments):b+arguments[0];return de.call(r,u)?r[u]:r[u]=n.apply(this,arguments)}if(!bt(n))throw new ae;return e.cache={},e},y.merge=function(n){var t=arguments,e=2;if(!wt(n))return n;if("number"!=typeof t[2]&&(e=t.length),3r(a,e))&&(o[e]=n)}),o},y.once=function(n){var t,e;if(!bt(n))throw new ae;return function(){return t?e:(t=!0,e=n.apply(this,arguments),n=null,e) +}},y.pairs=function(n){for(var t=-1,e=He(n),r=e.length,u=Xt(r);++te?Pe(0,r+e):Re(e,r-1))+1);r--;)if(n[r]===t)return r;return-1},y.mixin=Vt,y.noConflict=function(){return e._=pe,this},y.parseInt=pr,y.random=function(n,t,e){var r=null==n,u=null==t;return null==e&&(typeof n=="boolean"&&u?(e=n,n=1):u||typeof t!="boolean"||(e=t,u=!0)),r&&u&&(t=1),n=+n||0,u?(t=n,n=0):t=+t||0,e||n%1||t%1?(e=$e(),Re(n+e*(t-n+parseFloat("1e-"+((e+"").length-1))),t)):at(n,t) },y.reduce=Nt,y.reduceRight=Pt,y.result=function(n,t){if(n){var e=n[t];return bt(e)?n[t]():e}},y.runInContext=h,y.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:He(n).length},y.some=Ft,y.sortedIndex=qt,y.template=function(n,t,e){var r=y.templateSettings;n=oe(n||""),e=rr({},e,r);var u,o=rr({},e.imports,r.imports),r=He(o),o=Ct(o),i=0,l=e.interpolate||N,f="__p+='",l=ue((e.escape||N).source+"|"+l.source+"|"+(l===B?S:N).source+"|"+(e.evaluate||N).source+"|$","g");n.replace(l,function(t,e,r,o,l,c){return r||(r=o),f+=n.slice(i,c).replace(R,a),e&&(f+="'+__e("+e+")+'"),l&&(u=!0,f+="';"+l+";\n__p+='"),r&&(f+="'+((__t=("+r+"))==null?'':__t)+'"),i=c+t.length,t -}),f+="';",l=e=e.variable,l||(e="obj",f="with("+e+"){"+f+"}"),f=(u?f.replace(C,""):f).replace(E,"$1").replace(O,"$1;"),f="function("+e+"){"+(l?"":e+"||("+e+"={});")+"var __t,__p='',__e=_.escape"+(u?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+f+"return __p}";try{var c=ne(r,"return "+f).apply(v,o)}catch(p){throw p.source=f,p}return t?c(t):(c.source=f,c)},y.unescape=function(n){return null==n?"":oe(n).replace(Ze,vt)},y.uniqueId=function(n){var t=++d;return oe(null==n?"":n)+t +}),f+="';",l=e=e.variable,l||(e="obj",f="with("+e+"){"+f+"}"),f=(u?f.replace(C,""):f).replace(E,"$1").replace(O,"$1;"),f="function("+e+"){"+(l?"":e+"||("+e+"={});")+"var __t,__p='',__e=_.escape"+(u?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+f+"return __p}";try{var c=ne(r,"return "+f).apply(v,o)}catch(p){throw p.source=f,p}return t?c(t):(c.source=f,c)},y.unescape=function(n){return null==n?"":oe(n).replace(Ze,ht)},y.uniqueId=function(n){var t=++d;return oe(null==n?"":n)+t },y.all=Et,y.any=Ft,y.detect=St,y.findWhere=St,y.foldl=Nt,y.foldr=Pt,y.include=kt,y.inject=Nt,or(y,function(n,t){y.prototype[t]||(y.prototype[t]=function(){var t=[this.__wrapped__],e=this.__chain__;return be.apply(t,arguments),t=n.apply(y,t),e?new m(t,e):t})}),y.first=Lt,y.last=function(n,t,e){var r=0,u=n?n.length:0;if(typeof t!="number"&&null!=t){var o=u;for(t=y.createCallback(t,e,3);o--&&t(n[o],o,n);)r++}else if(r=t,null==r||e)return n?n[u-1]:v;return g(n,Pe(0,u-r))},y.sample=function(n,t,e){return n&&typeof n.length!="number"?n=Ct(n):We.unindexedChars&&xt(n)&&(n=n.split("")),null==t||e?n?n[at(0,n.length-1)]:v:(n=Rt(n),n.length=Re(Pe(0,t),n.length),n) },y.take=Lt,y.head=Lt,or(y,function(n,t){var e="sample"!==t;y.prototype[t]||(y.prototype[t]=function(t,r){var u=this.__chain__,o=n(this.__wrapped__,t,r);return u||null!=t&&(!r||e&&typeof t=="function")?new m(o,u):o})}),y.VERSION="2.2.1",y.prototype.chain=function(){return this.__chain__=!0,this},y.prototype.toString=function(){return oe(this.__wrapped__)},y.prototype.value=Qt,y.prototype.valueOf=Qt,tr(["join","pop","shift"],function(n){var t=ie[n];y.prototype[n]=function(){var n=this.__chain__,e=t.apply(this.__wrapped__,arguments); return n?new m(e,n):e}}),tr(["push","reverse","sort","unshift"],function(n){var t=ie[n];y.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this}}),tr(["concat","slice","splice"],function(n){var t=ie[n];y.prototype[n]=function(){return new m(t.apply(this.__wrapped__,arguments),this.__chain__)}}),We.spliceObjects||tr(["pop","shift","splice"],function(n){var t=ie[n],e="splice"==n;y.prototype[n]=function(){var n=this.__chain__,r=this.__wrapped__,u=t.apply(r,arguments);return 0===r.length&&delete r[0],n||e?new m(u,n):u diff --git a/dist/lodash.js b/dist/lodash.js index ded54e4a5..e23630c42 100644 --- a/dist/lodash.js +++ b/dist/lodash.js @@ -987,7 +987,7 @@ // non `Object` object instances with different constructors are not equal if (ctorA != ctorB && !(isFunction(ctorA) && ctorA instanceof ctorA && isFunction(ctorB) && ctorB instanceof ctorB) && - (!nativeCreate || ('constructor' in a && 'constructor' in b)) + ('constructor' in a && 'constructor' in b) ) { return false; } @@ -1327,7 +1327,7 @@ } if (this instanceof bound) { // ensure `new bound` is an instance of `func` - thisBinding = createObject(func.prototype); + thisBinding = create(func.prototype); // mimic the constructor's `return` behavior // http://es5.github.io/#x13.2.2 @@ -1341,28 +1341,6 @@ return bound; } - /** - * Creates a new object with the specified `prototype`. - * - * @private - * @param {Object} prototype The prototype object. - * @returns {Object} Returns the new object. - */ - function createObject(prototype) { - return isObject(prototype) ? nativeCreate(prototype) : {}; - } - // fallback for browsers without `Object.create` - if (!nativeCreate) { - createObject = function(prototype) { - if (isObject(prototype)) { - noop.prototype = prototype; - var result = new noop; - noop.prototype = null; - } - return result || {}; - }; - } - /** * Used by `escape` to convert characters to HTML entities. * @@ -1702,6 +1680,50 @@ return baseClone(value, true, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1)); } + /** + * Creates a new object with the specified `prototype`. + * + * @static + * @memberOf _ + * @category Objects + * @param {Object} prototype The prototype object. + * @returns {Object} Returns the new object. + * @example + * + * function Shape() { + * this.x = 0; + * this.y = 0; + * } + * + * function Circle() { + * Shape.call(this); + * } + * + * Circle.prototype = _.create(Shape.prototype); + * Circle.prototype.constructor = Circle; + * + * var circle = new Circle; + * circle instanceof Circle + * // => true + * + * circle instanceof Shape + * // => true + */ + function create(prototype) { + return isObject(prototype) ? nativeCreate(prototype) : {}; + } + // fallback for browsers without `Object.create` + if (!nativeCreate) { + create = function(prototype) { + if (isObject(prototype)) { + noop.prototype = prototype; + var result = new noop; + noop.prototype = null; + } + return result || {}; + }; + } + /** * Assigns own enumerable properties of source object(s) to the destination * object for all destination properties that resolve to `undefined`. Once a @@ -1866,18 +1888,20 @@ * @returns {Object} Returns `object`. * @example * - * function Dog(name) { - * this.name = name; + * function Shape() { + * this.x = 0; + * this.y = 0; * } * - * Dog.prototype.bark = function() { - * console.log('Woof, woof!'); + * Shape.prototype.move = function(x, y) { + * this.x += x; + * this.y += y; * }; * - * _.forIn(new Dog('Dagny'), function(value, key) { + * _.forIn(new Shape, function(value, key) { * console.log(key); * }); - * // => logs 'bark' and 'name' (property order is not guaranteed across environments) + * // => logs 'x', 'y', and 'move' (property order is not guaranteed across environments) */ var forIn = function(collection, callback, thisArg) { var index, iterable = collection, result = iterable; @@ -1903,18 +1927,20 @@ * @returns {Object} Returns `object`. * @example * - * function Dog(name) { - * this.name = name; + * function Shape() { + * this.x = 0; + * this.y = 0; * } * - * Dog.prototype.bark = function() { - * console.log('Woof, woof!'); + * Shape.prototype.move = function(x, y) { + * this.x += x; + * this.y += y; * }; * - * _.forInRight(new Dog('Dagny'), function(value, key) { + * _.forInRight(new Shape, function(value, key) { * console.log(key); * }); - * // => logs 'name' and 'bark' assuming `_.forIn ` logs 'bark' and 'name' + * // => logs 'move', 'y', and 'x' assuming `_.forIn ` logs 'x', 'y', and 'move' */ function forInRight(object, callback, thisArg) { var pairs = []; @@ -2363,18 +2389,18 @@ * @returns {boolean} Returns `true` if `value` is a plain object, else `false`. * @example * - * function Person(name, age) { - * this.name = name; - * this.age = age; + * function Shape() { + * this.x = 0; + * this.y = 0; * } * - * _.isPlainObject(new Person('fred', 40)); + * _.isPlainObject(new Shape); * // => false * * _.isPlainObject([1, 2, 3]); * // => false * - * _.isPlainObject({ 'name': 'fred', 'age': 40 }); + * _.isPlainObject({ 'x': 0, 'y': 0 }); * // => true */ var isPlainObject = function(value) { @@ -2658,7 +2684,7 @@ * @static * @memberOf _ * @category Objects - * @param {Array|Object} collection The collection to iterate over. + * @param {Array|Object} object The object to iterate over. * @param {Function} [callback=identity] The function called per iteration. * @param {*} [accumulator] The custom accumulator value. * @param {*} [thisArg] The `this` binding of `callback`. @@ -2680,8 +2706,6 @@ */ function transform(object, callback, accumulator, thisArg) { var isArr = isArray(object); - callback = baseCreateCallback(callback, thisArg, 4); - if (accumulator == null) { if (isArr) { accumulator = []; @@ -2689,12 +2713,15 @@ var ctor = object && object.constructor, proto = ctor && ctor.prototype; - accumulator = createObject(proto); + accumulator = create(proto); } } - (isArr ? forEach : forOwn)(object, function(value, index, object) { - return callback(accumulator, value, index, object); - }); + if (callback) { + callback = baseCreateCallback(callback, thisArg, 4); + (isArr ? forEach : forOwn)(object, function(value, index, object) { + return callback(accumulator, value, index, object); + }); + } return accumulator; } @@ -5697,7 +5724,7 @@ * // => 8 */ var parseInt = nativeParseInt(whitespace + '08') == 8 ? nativeParseInt : function(value, radix) { - // Firefox and Opera still follow the ES3 specified implementation of `parseInt` + // Firefox < 21 and Opera < 15 follow the ES3 specified implementation of `parseInt` return nativeParseInt(isString(value) ? value.replace(reLeadingSpacesAndZeros, '') : value, radix || 0); }; @@ -6189,6 +6216,7 @@ lodash.compact = compact; lodash.compose = compose; lodash.countBy = countBy; + lodash.create = create; lodash.createCallback = createCallback; lodash.curry = curry; lodash.debounce = debounce; diff --git a/dist/lodash.min.js b/dist/lodash.min.js index 27c3830a6..31d15731a 100644 --- a/dist/lodash.min.js +++ b/dist/lodash.min.js @@ -5,50 +5,50 @@ */ ;(function(){function n(n,t,e){e=(e||0)-1;for(var r=n?n.length:0;++er||typeof e=="undefined")return 1;if(ee?0:e);++re?0:e);++r=b&&f===n,v=u||h?i():s;if(h){var g=o(v);g?(f=t,v=g):(h=!1,v=u?v:(c(v),s))}for(;++af(v,y))&&((u||h)&&v.push(y),s.push(g)) -}return h?(c(v.k),p(v)):u&&c(v),s}function ft(n){return function(t,e,r){var u={};e=Y.createCallback(e,r,3),r=-1;var o=t?t.length:0;if(typeof o=="number")for(;++re?Re(0,o+e):e)||0,Pe(n)?a=-1=b&&f===n,h=u||v?i():s;if(v){var g=o(h);g?(f=t,h=g):(v=!1,h=u?h:(c(h),s))}for(;++af(h,y))&&((u||v)&&h.push(y),s.push(g)) +}return v?(c(h.k),p(h)):u&&c(h),s}function ft(n){return function(t,e,r){var u={};e=Y.createCallback(e,r,3),r=-1;var o=t?t.length:0;if(typeof o=="number")for(;++re?Re(0,o+e):e)||0,Pe(n)?a=-1o&&(o=i)}}else t=!t&&jt(n)?r:Y.createCallback(t,e,3),Nt(n,function(n,e,r){e=t(n,e,r),e>u&&(u=e,o=n)});return o}function At(n,t){var e=-1,r=n?n.length:0;if(typeof r=="number")for(var u=Xt(r);++earguments.length;t=et(t,r,4);var o=-1,a=n.length;if(typeof a=="number")for(u&&(e=n[++o]);++oarguments.length;return t=et(t,r,4),Et(n,function(n,r,o){e=u?(u=!1,n):t(e,n,r,o)}),e}function $t(n){var t=-1,e=n?n.length:0,r=Xt(typeof e=="number"?e:0);return Nt(n,function(n){var e=at(0,++t);r[t]=r[e],r[e]=n}),r}function Ft(n,t,e){var r;t=Y.createCallback(t,e,3),e=-1;var u=n?n.length:0;if(typeof u=="number")for(;++e=b&&u===n; -if(l){var c=o(i);c?(u=t,i=c):l=!1}for(;++ru(i,c)&&f.push(c);return l&&p(i),f}function Wt(n,t,e){var r=0,u=n?n.length:0;if(typeof t!="number"&&null!=t){var o=-1;for(t=Y.createCallback(t,e,3);++or?Re(0,u+r):r||0}else if(r)return r=Pt(t,e),t[r]===e?r:-1;return n(t,e,r)}function zt(n,t,e){if(typeof t!="number"&&null!=t){var r=0,u=-1,o=n?n.length:0; +else y(n,function(n,r,o){e=u?(u=!1,n):t(e,n,r,o)});return e}function Bt(n,t,e,r){var u=3>arguments.length;return t=et(t,r,4),Et(n,function(n,r,o){e=u?(u=!1,n):t(e,n,r,o)}),e}function $t(n){var t=-1,e=n?n.length:0,r=Xt(typeof e=="number"?e:0);return Nt(n,function(n){var e=at(0,++t);r[t]=r[e],r[e]=n}),r}function Ft(n,t,e){var r;t=Y.createCallback(t,e,3),e=-1;var u=n?n.length:0;if(typeof u=="number")for(;++e=b&&u===n; +if(l){var c=o(i);c?(u=t,i=c):l=!1}for(;++ru(i,c)&&f.push(c);return l&&p(i),f}function Wt(n,t,e){var r=0,u=n?n.length:0;if(typeof t!="number"&&null!=t){var o=-1;for(t=Y.createCallback(t,e,3);++or?Re(0,u+r):r||0}else if(r)return r=Pt(t,e),t[r]===e?r:-1;return n(t,e,r)}function zt(n,t,e){if(typeof t!="number"&&null!=t){var r=0,u=-1,o=n?n.length:0; for(t=Y.createCallback(t,e,3);++u>>1,e(n[r])e?0:e);++t/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:N,variable:"",imports:{_:Y}},Oe||(ct=function(n){if(dt(n)){l.prototype=n;var t=new l;l.prototype=null}return t||{}});var ze=xe?function(n,t){U.value=t,xe(n,"__bindData__",U)}:l,Pe=Ie||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&je.call(n)==$||!1},Ke=Se?function(n){return dt(n)?Se(n):[] -}:Q,Le={"&":"&","<":"<",">":">",'"':""","'":"'"},Me=_t(Le),Ue=ue("("+Ke(Me).join("|")+")","g"),Ve=ue("["+Ke(Le).join("")+"]","g"),Ge=ft(function(n,t,e){ye.call(n,e)?n[e]++:n[e]=1}),He=ft(function(n,t,e){(ye.call(n,e)?n[e]:n[e]=[]).push(t)}),Je=ft(function(n,t,e){n[e]=t});Te&&X&&typeof be=="function"&&(Gt=function(n){if(!bt(n))throw new ae;return be.apply(e,arguments)});var Qe=8==De(w+"08")?De:function(n,t){return De(jt(n)?n.replace(E,""):n,t||0)};return Y.after=function(n,t){if(!bt(t))throw new ae; -return function(){return 1>--n?t.apply(this,arguments):void 0}},Y.assign=J,Y.at=function(n){for(var t=arguments,e=-1,r=rt(t,!0,!1,1),t=t[2]&&t[2][t[1]]===n?1:r.length,u=Xt(t);++e=b&&o(a?r[a]:g) -}n:for(;++l(m?t(m,y):s(g,y))){for(a=u,(m||g).push(y);--a;)if(m=f[a],0>(m?t(m,y):s(r[a],y)))continue n;v.push(y)}}for(;u--;)(m=f[u])&&p(m);return c(f),c(g),v},Y.invert=_t,Y.invoke=function(n,t){var e=$e.call(arguments,2),r=-1,u=typeof t=="function",o=n?n.length:0,a=Xt(typeof o=="number"?o:0);return Nt(n,function(n){a[++r]=(u?t:n[t]).apply(n,e)}),a},Y.keys=Ke,Y.map=St,Y.max=Rt,Y.memoize=function(n,t){function e(){var r=e.cache,u=t?t.apply(this,arguments):_+arguments[0];return ye.call(r,u)?r[u]:r[u]=n.apply(this,arguments) +var qe=Y.support={};qe.fastBind=Ce&&!Te,qe.funcDecomp=!ce.test(e.a)&&R.test(v),qe.funcNames=typeof ne.name=="string",Y.templateSettings={escape:/<%-([\s\S]+?)%>/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:N,variable:"",imports:{_:Y}};var ze=xe?function(n,t){U.value=t,xe(n,"__bindData__",U)}:l,Pe=Ie||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&je.call(n)==$||!1},Ke=Se?function(n){return dt(n)?Se(n):[]}:Q,Le={"&":"&","<":"<",">":">",'"':""","'":"'"},Me=_t(Le),Ue=ue("("+Ke(Me).join("|")+")","g"),Ve=ue("["+Ke(Le).join("")+"]","g"); +Oe||(gt=function(n){if(dt(n)){l.prototype=n;var t=new l;l.prototype=null}return t||{}});var Ge=ft(function(n,t,e){ye.call(n,e)?n[e]++:n[e]=1}),He=ft(function(n,t,e){(ye.call(n,e)?n[e]:n[e]=[]).push(t)}),Je=ft(function(n,t,e){n[e]=t});Te&&X&&typeof be=="function"&&(Gt=function(n){if(!bt(n))throw new ae;return be.apply(e,arguments)});var Qe=8==De(w+"08")?De:function(n,t){return De(jt(n)?n.replace(E,""):n,t||0)};return Y.after=function(n,t){if(!bt(t))throw new ae;return function(){return 1>--n?t.apply(this,arguments):void 0 +}},Y.assign=J,Y.at=function(n){for(var t=arguments,e=-1,r=rt(t,!0,!1,1),t=t[2]&&t[2][t[1]]===n?1:r.length,u=Xt(t);++e=b&&o(a?r[a]:g) +}n:for(;++l(m?t(m,y):s(g,y))){for(a=u,(m||g).push(y);--a;)if(m=f[a],0>(m?t(m,y):s(r[a],y)))continue n;h.push(y)}}for(;u--;)(m=f[u])&&p(m);return c(f),c(g),h},Y.invert=_t,Y.invoke=function(n,t){var e=$e.call(arguments,2),r=-1,u=typeof t=="function",o=n?n.length:0,a=Xt(typeof o=="number"?o:0);return Nt(n,function(n){a[++r]=(u?t:n[t]).apply(n,e)}),a},Y.keys=Ke,Y.map=St,Y.max=Rt,Y.memoize=function(n,t){function e(){var r=e.cache,u=t?t.apply(this,arguments):_+arguments[0];return ye.call(r,u)?r[u]:r[u]=n.apply(this,arguments) }if(!bt(n))throw new ae;return e.cache={},e},Y.merge=function(n){var t=arguments,e=2;if(!dt(n))return n;if("number"!=typeof t[2]&&(e=t.length),3r(a,e))&&(o[e]=n)}),o},Y.once=function(n){var t,e;if(!bt(n))throw new ae;return function(){return t?e:(t=!0,e=n.apply(this,arguments),n=null,e)}},Y.pairs=function(n){for(var t=-1,e=Ke(n),r=e.length,u=Xt(r);++tr(a,e))&&(o[e]=n)}),o},Y.once=function(n){var t,e;if(!bt(n))throw new ae;return function(){return t?e:(t=!0,e=n.apply(this,arguments),n=null,e)}},Y.pairs=function(n){for(var t=-1,e=Ke(n),r=e.length,u=Xt(r);++te?Re(0,r+e):Ae(e,r-1))+1);r--;)if(n[r]===t)return r;return-1},Y.mixin=Jt,Y.noConflict=function(){return e._=le,this},Y.parseInt=Qe,Y.random=function(n,t,e){var r=null==n,u=null==t;return null==e&&(typeof n=="boolean"&&u?(e=n,n=1):u||typeof t!="boolean"||(e=t,u=!0)),r&&u&&(t=1),n=+n||0,u?(t=n,n=0):t=+t||0,e||n%1||t%1?(e=Be(),Ae(n+e*(t-n+parseFloat("1e-"+((e+"").length-1))),t)):at(n,t) -},Y.reduce=Dt,Y.reduceRight=Bt,Y.result=function(n,t){if(n){var e=n[t];return bt(e)?n[t]():e}},Y.runInContext=h,Y.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:Ke(n).length},Y.some=Ft,Y.sortedIndex=Pt,Y.template=function(n,t,e){var r=Y.templateSettings;n=oe(n||""),e=G({},e,r);var u,o=G({},e.imports,r.imports),r=Ke(o),o=kt(o),i=0,f=e.interpolate||S,l="__p+='",f=ue((e.escape||S).source+"|"+f.source+"|"+(f===N?C:S).source+"|"+(e.evaluate||S).source+"|$","g");n.replace(f,function(t,e,r,o,f,c){return r||(r=o),l+=n.slice(i,c).replace(A,a),e&&(l+="'+__e("+e+")+'"),f&&(u=!0,l+="';"+f+";\n__p+='"),r&&(l+="'+((__t=("+r+"))==null?'':__t)+'"),i=c+t.length,t -}),l+="';",f=e=e.variable,f||(e="obj",l="with("+e+"){"+l+"}"),l=(u?l.replace(j,""):l).replace(k,"$1").replace(x,"$1;"),l="function("+e+"){"+(f?"":e+"||("+e+"={});")+"var __t,__p='',__e=_.escape"+(u?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+l+"return __p}";try{var c=ne(r,"return "+l).apply(v,o)}catch(p){throw p.source=l,p}return t?c(t):(c.source=l,c)},Y.unescape=function(n){return null==n?"":oe(n).replace(Ue,vt)},Y.uniqueId=function(n){var t=++m;return oe(null==n?"":n)+t -},Y.all=Ct,Y.any=Ft,Y.detect=It,Y.findWhere=It,Y.foldl=Dt,Y.foldr=Bt,Y.include=xt,Y.inject=Dt,y(Y,function(n,t){Y.prototype[t]||(Y.prototype[t]=function(){var t=[this.__wrapped__],e=this.__chain__;return _e.apply(t,arguments),t=n.apply(Y,t),e?new nt(t,e):t})}),Y.first=Wt,Y.last=function(n,t,e){var r=0,u=n?n.length:0;if(typeof t!="number"&&null!=t){var o=u;for(t=Y.createCallback(t,e,3);o--&&t(n[o],o,n);)r++}else if(r=t,null==r||e)return n?n[u-1]:v;return s(n,Re(0,u-r))},Y.sample=function(n,t,e){return n&&typeof n.length!="number"&&(n=kt(n)),null==t||e?n?n[at(0,n.length-1)]:v:(n=$t(n),n.length=Ae(Re(0,t),n.length),n) +},Y.reduce=Dt,Y.reduceRight=Bt,Y.result=function(n,t){if(n){var e=n[t];return bt(e)?n[t]():e}},Y.runInContext=v,Y.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:Ke(n).length},Y.some=Ft,Y.sortedIndex=Pt,Y.template=function(n,t,e){var r=Y.templateSettings;n=oe(n||""),e=G({},e,r);var u,o=G({},e.imports,r.imports),r=Ke(o),o=kt(o),i=0,f=e.interpolate||S,l="__p+='",f=ue((e.escape||S).source+"|"+f.source+"|"+(f===N?C:S).source+"|"+(e.evaluate||S).source+"|$","g");n.replace(f,function(t,e,r,o,f,c){return r||(r=o),l+=n.slice(i,c).replace(A,a),e&&(l+="'+__e("+e+")+'"),f&&(u=!0,l+="';"+f+";\n__p+='"),r&&(l+="'+((__t=("+r+"))==null?'':__t)+'"),i=c+t.length,t +}),l+="';",f=e=e.variable,f||(e="obj",l="with("+e+"){"+l+"}"),l=(u?l.replace(j,""):l).replace(k,"$1").replace(x,"$1;"),l="function("+e+"){"+(f?"":e+"||("+e+"={});")+"var __t,__p='',__e=_.escape"+(u?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+l+"return __p}";try{var c=ne(r,"return "+l).apply(h,o)}catch(p){throw p.source=l,p}return t?c(t):(c.source=l,c)},Y.unescape=function(n){return null==n?"":oe(n).replace(Ue,vt)},Y.uniqueId=function(n){var t=++m;return oe(null==n?"":n)+t +},Y.all=Ct,Y.any=Ft,Y.detect=It,Y.findWhere=It,Y.foldl=Dt,Y.foldr=Bt,Y.include=xt,Y.inject=Dt,y(Y,function(n,t){Y.prototype[t]||(Y.prototype[t]=function(){var t=[this.__wrapped__],e=this.__chain__;return _e.apply(t,arguments),t=n.apply(Y,t),e?new nt(t,e):t})}),Y.first=Wt,Y.last=function(n,t,e){var r=0,u=n?n.length:0;if(typeof t!="number"&&null!=t){var o=u;for(t=Y.createCallback(t,e,3);o--&&t(n[o],o,n);)r++}else if(r=t,null==r||e)return n?n[u-1]:h;return s(n,Re(0,u-r))},Y.sample=function(n,t,e){return n&&typeof n.length!="number"&&(n=kt(n)),null==t||e?n?n[at(0,n.length-1)]:h:(n=$t(n),n.length=Ae(Re(0,t),n.length),n) },Y.take=Wt,Y.head=Wt,y(Y,function(n,t){var e="sample"!==t;Y.prototype[t]||(Y.prototype[t]=function(t,r){var u=this.__chain__,o=n(this.__wrapped__,t,r);return u||null!=t&&(!r||e&&typeof t=="function")?new nt(o,u):o})}),Y.VERSION="2.2.1",Y.prototype.chain=function(){return this.__chain__=!0,this},Y.prototype.toString=function(){return oe(this.__wrapped__)},Y.prototype.value=Qt,Y.prototype.valueOf=Qt,Nt(["join","pop","shift"],function(n){var t=ie[n];Y.prototype[n]=function(){var n=this.__chain__,e=t.apply(this.__wrapped__,arguments); -return n?new nt(e,n):e}}),Nt(["push","reverse","sort","unshift"],function(n){var t=ie[n];Y.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this}}),Nt(["concat","slice","splice"],function(n){var t=ie[n];Y.prototype[n]=function(){return new nt(t.apply(this.__wrapped__,arguments),this.__chain__)}}),Y}var v,g=[],y=[],m=0,_=+new Date+"",b=75,d=40,w=" \t\x0B\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000",j=/\b__p\+='';/g,k=/\b(__p\+=)''\+/g,x=/(__e\(.*?\)|\b__t\))\+'';/g,C=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,O=/\w*$/,I=/^function[ \n\r\t]+\w/,N=/<%=([\s\S]+?)%>/g,E=RegExp("^["+w+"]*0+(?=.$)"),S=/($^)/,R=/\bthis\b/,A=/['\n\r\t\u2028\u2029\\]/g,D="Array Boolean Date Function Math Number Object RegExp String _ attachEvent clearTimeout isFinite isNaN parseInt setImmediate setTimeout".split(" "),B="[object Arguments]",$="[object Array]",F="[object Boolean]",T="[object Date]",W="[object Function]",q="[object Number]",z="[object Object]",P="[object RegExp]",K="[object String]",L={}; +return n?new nt(e,n):e}}),Nt(["push","reverse","sort","unshift"],function(n){var t=ie[n];Y.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this}}),Nt(["concat","slice","splice"],function(n){var t=ie[n];Y.prototype[n]=function(){return new nt(t.apply(this.__wrapped__,arguments),this.__chain__)}}),Y}var h,g=[],y=[],m=0,_=+new Date+"",b=75,d=40,w=" \t\x0B\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000",j=/\b__p\+='';/g,k=/\b(__p\+=)''\+/g,x=/(__e\(.*?\)|\b__t\))\+'';/g,C=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,O=/\w*$/,I=/^function[ \n\r\t]+\w/,N=/<%=([\s\S]+?)%>/g,E=RegExp("^["+w+"]*0+(?=.$)"),S=/($^)/,R=/\bthis\b/,A=/['\n\r\t\u2028\u2029\\]/g,D="Array Boolean Date Function Math Number Object RegExp String _ attachEvent clearTimeout isFinite isNaN parseInt setImmediate setTimeout".split(" "),B="[object Arguments]",$="[object Array]",F="[object Boolean]",T="[object Date]",W="[object Function]",q="[object Number]",z="[object Object]",P="[object RegExp]",K="[object String]",L={}; L[W]=!1,L[B]=L[$]=L[F]=L[T]=L[q]=L[z]=L[P]=L[K]=!0;var M={leading:!1,maxWait:0,trailing:!1},U={configurable:!1,enumerable:!1,value:null,writable:!1},V={"boolean":!1,"function":!0,object:!0,number:!1,string:!1,undefined:!1},G={"\\":"\\","'":"'","\n":"n","\r":"r","\t":"t","\u2028":"u2028","\u2029":"u2029"},H=V[typeof window]&&window||this,J=V[typeof exports]&&exports&&!exports.nodeType&&exports,Q=V[typeof module]&&module&&!module.nodeType&&module,X=Q&&Q.exports===J&&J,Y=V[typeof global]&&global;!Y||Y.global!==Y&&Y.window!==Y||(H=Y); -var Z=h();typeof define=="function"&&typeof define.amd=="object"&&define.amd?(H._=Z, define(function(){return Z})):J&&Q?X?(Q.exports=Z)._=Z:J._=Z:H._=Z}).call(this); \ No newline at end of file +var Z=v();typeof define=="function"&&typeof define.amd=="object"&&define.amd?(H._=Z, define(function(){return Z})):J&&Q?X?(Q.exports=Z)._=Z:J._=Z:H._=Z}).call(this); \ No newline at end of file diff --git a/dist/lodash.underscore.js b/dist/lodash.underscore.js index a17b5f79e..0ba7bc3df 100644 --- a/dist/lodash.underscore.js +++ b/dist/lodash.underscore.js @@ -715,7 +715,7 @@ } if (this instanceof bound) { // ensure `new bound` is an instance of `func` - thisBinding = createObject(func.prototype); + thisBinding = create(func.prototype); // mimic the constructor's `return` behavior // http://es5.github.io/#x13.2.2 @@ -728,28 +728,6 @@ return bound; } - /** - * Creates a new object with the specified `prototype`. - * - * @private - * @param {Object} prototype The prototype object. - * @returns {Object} Returns the new object. - */ - function createObject(prototype) { - return isObject(prototype) ? nativeCreate(prototype) : {}; - } - // fallback for browsers without `Object.create` - if (!nativeCreate) { - createObject = function(prototype) { - if (isObject(prototype)) { - noop.prototype = prototype; - var result = new noop; - noop.prototype = null; - } - return result || {}; - }; - } - /** * Used by `escape` to convert characters to HTML entities. * @@ -994,6 +972,50 @@ : value; } + /** + * Creates a new object with the specified `prototype`. + * + * @static + * @memberOf _ + * @category Objects + * @param {Object} prototype The prototype object. + * @returns {Object} Returns the new object. + * @example + * + * function Shape() { + * this.x = 0; + * this.y = 0; + * } + * + * function Circle() { + * Shape.call(this); + * } + * + * Circle.prototype = _.create(Shape.prototype); + * Circle.prototype.constructor = Circle; + * + * var circle = new Circle; + * circle instanceof Circle + * // => true + * + * circle instanceof Shape + * // => true + */ + function create(prototype) { + return isObject(prototype) ? nativeCreate(prototype) : {}; + } + // fallback for browsers without `Object.create` + if (!nativeCreate) { + create = function(prototype) { + if (isObject(prototype)) { + noop.prototype = prototype; + var result = new noop; + noop.prototype = null; + } + return result || {}; + }; + } + /** * Assigns own enumerable properties of source object(s) to the destination * object for all destination properties that resolve to `undefined`. Once a @@ -1047,18 +1069,20 @@ * @returns {Object} Returns `object`. * @example * - * function Dog(name) { - * this.name = name; + * function Shape() { + * this.x = 0; + * this.y = 0; * } * - * Dog.prototype.bark = function() { - * console.log('Woof, woof!'); + * Shape.prototype.move = function(x, y) { + * this.x += x; + * this.y += y; * }; * - * _.forIn(new Dog('Dagny'), function(value, key) { + * _.forIn(new Shape, function(value, key) { * console.log(key); * }); - * // => logs 'bark' and 'name' (property order is not guaranteed across environments) + * // => logs 'x', 'y', and 'move' (property order is not guaranteed across environments) */ var forIn = function(collection, callback) { var index, iterable = collection, result = iterable; @@ -4578,6 +4602,7 @@ lodash.compact = compact; lodash.compose = compose; lodash.countBy = countBy; + lodash.create = create; lodash.debounce = debounce; lodash.defaults = defaults; lodash.defer = defer; diff --git a/dist/lodash.underscore.min.js b/dist/lodash.underscore.min.js index 7783c424e..7ede2f83a 100644 --- a/dist/lodash.underscore.min.js +++ b/dist/lodash.underscore.min.js @@ -4,35 +4,35 @@ * Build: `lodash underscore exports="amd,commonjs,global,node" -o ./dist/lodash.underscore.js` */ ;(function(){function n(n,r,t){t=(t||0)-1;for(var e=n?n.length:0;++te||typeof t=="undefined")return 1;if(tu(f,l))&&(t&&f.push(l),i.push(a))}return i}function c(n){return function(r,t,e){var u={};t=K(t,e,3),e=-1; -var o=r?r.length:0;if(typeof o=="number")for(;++eu(f,l))&&(t&&f.push(l),i.push(a))}return i}function c(n){return function(r,t,e){var u={};t=K(t,e,3),e=-1; +var o=r?r.length:0;if(typeof o=="number")for(;++ee&&(e=t,u=n)});else for(;++ou&&(u=t);return u }function D(n,r){var t=-1,e=n?n.length:0;if(typeof e=="number")for(var u=Array(e);++targuments.length;r=i(r,e,4);var o=-1,f=n.length;if(typeof f=="number")for(u&&(t=n[++o]);++oarguments.length;return r=i(r,e,4),k(n,function(n,e,o){t=u?(u=!1,n):r(t,n,e,o)}),t}function I(n){var r=-1,t=n?n.length:0,e=Array(typeof t=="number"?t:0); -return R(n,function(n){var t;t=++r,t=0+Er(Ir()*(t-0+1)),e[r]=e[t],e[t]=n}),e}function W(n,r,t){var e;r=K(r,t,3),t=-1;var u=n?n.length:0;if(typeof u=="number")for(;++tt(u,i)&&o.push(i)}return o}function P(n,r,t){var e=0,u=n?n.length:0;if(typeof r!="number"&&null!=r){var o=-1;for(r=K(r,t,3);++ot(u,i)&&o.push(i)}return o}function P(n,r,t){var e=0,u=n?n.length:0;if(typeof r!="number"&&null!=r){var o=-1;for(r=K(r,t,3);++oe?Mr(0,u+e):e||0}else if(e)return e=G(r,t),r[e]===t?e:-1;return n(r,t,e)}function V(n,r,t){if(typeof r!="number"&&null!=r){var e=0,u=-1,o=n?n.length:0;for(r=K(r,t,3);++u>>1,t(n[e])/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,variable:""},Rr||(s=function(n){if(x(n)){e.prototype=n;var r=new e;e.prototype=null}return r||{}}),y(arguments)||(y=function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Ar.call(n,"callee")||!1});var Ur=kr||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Sr.call(n)==or||!1 -},Vr=function(n){var r,t=[];if(!n||!sr[typeof n])return t;for(r in n)Ar.call(n,r)&&t.push(r);return t},Gr=Dr?function(n){return x(n)?Dr(n):[]}:Vr,Hr={"&":"&","<":"<",">":">",'"':""","'":"'"},Jr=b(Hr),Kr=RegExp("("+Gr(Jr).join("|")+")","g"),Lr=RegExp("["+Gr(Hr).join("")+"]","g"),Qr=function(n,r){var t;if(!n||!sr[typeof n])return n;for(t in n)if(r(n[t],t,n)===nr)break;return n},Xr=function(n,r){var t;if(!n||!sr[typeof n])return n;for(t in n)if(Ar.call(n,t)&&r(n[t],t,n)===nr)break; +o.prototype=u.prototype;var Pr={};!function(){var n={0:1,length:1};Pr.fastBind=Nr&&!Cr,Pr.spliceObjects=(dr.splice.call(n,0,1),!n[0])}(1),u.templateSettings={escape:/<%-([\s\S]+?)%>/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,variable:""},v(arguments)||(v=function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Ar.call(n,"callee")||!1});var Ur=kr||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Sr.call(n)==or||!1},Vr=function(n){var r,t=[];if(!n||!sr[typeof n])return t; +for(r in n)Ar.call(n,r)&&t.push(r);return t},Gr=Dr?function(n){return x(n)?Dr(n):[]}:Vr,Hr={"&":"&","<":"<",">":">",'"':""","'":"'"},Jr=b(Hr),Kr=RegExp("("+Gr(Jr).join("|")+")","g"),Lr=RegExp("["+Gr(Hr).join("")+"]","g");Rr||(m=function(n){if(x(n)){e.prototype=n;var r=new e;e.prototype=null}return r||{}});var Qr=function(n,r){var t;if(!n||!sr[typeof n])return n;for(t in n)if(r(n[t],t,n)===nr)break;return n},Xr=function(n,r){var t;if(!n||!sr[typeof n])return n;for(t in n)if(Ar.call(n,t)&&r(n[t],t,n)===nr)break; return n};j(/x/)&&(j=function(n){return typeof n=="function"&&"[object Function]"==Sr.call(n)});var Yr=c(function(n,r,t){Ar.call(n,t)?n[t]++:n[t]=1}),Zr=c(function(n,r,t){(Ar.call(n,t)?n[t]:n[t]=[]).push(r)}),nt=c(function(n,r,t){n[t]=r});u.after=function(n,r){if(!j(r))throw new TypeError;return function(){return 1>--n?r.apply(this,arguments):void 0}},u.bind=J,u.bindAll=function(n){for(var r=1u(i,f)){for(var a=t;--a;)if(0>u(r[a],f))continue n; -i.push(f)}}return i},u.invert=b,u.invoke=function(n,r){var t=Wr.call(arguments,2),e=-1,u=typeof r=="function",o=n?n.length:0,i=Array(typeof o=="number"?o:0);return R(n,function(n){i[++e]=(u?r:n[r]).apply(n,t)}),i},u.keys=Gr,u.map=F,u.max=q,u.memoize=function(n,r){var t={};return function(){var e=r?r.apply(this,arguments):rr+arguments[0];return Ar.call(t,e)?t[e]:t[e]=n.apply(this,arguments)}},u.min=function(n,r,t){var e=1/0,u=e,o=-1,i=n?n.length:0;if(r||typeof i!="number")r=K(r,t,3),R(n,function(n,t,o){t=r(n,t,o),tr(t,u)&&(e[u]=n)}),e},u.once=function(n){var r,t;if(!j(n))throw new TypeError;return function(){return r?t:(r=!0,t=n.apply(this,arguments),n=null,t)}},u.pairs=function(n){for(var r=-1,t=Gr(n),e=t.length,u=Array(e);++rr?0:r);++nt?Mr(0,e+t):$r(t,e-1))+1);e--;)if(n[e]===r)return e;return-1},u.mixin=X,u.noConflict=function(){return gr._=wr,this},u.random=function(n,r){return null==n&&null==r&&(r=1),n=+n||0,null==r?(r=n,n=0):r=+r||0,n+Er(Ir()*(r-n+1))},u.reduce=M,u.reduceRight=$,u.result=function(n,r){if(n){var t=n[r];return j(t)?n[r]():t}},u.size=function(n){var r=n?n.length:0; -return typeof r=="number"?r:Gr(n).length},u.some=W,u.sortedIndex=G,u.template=function(n,r,e){var o=u,i=o.templateSettings;n=(n||"")+"",e=_({},e,i);var f=0,a="__p+='",i=e.variable;n.replace(RegExp((e.escape||tr).source+"|"+(e.interpolate||tr).source+"|"+(e.evaluate||tr).source+"|$","g"),function(r,e,u,o,i){return a+=n.slice(f,i).replace(er,t),e&&(a+="'+_.escape("+e+")+'"),o&&(a+="';"+o+";\n__p+='"),u&&(a+="'+((__t=("+u+"))==null?'':__t)+'"),f=i+r.length,r}),a+="';",i||(i="obj",a="with("+i+"||{}){"+a+"}"),a="function("+i+"){var __t,__p='',__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+a+"return __p}"; -try{var l=Function("_","return "+a)(o)}catch(c){throw c.source=a,c}return r?l(r):(l.source=a,l)},u.unescape=function(n){return null==n?"":(n+"").replace(Kr,v)},u.uniqueId=function(n){var r=++Z+"";return n?n+r:r},u.all=S,u.any=W,u.detect=N,u.findWhere=function(n,r){return z(n,r,!0)},u.foldl=M,u.foldr=$,u.include=O,u.inject=M,u.first=P,u.last=function(n,r,t){var e=0,u=n?n.length:0;if(typeof r!="number"&&null!=r){var o=u;for(r=K(r,t,3);o--&&r(n[o],o,n);)e++}else if(e=r,null==e||t)return n?n[u-1]:Y;return Wr.call(n,Mr(0,u-e)) -},u.sample=function(n,r,t){return n&&typeof n.length!="number"&&(n=T(n)),null==r||t?n?n[0+Er(Ir()*(n.length-1-0+1))]:Y:(n=I(n),n.length=$r(Mr(0,r),n.length),n)},u.take=P,u.head=P,X(u),u.VERSION="2.2.1",u.prototype.chain=function(){return this.__chain__=!0,this},u.prototype.value=function(){return this.__wrapped__},R("pop push reverse shift sort splice unshift".split(" "),function(n){var r=dr[n];u.prototype[n]=function(){var n=this.__wrapped__;return r.apply(n,arguments),Pr.spliceObjects||0!==n.length||delete n[0],this -}}),R(["concat","join","slice"],function(n){var r=dr[n];u.prototype[n]=function(){var n=r.apply(this.__wrapped__,arguments);return this.__chain__&&(n=new o(n),n.__chain__=!0),n}}),typeof define=="function"&&typeof define.amd=="object"&&define.amd?(gr._=u, define(function(){return u})):vr&&yr?mr?(yr.exports=u)._=u:vr._=u:gr._=u}).call(this); \ No newline at end of file +},u.chain=function(n){return n=new o(n),n.__chain__=!0,n},u.compact=function(n){for(var r=-1,t=n?n.length:0,e=[];++ru(i,f)){for(var a=t;--a;)if(0>u(r[a],f))continue n;i.push(f)}}return i},u.invert=b,u.invoke=function(n,r){var t=Wr.call(arguments,2),e=-1,u=typeof r=="function",o=n?n.length:0,i=Array(typeof o=="number"?o:0);return R(n,function(n){i[++e]=(u?r:n[r]).apply(n,t)}),i},u.keys=Gr,u.map=F,u.max=q,u.memoize=function(n,r){var t={};return function(){var e=r?r.apply(this,arguments):rr+arguments[0];return Ar.call(t,e)?t[e]:t[e]=n.apply(this,arguments)}},u.min=function(n,r,t){var e=1/0,u=e,o=-1,i=n?n.length:0; +if(r||typeof i!="number")r=K(r,t,3),R(n,function(n,t,o){t=r(n,t,o),tr(t,u)&&(e[u]=n)}),e},u.once=function(n){var r,t;if(!j(n))throw new TypeError;return function(){return r?t:(r=!0,t=n.apply(this,arguments),n=null,t)}},u.pairs=function(n){for(var r=-1,t=Gr(n),e=t.length,u=Array(e);++rr?0:r);++nt?Mr(0,e+t):$r(t,e-1))+1);e--;)if(n[e]===r)return e;return-1},u.mixin=X,u.noConflict=function(){return gr._=wr,this},u.random=function(n,r){return null==n&&null==r&&(r=1),n=+n||0,null==r?(r=n,n=0):r=+r||0,n+Er(Ir()*(r-n+1))},u.reduce=M,u.reduceRight=$,u.result=function(n,r){if(n){var t=n[r]; +return j(t)?n[r]():t}},u.size=function(n){var r=n?n.length:0;return typeof r=="number"?r:Gr(n).length},u.some=W,u.sortedIndex=G,u.template=function(n,r,e){var o=u,i=o.templateSettings;n=(n||"")+"",e=_({},e,i);var f=0,a="__p+='",i=e.variable;n.replace(RegExp((e.escape||tr).source+"|"+(e.interpolate||tr).source+"|"+(e.evaluate||tr).source+"|$","g"),function(r,e,u,o,i){return a+=n.slice(f,i).replace(er,t),e&&(a+="'+_.escape("+e+")+'"),o&&(a+="';"+o+";\n__p+='"),u&&(a+="'+((__t=("+u+"))==null?'':__t)+'"),f=i+r.length,r +}),a+="';",i||(i="obj",a="with("+i+"||{}){"+a+"}"),a="function("+i+"){var __t,__p='',__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+a+"return __p}";try{var l=Function("_","return "+a)(o)}catch(c){throw c.source=a,c}return r?l(r):(l.source=a,l)},u.unescape=function(n){return null==n?"":(n+"").replace(Kr,g)},u.uniqueId=function(n){var r=++Z+"";return n?n+r:r},u.all=S,u.any=W,u.detect=N,u.findWhere=function(n,r){return z(n,r,!0)},u.foldl=M,u.foldr=$,u.include=O,u.inject=M,u.first=P,u.last=function(n,r,t){var e=0,u=n?n.length:0; +if(typeof r!="number"&&null!=r){var o=u;for(r=K(r,t,3);o--&&r(n[o],o,n);)e++}else if(e=r,null==e||t)return n?n[u-1]:Y;return Wr.call(n,Mr(0,u-e))},u.sample=function(n,r,t){return n&&typeof n.length!="number"&&(n=T(n)),null==r||t?n?n[0+Er(Ir()*(n.length-1-0+1))]:Y:(n=I(n),n.length=$r(Mr(0,r),n.length),n)},u.take=P,u.head=P,X(u),u.VERSION="2.2.1",u.prototype.chain=function(){return this.__chain__=!0,this},u.prototype.value=function(){return this.__wrapped__},R("pop push reverse shift sort splice unshift".split(" "),function(n){var r=dr[n]; +u.prototype[n]=function(){var n=this.__wrapped__;return r.apply(n,arguments),Pr.spliceObjects||0!==n.length||delete n[0],this}}),R(["concat","join","slice"],function(n){var r=dr[n];u.prototype[n]=function(){var n=r.apply(this.__wrapped__,arguments);return this.__chain__&&(n=new o(n),n.__chain__=!0),n}}),typeof define=="function"&&typeof define.amd=="object"&&define.amd?(gr._=u, define(function(){return u})):vr&&yr?mr?(yr.exports=u)._=u:vr._=u:gr._=u}).call(this); \ No newline at end of file diff --git a/lodash.js b/lodash.js index e484cb92b..d80a01a5f 100644 --- a/lodash.js +++ b/lodash.js @@ -1279,7 +1279,7 @@ // non `Object` object instances with different constructors are not equal if (ctorA != ctorB && !(isFunction(ctorA) && ctorA instanceof ctorA && isFunction(ctorB) && ctorB instanceof ctorB) && - (!nativeCreate || ('constructor' in a && 'constructor' in b)) + ('constructor' in a && 'constructor' in b) ) { return false; } @@ -1619,7 +1619,7 @@ } if (this instanceof bound) { // ensure `new bound` is an instance of `func` - thisBinding = createObject(func.prototype); + thisBinding = create(func.prototype); // mimic the constructor's `return` behavior // http://es5.github.io/#x13.2.2 @@ -1682,28 +1682,6 @@ ); } - /** - * Creates a new object with the specified `prototype`. - * - * @private - * @param {Object} prototype The prototype object. - * @returns {Object} Returns the new object. - */ - function createObject(prototype) { - return isObject(prototype) ? nativeCreate(prototype) : {}; - } - // fallback for browsers without `Object.create` - if (!nativeCreate) { - createObject = function(prototype) { - if (isObject(prototype)) { - noop.prototype = prototype; - var result = new noop; - noop.prototype = null; - } - return result || {}; - }; - } - /** * Used by `escape` to convert characters to HTML entities. * @@ -2093,6 +2071,50 @@ return baseClone(value, true, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1)); } + /** + * Creates a new object with the specified `prototype`. + * + * @static + * @memberOf _ + * @category Objects + * @param {Object} prototype The prototype object. + * @returns {Object} Returns the new object. + * @example + * + * function Shape() { + * this.x = 0; + * this.y = 0; + * } + * + * function Circle() { + * Shape.call(this); + * } + * + * Circle.prototype = _.create(Shape.prototype); + * Circle.prototype.constructor = Circle; + * + * var circle = new Circle; + * circle instanceof Circle + * // => true + * + * circle instanceof Shape + * // => true + */ + function create(prototype) { + return isObject(prototype) ? nativeCreate(prototype) : {}; + } + // fallback for browsers without `Object.create` + if (!nativeCreate) { + create = function(prototype) { + if (isObject(prototype)) { + noop.prototype = prototype; + var result = new noop; + noop.prototype = null; + } + return result || {}; + }; + } + /** * Assigns own enumerable properties of source object(s) to the destination * object for all destination properties that resolve to `undefined`. Once a @@ -2237,18 +2259,20 @@ * @returns {Object} Returns `object`. * @example * - * function Dog(name) { - * this.name = name; + * function Shape() { + * this.x = 0; + * this.y = 0; * } * - * Dog.prototype.bark = function() { - * console.log('Woof, woof!'); + * Shape.prototype.move = function(x, y) { + * this.x += x; + * this.y += y; * }; * - * _.forIn(new Dog('Dagny'), function(value, key) { + * _.forIn(new Shape, function(value, key) { * console.log(key); * }); - * // => logs 'bark' and 'name' (property order is not guaranteed across environments) + * // => logs 'x', 'y', and 'move' (property order is not guaranteed across environments) */ var forIn = createIterator(eachIteratorOptions, forOwnIteratorOptions, { 'useHas': false @@ -2267,18 +2291,20 @@ * @returns {Object} Returns `object`. * @example * - * function Dog(name) { - * this.name = name; + * function Shape() { + * this.x = 0; + * this.y = 0; * } * - * Dog.prototype.bark = function() { - * console.log('Woof, woof!'); + * Shape.prototype.move = function(x, y) { + * this.x += x; + * this.y += y; * }; * - * _.forInRight(new Dog('Dagny'), function(value, key) { + * _.forInRight(new Shape, function(value, key) { * console.log(key); * }); - * // => logs 'name' and 'bark' assuming `_.forIn ` logs 'bark' and 'name' + * // => logs 'move', 'y', and 'x' assuming `_.forIn ` logs 'x', 'y', and 'move' */ function forInRight(object, callback, thisArg) { var pairs = []; @@ -2720,18 +2746,18 @@ * @returns {boolean} Returns `true` if `value` is a plain object, else `false`. * @example * - * function Person(name, age) { - * this.name = name; - * this.age = age; + * function Shape() { + * this.x = 0; + * this.y = 0; * } * - * _.isPlainObject(new Person('fred', 40)); + * _.isPlainObject(new Shape); * // => false * * _.isPlainObject([1, 2, 3]); * // => false * - * _.isPlainObject({ 'name': 'fred', 'age': 40 }); + * _.isPlainObject({ 'x': 0, 'y': 0 }); * // => true */ var isPlainObject = !getPrototypeOf ? shimIsPlainObject : function(value) { @@ -3015,7 +3041,7 @@ * @static * @memberOf _ * @category Objects - * @param {Array|Object} collection The collection to iterate over. + * @param {Array|Object} object The object to iterate over. * @param {Function} [callback=identity] The function called per iteration. * @param {*} [accumulator] The custom accumulator value. * @param {*} [thisArg] The `this` binding of `callback`. @@ -3037,8 +3063,6 @@ */ function transform(object, callback, accumulator, thisArg) { var isArr = isArray(object); - callback = baseCreateCallback(callback, thisArg, 4); - if (accumulator == null) { if (isArr) { accumulator = []; @@ -3046,12 +3070,15 @@ var ctor = object && object.constructor, proto = ctor && ctor.prototype; - accumulator = createObject(proto); + accumulator = create(proto); } } - (isArr ? baseEach : forOwn)(object, function(value, index, object) { - return callback(accumulator, value, index, object); - }); + if (callback) { + callback = baseCreateCallback(callback, thisArg, 4); + (isArr ? baseEach : forOwn)(object, function(value, index, object) { + return callback(accumulator, value, index, object); + }); + } return accumulator; } @@ -6545,6 +6572,7 @@ lodash.compact = compact; lodash.compose = compose; lodash.countBy = countBy; + lodash.create = create; lodash.createCallback = createCallback; lodash.curry = curry; lodash.debounce = debounce;