Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add unit tests for better coverage of Y.each and Y.some (and conseque…

…ntly, Y.dispatchIterator)
  • Loading branch information...
commit cfd2fee09bc5f9bb4cfc1223a9edd11919f94723 1 parent bfbcb32
John Lindal authored
Showing with 127 additions and 101 deletions.
  1. +63 −50 build/oop/oop-debug.js
  2. +1 −1  build/oop/oop-min.js
  3. +63 −50 build/oop/oop.js
View
113 build/oop/oop-debug.js
@@ -15,57 +15,70 @@ var L = Y.Lang,
hasOwn = OP.hasOwnProperty,
toString = OP.toString;
-function dispatch(o, f, c, proto, action) {
- if (o && o[action] && o !== Y) {
- return o[action].call(o, f, c);
- } else {
- switch (A.test(o)) {
- case 1:
- return A[action](o, f, c);
- case 2:
- return A[action](Y.Array(o, 0, true), f, c);
- default:
- return Y.Object[action](o, f, c, proto);
- }
+/**
+ * Executes the named action for a collection by dispatching the call to
+ * either the object, Y.Object, or Y.Array, as appropriate.
+ *
+ * @method dispatchIterator
+ * @param {String} name of the function to call
+ * @param {Array|Object} array or object to iterate
+ * @param {Mixed} ... arguments to pass to the function
+ * @return {Mixed} whatever the function returns
+ */
+Y.dispatchIterator = function(action, o) {
+ var args = A(arguments, 1, true);
+ switch (A.test(o)) {
+ case 1: // array
+ return A[action].apply(null, args);
+ case 2: // array-like
+ args[0] = A(o, 0, true);
+ return A[action].apply(null, args);
+ default: // something else
+ if (o && o[action] && o !== Y) {
+ args.shift();
+ return o[action].apply(o, args);
+ } else {
+ return Y.Object[action].apply(null, args);
+ }
}
-}
+};
/**
-Augments the _receiver_ with prototype properties from the _supplier_. The
-receiver may be a constructor function or an object. The supplier must be a
-constructor function.
-
-If the _receiver_ is an object, then the _supplier_ constructor will be called
-immediately after _receiver_ is augmented, with _receiver_ as the `this` object.
-
-If the _receiver_ is a constructor function, then all prototype methods of
-_supplier_ that are copied to _receiver_ will be sequestered, and the
-_supplier_ constructor will not be called immediately. The first time any
-sequestered method is called on the _receiver_'s prototype, all sequestered
-methods will be immediately copied to the _receiver_'s prototype, the
-_supplier_'s constructor will be executed, and finally the newly unsequestered
-method that was called will be executed.
-
-This sequestering logic sounds like a bunch of complicated voodoo, but it makes
-it cheap to perform frequent augmentation by ensuring that suppliers'
-constructors are only called if a supplied method is actually used. If none of
-the supplied methods is ever used, then there's no need to take the performance
-hit of calling the _supplier_'s constructor.
-
-@method augment
-@param {Function|Object} receiver Object or function to be augmented.
-@param {Function} supplier Function that supplies the prototype properties with
- which to augment the _receiver_.
-@param {Boolean} [overwrite=false] If `true`, properties already on the receiver
- will be overwritten if found on the supplier's prototype.
-@param {String[]} [whitelist] An array of property names. If specified,
- only the whitelisted prototype properties will be applied to the receiver, and
- all others will be ignored.
-@param {Array|any} [args] Argument or array of arguments to pass to the
- supplier's constructor when initializing.
-@return {Function} Augmented object.
-@for YUI
-**/
+ * Augments the _receiver_ with prototype properties from the _supplier_. The
+ * receiver may be a constructor function or an object. The supplier must be a
+ * constructor function.
+ *
+ * If the _receiver_ is an object, then the _supplier_ constructor will be called
+ * immediately after _receiver_ is augmented, with _receiver_ as the `this` object.
+ *
+ * If the _receiver_ is a constructor function, then all prototype methods of
+ * _supplier_ that are copied to _receiver_ will be sequestered, and the
+ * _supplier_ constructor will not be called immediately. The first time any
+ * sequestered method is called on the _receiver_'s prototype, all sequestered
+ * methods will be immediately copied to the _receiver_'s prototype, the
+ * _supplier_'s constructor will be executed, and finally the newly unsequestered
+ * method that was called will be executed.
+ *
+ * This sequestering logic sounds like a bunch of complicated voodoo, but it makes
+ * it cheap to perform frequent augmentation by ensuring that suppliers'
+ * constructors are only called if a supplied method is actually used. If none of
+ * the supplied methods is ever used, then there's no need to take the performance
+ * hit of calling the _supplier_'s constructor.
+ *
+ * @method augment
+ * @param {Function|Object} receiver Object or function to be augmented.
+ * @param {Function} supplier Function that supplies the prototype properties with
+ * which to augment the _receiver_.
+ * @param {Boolean} [overwrite=false] If `true`, properties already on the receiver
+ * will be overwritten if found on the supplier's prototype.
+ * @param {String[]} [whitelist] An array of property names. If specified,
+ * only the whitelisted prototype properties will be applied to the receiver, and
+ * all others will be ignored.
+ * @param {Array|any} [args] Argument or array of arguments to pass to the
+ * supplier's constructor when initializing.
+ * @return {Function} Augmented object.
+ * @for YUI
+ */
Y.augment = function (receiver, supplier, overwrite, whitelist, args) {
var rProto = receiver.prototype,
sequester = rProto && supplier,
@@ -210,7 +223,7 @@ Y.extend = function(r, s, px, sx) {
* @return {YUI} the YUI instance.
*/
Y.each = function(o, f, c, proto) {
- return dispatch(o, f, c, proto, 'each');
+ return Y.dispatchIterator('each', o, f, c, proto);
};
/**
@@ -229,7 +242,7 @@ Y.each = function(o, f, c, proto) {
* false otherwise.
*/
Y.some = function(o, f, c, proto) {
- return dispatch(o, f, c, proto, 'some');
+ return Y.dispatchIterator('some', o, f, c, proto);
};
/**
View
2  build/oop/oop-min.js
@@ -1 +1 @@
-YUI.add("oop",function(h){var d=h.Lang,c=h.Array,b=Object.prototype,a="_~yuim~_",e=b.hasOwnProperty,g=b.toString;function f(l,k,m,i,j){if(l&&l[j]&&l!==h){return l[j].call(l,k,m);}else{switch(c.test(l)){case 1:return c[j](l,k,m);case 2:return c[j](h.Array(l,0,true),k,m);default:return h.Object[j](l,k,m,i);}}}h.augment=function(i,k,r,o,s){var n=i.prototype,m=n&&k,q=k.prototype,v=n||i,j,u,p,l,t;s=s?h.Array(s):[];if(m){u={};p={};l={};j=function(x,w){if(r||!(w in n)){if(g.call(x)==="[object Function]"){l[w]=x;u[w]=p[w]=function(){return t(this,x,arguments);};}else{u[w]=x;}}};t=function(w,y,z){for(var x in l){if(e.call(l,x)&&w[x]===p[x]){w[x]=l[x];}}k.apply(w,s);return y.apply(w,z);};if(o){h.Array.each(o,function(w){if(w in q){j(q[w],w);}});}else{h.Object.each(q,j,null,true);}}h.mix(v,u||q,r,o);if(!m){k.apply(v,s);}return i;};h.aggregate=function(k,j,i,l){return h.mix(k,j,i,l,0,true);};h.extend=function(l,k,i,n){if(!k||!l){h.error("extend failed, verify dependencies");}var m=k.prototype,j=h.Object(m);l.prototype=j;j.constructor=l;l.superclass=m;if(k!=Object&&m.constructor==b.constructor){m.constructor=k;}if(i){h.mix(j,i,true);}if(n){h.mix(l,n,true);}return l;};h.each=function(k,j,l,i){return f(k,j,l,i,"each");};h.some=function(k,j,l,i){return f(k,j,l,i,"some");};h.clone=function(l,m,r,s,k,q){if(!d.isObject(l)){return l;}if(h.instanceOf(l,YUI)){return l;}var n,j=q||{},i,p=h.each;switch(d.type(l)){case"date":return new Date(l);case"regexp":return l;case"function":return l;case"array":n=[];break;default:if(l[a]){return j[l[a]];}i=h.guid();n=(m)?{}:h.Object(l);l[a]=i;j[i]=l;}if(!l.addEventListener&&!l.attachEvent){p(l,function(t,o){if((o||o===0)&&(!r||(r.call(s||this,t,o,this,l)!==false))){if(o!==a){if(o=="prototype"){}else{this[o]=h.clone(t,m,r,s,k||l,j);}}}},n);}if(!q){h.Object.each(j,function(t,o){if(t[a]){try{delete t[a];}catch(u){t[a]=null;}}},this);j=null;}return n;};h.bind=function(i,k){var j=arguments.length>2?h.Array(arguments,2,true):null;return function(){var m=d.isString(i)?k[i]:i,l=(j)?j.concat(h.Array(arguments,0,true)):arguments;return m.apply(k||m,l);};};h.rbind=function(i,k){var j=arguments.length>2?h.Array(arguments,2,true):null;return function(){var m=d.isString(i)?k[i]:i,l=(j)?h.Array(arguments,0,true).concat(j):arguments;return m.apply(k||m,l);};};},"@VERSION@",{requires:["yui-base"]});
+YUI.add("oop",function(g){var d=g.Lang,c=g.Array,b=Object.prototype,a="_~yuim~_",e=b.hasOwnProperty,f=b.toString;g.dispatchIterator=function(i,j){var h=c(arguments,1,true);switch(c.test(j)){case 1:return c[i].apply(null,h);case 2:h[0]=c(j,0,true);return c[i].apply(null,h);default:if(j&&j[i]&&j!==g){h.shift();return j[i].apply(j,h);}else{return g.Object[i].apply(null,h);}}};g.augment=function(h,j,q,n,r){var m=h.prototype,l=m&&j,p=j.prototype,u=m||h,i,t,o,k,s;r=r?g.Array(r):[];if(l){t={};o={};k={};i=function(w,v){if(q||!(v in m)){if(f.call(w)==="[object Function]"){k[v]=w;t[v]=o[v]=function(){return s(this,w,arguments);};}else{t[v]=w;}}};s=function(v,x,y){for(var w in k){if(e.call(k,w)&&v[w]===o[w]){v[w]=k[w];}}j.apply(v,r);return x.apply(v,y);};if(n){g.Array.each(n,function(v){if(v in p){i(p[v],v);}});}else{g.Object.each(p,i,null,true);}}g.mix(u,t||p,q,n);if(!l){j.apply(u,r);}return h;};g.aggregate=function(j,i,h,k){return g.mix(j,i,h,k,0,true);};g.extend=function(k,j,h,m){if(!j||!k){g.error("extend failed, verify dependencies");}var l=j.prototype,i=g.Object(l);k.prototype=i;i.constructor=k;k.superclass=l;if(j!=Object&&l.constructor==b.constructor){l.constructor=j;}if(h){g.mix(i,h,true);}if(m){g.mix(k,m,true);}return k;};g.each=function(j,i,k,h){return g.dispatchIterator("each",j,i,k,h);};g.some=function(j,i,k,h){return g.dispatchIterator("some",j,i,k,h);};g.clone=function(k,l,q,r,j,p){if(!d.isObject(k)){return k;}if(g.instanceOf(k,YUI)){return k;}var m,i=p||{},h,n=g.each;switch(d.type(k)){case"date":return new Date(k);case"regexp":return k;case"function":return k;case"array":m=[];break;default:if(k[a]){return i[k[a]];}h=g.guid();m=(l)?{}:g.Object(k);k[a]=h;i[h]=k;}if(!k.addEventListener&&!k.attachEvent){n(k,function(s,o){if((o||o===0)&&(!q||(q.call(r||this,s,o,this,k)!==false))){if(o!==a){if(o=="prototype"){}else{this[o]=g.clone(s,l,q,r,j||k,i);}}}},m);}if(!p){g.Object.each(i,function(s,o){if(s[a]){try{delete s[a];}catch(t){s[a]=null;}}},this);i=null;}return m;};g.bind=function(h,j){var i=arguments.length>2?g.Array(arguments,2,true):null;return function(){var l=d.isString(h)?j[h]:h,k=(i)?i.concat(g.Array(arguments,0,true)):arguments;return l.apply(j||l,k);};};g.rbind=function(h,j){var i=arguments.length>2?g.Array(arguments,2,true):null;return function(){var l=d.isString(h)?j[h]:h,k=(i)?g.Array(arguments,0,true).concat(i):arguments;return l.apply(j||l,k);};};},"@VERSION@",{requires:["yui-base"]});
View
113 build/oop/oop.js
@@ -15,57 +15,70 @@ var L = Y.Lang,
hasOwn = OP.hasOwnProperty,
toString = OP.toString;
-function dispatch(o, f, c, proto, action) {
- if (o && o[action] && o !== Y) {
- return o[action].call(o, f, c);
- } else {
- switch (A.test(o)) {
- case 1:
- return A[action](o, f, c);
- case 2:
- return A[action](Y.Array(o, 0, true), f, c);
- default:
- return Y.Object[action](o, f, c, proto);
- }
+/**
+ * Executes the named action for a collection by dispatching the call to
+ * either the object, Y.Object, or Y.Array, as appropriate.
+ *
+ * @method dispatchIterator
+ * @param {String} name of the function to call
+ * @param {Array|Object} array or object to iterate
+ * @param {Mixed} ... arguments to pass to the function
+ * @return {Mixed} whatever the function returns
+ */
+Y.dispatchIterator = function(action, o) {
+ var args = A(arguments, 1, true);
+ switch (A.test(o)) {
+ case 1: // array
+ return A[action].apply(null, args);
+ case 2: // array-like
+ args[0] = A(o, 0, true);
+ return A[action].apply(null, args);
+ default: // something else
+ if (o && o[action] && o !== Y) {
+ args.shift();
+ return o[action].apply(o, args);
+ } else {
+ return Y.Object[action].apply(null, args);
+ }
}
-}
+};
/**
-Augments the _receiver_ with prototype properties from the _supplier_. The
-receiver may be a constructor function or an object. The supplier must be a
-constructor function.
-
-If the _receiver_ is an object, then the _supplier_ constructor will be called
-immediately after _receiver_ is augmented, with _receiver_ as the `this` object.
-
-If the _receiver_ is a constructor function, then all prototype methods of
-_supplier_ that are copied to _receiver_ will be sequestered, and the
-_supplier_ constructor will not be called immediately. The first time any
-sequestered method is called on the _receiver_'s prototype, all sequestered
-methods will be immediately copied to the _receiver_'s prototype, the
-_supplier_'s constructor will be executed, and finally the newly unsequestered
-method that was called will be executed.
-
-This sequestering logic sounds like a bunch of complicated voodoo, but it makes
-it cheap to perform frequent augmentation by ensuring that suppliers'
-constructors are only called if a supplied method is actually used. If none of
-the supplied methods is ever used, then there's no need to take the performance
-hit of calling the _supplier_'s constructor.
-
-@method augment
-@param {Function|Object} receiver Object or function to be augmented.
-@param {Function} supplier Function that supplies the prototype properties with
- which to augment the _receiver_.
-@param {Boolean} [overwrite=false] If `true`, properties already on the receiver
- will be overwritten if found on the supplier's prototype.
-@param {String[]} [whitelist] An array of property names. If specified,
- only the whitelisted prototype properties will be applied to the receiver, and
- all others will be ignored.
-@param {Array|any} [args] Argument or array of arguments to pass to the
- supplier's constructor when initializing.
-@return {Function} Augmented object.
-@for YUI
-**/
+ * Augments the _receiver_ with prototype properties from the _supplier_. The
+ * receiver may be a constructor function or an object. The supplier must be a
+ * constructor function.
+ *
+ * If the _receiver_ is an object, then the _supplier_ constructor will be called
+ * immediately after _receiver_ is augmented, with _receiver_ as the `this` object.
+ *
+ * If the _receiver_ is a constructor function, then all prototype methods of
+ * _supplier_ that are copied to _receiver_ will be sequestered, and the
+ * _supplier_ constructor will not be called immediately. The first time any
+ * sequestered method is called on the _receiver_'s prototype, all sequestered
+ * methods will be immediately copied to the _receiver_'s prototype, the
+ * _supplier_'s constructor will be executed, and finally the newly unsequestered
+ * method that was called will be executed.
+ *
+ * This sequestering logic sounds like a bunch of complicated voodoo, but it makes
+ * it cheap to perform frequent augmentation by ensuring that suppliers'
+ * constructors are only called if a supplied method is actually used. If none of
+ * the supplied methods is ever used, then there's no need to take the performance
+ * hit of calling the _supplier_'s constructor.
+ *
+ * @method augment
+ * @param {Function|Object} receiver Object or function to be augmented.
+ * @param {Function} supplier Function that supplies the prototype properties with
+ * which to augment the _receiver_.
+ * @param {Boolean} [overwrite=false] If `true`, properties already on the receiver
+ * will be overwritten if found on the supplier's prototype.
+ * @param {String[]} [whitelist] An array of property names. If specified,
+ * only the whitelisted prototype properties will be applied to the receiver, and
+ * all others will be ignored.
+ * @param {Array|any} [args] Argument or array of arguments to pass to the
+ * supplier's constructor when initializing.
+ * @return {Function} Augmented object.
+ * @for YUI
+ */
Y.augment = function (receiver, supplier, overwrite, whitelist, args) {
var rProto = receiver.prototype,
sequester = rProto && supplier,
@@ -210,7 +223,7 @@ Y.extend = function(r, s, px, sx) {
* @return {YUI} the YUI instance.
*/
Y.each = function(o, f, c, proto) {
- return dispatch(o, f, c, proto, 'each');
+ return Y.dispatchIterator('each', o, f, c, proto);
};
/**
@@ -229,7 +242,7 @@ Y.each = function(o, f, c, proto) {
* false otherwise.
*/
Y.some = function(o, f, c, proto) {
- return dispatch(o, f, c, proto, 'some');
+ return Y.dispatchIterator('some', o, f, c, proto);
};
/**
Please sign in to comment.
Something went wrong with that request. Please try again.