Commit ead6c5db ead6c5db985386523b95e5150709f61768f29107 by Nicolas Perriault

added utils.objectValues()

1 parent c6dd0e64
...@@ -608,9 +608,7 @@ Casper.prototype.evaluate = function evaluate(fn, context) { ...@@ -608,9 +608,7 @@ Casper.prototype.evaluate = function evaluate(fn, context) {
608 } else if (arguments.length === 2) { 608 } else if (arguments.length === 2) {
609 // check for closure signature if it matches context 609 // check for closure signature if it matches context
610 if (utils.isObject(context) && eval(fn).length === Object.keys(context).length) { 610 if (utils.isObject(context) && eval(fn).length === Object.keys(context).length) {
611 context = Object.keys(context).map(function(arg) { 611 context = utils.objectValues(context);
612 return context[arg];
613 });
614 } else { 612 } else {
615 context = [context]; 613 context = [context];
616 } 614 }
...@@ -619,9 +617,7 @@ Casper.prototype.evaluate = function evaluate(fn, context) { ...@@ -619,9 +617,7 @@ Casper.prototype.evaluate = function evaluate(fn, context) {
619 context = [].slice.call(arguments).slice(1); 617 context = [].slice.call(arguments).slice(1);
620 } else { 618 } else {
621 // old casperjs method signature 619 // old casperjs method signature
622 context = Object.keys(context).map(function(arg) { 620 context = utils.objectValues(context);
623 return context[arg];
624 });
625 } 621 }
626 return this.page.evaluate.apply(this.page, [fn].concat(context)); 622 return this.page.evaluate.apply(this.page, [fn].concat(context));
627 }; 623 };
......
...@@ -480,6 +480,19 @@ function node(name, attributes) { ...@@ -480,6 +480,19 @@ function node(name, attributes) {
480 exports.node = node; 480 exports.node = node;
481 481
482 /** 482 /**
483 * Maps an object to an array made from its values.
484 *
485 * @param Object obj
486 * @return Array
487 */
488 function objectValues(obj) {
489 return Object.keys(obj).map(function(arg) {
490 return obj[arg];
491 });
492 }
493 exports.objectValues = objectValues;
494
495 /**
483 * Serializes a value using JSON. 496 * Serializes a value using JSON.
484 * 497 *
485 * @param Mixed value 498 * @param Mixed value
......
...@@ -255,6 +255,12 @@ t.comment('mergeObjects()'); ...@@ -255,6 +255,12 @@ t.comment('mergeObjects()');
255 }); 255 });
256 })(); 256 })();
257 257
258 t.comment('objectValues()');
259 (function() {
260 t.assertEquals(utils.objectValues({}), [], 'objectValues() can extract object values');
261 t.assertEquals(utils.objectValues({a: 1, b: 2}), [1, 2], 'objectValues() can extract object values');
262 })();
263
258 t.comment('unique()'); 264 t.comment('unique()');
259 (function() { 265 (function() {
260 var testCases = [ 266 var testCases = [
......