Commit 930a75d7 930a75d73808c9e730b2901923a411e63dfd0732 by Nicolas Perriault

changed uses of typeof for betterTypeof; also created isType() helper for easily check types

1 parent 77836645
Showing 1 changed file with 51 additions and 38 deletions
...@@ -117,7 +117,7 @@ ...@@ -117,7 +117,7 @@
117 capture: function(targetFile, clipRect) { 117 capture: function(targetFile, clipRect) {
118 var previousClipRect; 118 var previousClipRect;
119 if (clipRect) { 119 if (clipRect) {
120 if (typeof clipRect !== "object") { 120 if (!isType(clipRect, "object")) {
121 throw new Error("clipRect must be an Object instance."); 121 throw new Error("clipRect must be an Object instance.");
122 } 122 }
123 previousClipRect = this.page.clipRect; 123 previousClipRect = this.page.clipRect;
...@@ -170,7 +170,7 @@ ...@@ -170,7 +170,7 @@
170 */ 170 */
171 checkStep: function(self, onComplete) { 171 checkStep: function(self, onComplete) {
172 var step = self.steps[self.step]; 172 var step = self.steps[self.step];
173 if (!self.loadInProgress && typeof step === "function") { 173 if (!self.loadInProgress && isType(step, "function")) {
174 var curStepNum = self.step + 1; 174 var curStepNum = self.step + 1;
175 var stepInfo = "Step " + curStepNum + "/" + self.steps.length + ": "; 175 var stepInfo = "Step " + curStepNum + "/" + self.steps.length + ": ";
176 self.log(stepInfo + self.page.evaluate(function() { 176 self.log(stepInfo + self.page.evaluate(function() {
...@@ -189,11 +189,11 @@ ...@@ -189,11 +189,11 @@
189 self.log(stepInfo + "done in " + time + "ms.", "info"); 189 self.log(stepInfo + "done in " + time + "ms.", "info");
190 self.step++; 190 self.step++;
191 } 191 }
192 if (typeof step !== "function" && !self.delayedExecution) { 192 if (!isType(step, "function") && !self.delayedExecution) {
193 self.result.time = new Date().getTime() - self.startTime; 193 self.result.time = new Date().getTime() - self.startTime;
194 self.log("Done " + self.steps.length + " steps in " + self.result.time + 'ms.', "info"); 194 self.log("Done " + self.steps.length + " steps in " + self.result.time + 'ms.', "info");
195 clearInterval(self.checker); 195 clearInterval(self.checker);
196 if (typeof onComplete === "function") { 196 if (isType(onComplete, "function")) {
197 try { 197 try {
198 onComplete(self); 198 onComplete(self);
199 } catch (err) { 199 } catch (err) {
...@@ -215,7 +215,7 @@ ...@@ -215,7 +215,7 @@
215 * @return Boolean 215 * @return Boolean
216 */ 216 */
217 click: function(selector, fallbackToHref) { 217 click: function(selector, fallbackToHref) {
218 fallbackToHref = typeof(fallbackToHref) == "undefined" ? true : !!fallbackToHref; 218 fallbackToHref = isType(fallbackToHref, "undefined") ? true : !!fallbackToHref;
219 this.log("click on selector: " + selector, "debug"); 219 this.log("click on selector: " + selector, "debug");
220 return this.evaluate(function() { 220 return this.evaluate(function() {
221 return __utils__.click(__casper_params__.selector, __casper_params__.fallbackToHref); 221 return __utils__.click(__casper_params__.selector, __casper_params__.fallbackToHref);
...@@ -259,9 +259,9 @@ ...@@ -259,9 +259,9 @@
259 die: function(message, status) { 259 die: function(message, status) {
260 this.result.status = 'error'; 260 this.result.status = 'error';
261 this.result.time = new Date().getTime() - this.startTime; 261 this.result.time = new Date().getTime() - this.startTime;
262 message = typeof message === "string" && message.length > 0 ? message : DEFAULT_DIE_MESSAGE; 262 message = isType(message, "string") && message.length > 0 ? message : DEFAULT_DIE_MESSAGE;
263 this.log(message, "error"); 263 this.log(message, "error");
264 if (typeof this.options.onDie === "function") { 264 if (isType(this.options.onDie, "function")) {
265 this.options.onDie(this, message, status); 265 this.options.onDie(this, message, status);
266 } 266 }
267 return this.exit(Number(status) > 0 ? Number(status) : 1); 267 return this.exit(Number(status) > 0 ? Number(status) : 1);
...@@ -335,7 +335,7 @@ ...@@ -335,7 +335,7 @@
335 * @see WebPage#evaluate 335 * @see WebPage#evaluate
336 */ 336 */
337 evaluate: function(fn, replacements) { 337 evaluate: function(fn, replacements) {
338 replacements = typeof(replacements) === "object" ? replacements : {}; 338 replacements = isType(replacements, "object") ? replacements : {};
339 this.page.evaluate(replaceFunctionPlaceholders(function() { 339 this.page.evaluate(replaceFunctionPlaceholders(function() {
340 window.__casper_params__ = {}; 340 window.__casper_params__ = {};
341 try { 341 try {
...@@ -415,10 +415,10 @@ ...@@ -415,10 +415,10 @@
415 */ 415 */
416 fill: function(selector, vals, submit) { 416 fill: function(selector, vals, submit) {
417 submit = submit === true ? submit : false; 417 submit = submit === true ? submit : false;
418 if (typeof selector !== "string" || !selector.length) { 418 if (!isType(selector, "string") || !selector.length) {
419 throw "form selector must be a non-empty string"; 419 throw "form selector must be a non-empty string";
420 } 420 }
421 if (typeof vals !== "object") { 421 if (!isType(vals, "object")) {
422 throw "form values must be provided as an object"; 422 throw "form values must be provided as an object";
423 } 423 }
424 var fillResults = this.evaluate(function() { 424 var fillResults = this.evaluate(function() {
...@@ -496,7 +496,7 @@ ...@@ -496,7 +496,7 @@
496 log: function(message, level, space) { 496 log: function(message, level, space) {
497 level = level && this.logLevels.indexOf(level) > -1 ? level : "debug"; 497 level = level && this.logLevels.indexOf(level) > -1 ? level : "debug";
498 space = space ? space : "phantom"; 498 space = space ? space : "phantom";
499 if (level === "error" && typeof this.options.onError === "function") { 499 if (level === "error" && isType(this.options.onError, "function")) {
500 this.options.onError(this, message, space); 500 this.options.onError(this, message, space);
501 } 501 }
502 if (this.logLevels.indexOf(level) < this.logLevels.indexOf(this.options.logLevel)) { 502 if (this.logLevels.indexOf(level) < this.logLevels.indexOf(this.options.logLevel)) {
...@@ -590,25 +590,25 @@ ...@@ -590,25 +590,25 @@
590 } 590 }
591 } 591 }
592 this.page.settings = mergeObjects(this.page.settings, this.options.pageSettings); 592 this.page.settings = mergeObjects(this.page.settings, this.options.pageSettings);
593 if (typeof this.options.clipRect === "object") { 593 if (isType(this.options.clipRect, "object")) {
594 this.page.clipRect = this.options.clipRect; 594 this.page.clipRect = this.options.clipRect;
595 } 595 }
596 if (typeof this.options.viewportSize === "object") { 596 if (isType(this.options.viewportSize, "object")) {
597 this.page.viewportSize = this.options.viewportSize; 597 this.page.viewportSize = this.options.viewportSize;
598 } 598 }
599 this.started = true; 599 this.started = true;
600 if (typeof this.options.timeout === "number" && this.options.timeout > 0) { 600 if (isType(this.options.timeout, "number") && this.options.timeout > 0) {
601 self.log("execution timeout set to " + this.options.timeout + 'ms', "info"); 601 self.log("execution timeout set to " + this.options.timeout + 'ms', "info");
602 setTimeout(function(self) { 602 setTimeout(function(self) {
603 self.log("timeout of " + self.options.timeout + "ms exceeded", "info").exit(); 603 self.log("timeout of " + self.options.timeout + "ms exceeded", "info").exit();
604 }, this.options.timeout, this); 604 }, this.options.timeout, this);
605 } 605 }
606 if (typeof this.options.onPageInitialized === "function") { 606 if (isType(this.options.onPageInitialized, "function")) {
607 this.log("Post-configuring WebPage instance", "debug"); 607 this.log("Post-configuring WebPage instance", "debug");
608 this.options.onPageInitialized(this.page); 608 this.options.onPageInitialized(this.page);
609 } 609 }
610 if (typeof location === "string" && location.length > 0) { 610 if (isType(location, "string") && location.length > 0) {
611 if (typeof then === "function") { 611 if (isType(then, "function")) {
612 return this.open(location).then(then); 612 return this.open(location).then(then);
613 } else { 613 } else {
614 return this.open(location); 614 return this.open(location);
...@@ -627,7 +627,7 @@ ...@@ -627,7 +627,7 @@
627 if (!this.started) { 627 if (!this.started) {
628 throw "Casper not started; please use Casper#start"; 628 throw "Casper not started; please use Casper#start";
629 } 629 }
630 if (typeof step !== "function") { 630 if (!isType(step, "function")) {
631 throw "You can only define a step as a function"; 631 throw "You can only define a step as a function";
632 } 632 }
633 this.steps.push(step); 633 this.steps.push(step);
...@@ -649,7 +649,7 @@ ...@@ -649,7 +649,7 @@
649 this.then(function(self) { 649 this.then(function(self) {
650 self.click(selector, fallbackToHref); 650 self.click(selector, fallbackToHref);
651 }); 651 });
652 return typeof then === "function" ? this.then(then) : this; 652 return isType(then, "function") ? this.then(then) : this;
653 }, 653 },
654 654
655 /** 655 /**
...@@ -679,7 +679,7 @@ ...@@ -679,7 +679,7 @@
679 this.then(function(self) { 679 this.then(function(self) {
680 self.open(location); 680 self.open(location);
681 }); 681 });
682 return typeof then === "function" ? this.then(then) : this; 682 return isType(then, "function") ? this.then(then) : this;
683 }, 683 },
684 684
685 /** 685 /**
...@@ -705,7 +705,7 @@ ...@@ -705,7 +705,7 @@
705 * @return Casper 705 * @return Casper
706 */ 706 */
707 viewport: function(width, height) { 707 viewport: function(width, height) {
708 if (typeof width !== "number" || typeof height !== "number" || width <= 0 || height <= 0) { 708 if (!isType(width, "number") || !isType(height, "number") || width <= 0 || height <= 0) {
709 throw new Error("Invalid viewport width/height set: " + width + 'x' + height); 709 throw new Error("Invalid viewport width/height set: " + width + 'x' + height);
710 } 710 }
711 this.page.viewportSize = { 711 this.page.viewportSize = {
...@@ -724,10 +724,11 @@ ...@@ -724,10 +724,11 @@
724 * @return Casper 724 * @return Casper
725 */ 725 */
726 wait: function(timeout, then) { 726 wait: function(timeout, then) {
727 if (typeof(timeout) !== "number" || Number(timeout, 10) < 1) { 727 timeout = Number(timeout, 10);
728 if (!isType(timeout, "number") || timeout < 1) {
728 this.die("wait() only accepts a positive integer > 0 as a timeout value"); 729 this.die("wait() only accepts a positive integer > 0 as a timeout value");
729 } 730 }
730 if (then && typeof(then) !== "function") { 731 if (then && !isType(then, "function")) {
731 this.die("wait() a step definition must be a function"); 732 this.die("wait() a step definition must be a function");
732 } 733 }
733 return this.then(function(self) { 734 return this.then(function(self) {
...@@ -757,10 +758,10 @@ ...@@ -757,10 +758,10 @@
757 */ 758 */
758 waitFor: function(testFx, then, onTimeout, timeout) { 759 waitFor: function(testFx, then, onTimeout, timeout) {
759 timeout = timeout ? timeout : this.defaultWaitTimeout; 760 timeout = timeout ? timeout : this.defaultWaitTimeout;
760 if (typeof testFx !== "function") { 761 if (!isType(testFx, "function")) {
761 this.die("waitFor() needs a test function"); 762 this.die("waitFor() needs a test function");
762 } 763 }
763 if (then && typeof then !== "function") { 764 if (then && !isType(then, "function")) {
764 this.die("waitFor() next step definition must be a function"); 765 this.die("waitFor() next step definition must be a function");
765 } 766 }
766 this.delayedExecution = true; 767 this.delayedExecution = true;
...@@ -773,7 +774,7 @@ ...@@ -773,7 +774,7 @@
773 self.delayedExecution = false; 774 self.delayedExecution = false;
774 if (!condition) { 775 if (!condition) {
775 self.log("Casper.waitFor() timeout", "warning"); 776 self.log("Casper.waitFor() timeout", "warning");
776 if (typeof onTimeout === "function") { 777 if (isType(onTimeout, "function")) {
777 onTimeout(self); 778 onTimeout(self);
778 } else { 779 } else {
779 self.die("Expired timeout, exiting.", "error"); 780 self.die("Expired timeout, exiting.", "error");
...@@ -815,8 +816,8 @@ ...@@ -815,8 +816,8 @@
815 * @param Object proto Prototype methods to add to Casper 816 * @param Object proto Prototype methods to add to Casper
816 */ 817 */
817 phantom.Casper.extend = function(proto) { 818 phantom.Casper.extend = function(proto) {
818 if (typeof proto !== "object") { 819 if (!isType(proto, "object")) {
819 throw "extends() only accept objects"; 820 throw "extends() only accept objects as prototypes";
820 } 821 }
821 mergeObjects(phantom.Casper.prototype, proto); 822 mergeObjects(phantom.Casper.prototype, proto);
822 }; 823 };
...@@ -833,7 +834,7 @@ ...@@ -833,7 +834,7 @@
833 * @return Boolean 834 * @return Boolean
834 */ 835 */
835 this.click = function(selector, fallbackToHref) { 836 this.click = function(selector, fallbackToHref) {
836 fallbackToHref = typeof(fallbackToHref) == "undefined" ? true : !!fallbackToHref; 837 fallbackToHref = typeof fallbackToHref === "undefined" ? true : !!fallbackToHref;
837 var elem = this.findOne(selector); 838 var elem = this.findOne(selector);
838 if (!elem) { 839 if (!elem) {
839 return false; 840 return false;
...@@ -1196,7 +1197,7 @@ ...@@ -1196,7 +1197,7 @@
1196 * 1197 *
1197 */ 1198 */
1198 phantom.Casper.Tester = function(casper, options) { 1199 phantom.Casper.Tester = function(casper, options) {
1199 this.options = typeof options === "object" || {}; 1200 this.options = isType(options, "object") ? options : {};
1200 if (!casper instanceof phantom.Casper) { 1201 if (!casper instanceof phantom.Casper) {
1201 throw "phantom.Casper.Tester needs a phantom.Casper instance"; 1202 throw "phantom.Casper.Tester needs a phantom.Casper instance";
1202 } 1203 }
...@@ -1432,7 +1433,7 @@ ...@@ -1432,7 +1433,7 @@
1432 * @param Boolean exit 1433 * @param Boolean exit
1433 */ 1434 */
1434 this.renderResults = function(exit, status, save) { 1435 this.renderResults = function(exit, status, save) {
1435 save = typeof save === "string" ? save : this.options.save; 1436 save = isType(save, "string") ? save : this.options.save;
1436 var total = this.testResults.passed + this.testResults.failed, statusText, style, result; 1437 var total = this.testResults.passed + this.testResults.failed, statusText, style, result;
1437 if (this.testResults.failed > 0) { 1438 if (this.testResults.failed > 0) {
1438 statusText = FAIL; 1439 statusText = FAIL;
...@@ -1446,7 +1447,7 @@ ...@@ -1446,7 +1447,7 @@
1446 result += new Array(80 - result.length + 1).join(' '); 1447 result += new Array(80 - result.length + 1).join(' ');
1447 } 1448 }
1448 casper.echo(this.colorize(result, style)); 1449 casper.echo(this.colorize(result, style));
1449 if (save && typeof require === "function") { 1450 if (save && isType(require, "function")) {
1450 try { 1451 try {
1451 require('fs').write(save, exporter.getXML(), 'w'); 1452 require('fs').write(save, exporter.getXML(), 'w');
1452 casper.echo('result log stored in ' + save, 'INFO'); 1453 casper.echo('result log stored in ' + save, 'INFO');
...@@ -1469,7 +1470,7 @@ ...@@ -1469,7 +1470,7 @@
1469 var node = document.createElement(name); 1470 var node = document.createElement(name);
1470 for (var attrName in attributes) { 1471 for (var attrName in attributes) {
1471 var value = attributes[attrName]; 1472 var value = attributes[attrName];
1472 if (attributes.hasOwnProperty(attrName) && typeof attrName === "string") { 1473 if (attributes.hasOwnProperty(attrName) && isType(attrName, "string")) {
1473 node.setAttribute(attrName, value); 1474 node.setAttribute(attrName, value);
1474 } 1475 }
1475 } 1476 }
...@@ -1549,7 +1550,7 @@ ...@@ -1549,7 +1550,7 @@
1549 */ 1550 */
1550 function createPage(casper) { 1551 function createPage(casper) {
1551 var page; 1552 var page;
1552 if (phantom.version.major <= 1 && phantom.version.minor < 3 && typeof require === "function") { 1553 if (phantom.version.major <= 1 && phantom.version.minor < 3 && isType(require, "function")) {
1553 page = new WebPage(); 1554 page = new WebPage();
1554 } else { 1555 } else {
1555 page = require('webpage').create(); 1556 page = require('webpage').create();
...@@ -1573,7 +1574,7 @@ ...@@ -1573,7 +1574,7 @@
1573 } 1574 }
1574 message += ': ' + casper.requestUrl; 1575 message += ': ' + casper.requestUrl;
1575 casper.log(message, "warning"); 1576 casper.log(message, "warning");
1576 if (typeof casper.options.onLoadError === "function") { 1577 if (isType(casper.options.onLoadError, "function")) {
1577 casper.options.onLoadError(casper, casper.requestUrl, status); 1578 casper.options.onLoadError(casper, casper.requestUrl, status);
1578 } 1579 }
1579 } 1580 }
...@@ -1616,16 +1617,28 @@ ...@@ -1616,16 +1617,28 @@
1616 } 1617 }
1617 1618
1618 /** 1619 /**
1620 * Shorthands for checking if a value is of the given type. Can check for
1621 * arrays.
1622 *
1623 * @param mixed what The value to check
1624 * @param String typeName The type name ("string", "number", "function", etc.)
1625 * @return Boolean
1626 */
1627 function isType(what, typeName) {
1628 return betterTypeOf(what) === typeName;
1629 }
1630
1631 /**
1619 * Checks if the provided var is a WebPage instance 1632 * Checks if the provided var is a WebPage instance
1620 * 1633 *
1621 * @param mixed what 1634 * @param mixed what
1622 * @return Boolean 1635 * @return Boolean
1623 */ 1636 */
1624 function isWebPage(what) { 1637 function isWebPage(what) {
1625 if (!what || typeof what !== "object") { 1638 if (!what || !isType(what, "object")) {
1626 return false; 1639 return false;
1627 } 1640 }
1628 if (phantom.version.major <= 1 && phantom.version.minor < 3 && typeof require === "function") { 1641 if (phantom.version.major <= 1 && phantom.version.minor < 3 && isType(require, "function")) {
1629 return what instanceof WebPage; 1642 return what instanceof WebPage;
1630 } else { 1643 } else {
1631 return what.toString().indexOf('WebPage(') === 0; 1644 return what.toString().indexOf('WebPage(') === 0;
...@@ -1663,7 +1676,7 @@ ...@@ -1663,7 +1676,7 @@
1663 * @return String A function string representation 1676 * @return String A function string representation
1664 */ 1677 */
1665 function replaceFunctionPlaceholders(fn, replacements) { 1678 function replaceFunctionPlaceholders(fn, replacements) {
1666 if (replacements && typeof replacements === "object") { 1679 if (replacements && isType(replacements, "object")) {
1667 fn = fn.toString(); 1680 fn = fn.toString();
1668 for (var placeholder in replacements) { 1681 for (var placeholder in replacements) {
1669 var match = '%' + placeholder + '%'; 1682 var match = '%' + placeholder + '%';
......