Commit 0cb73a8a 0cb73a8a0c979c2309f56eb5a130c02cbe35e0ae by Nicolas Perriault

enhanced utils.betterTypeOf() to handle `undefined` and `null` values

1 parent 33723622
......@@ -31,7 +31,8 @@ casper.evaluate(function(a, b) {
- fixed [#274](https://github.com/n1k0/casperjs/issues/274) - some headers couldn't be set
- fixed [#277](https://github.com/n1k0/casperjs/issues/277) - multiline support in `ClientUtils.echo()`
- fixed [#282](https://github.com/n1k0/casperjs/issues/282) - added support for remote client scripts loading with a new `remoteScripts` casper option
- merged [#290](https://github.com/n1k0/casperjs/issues/#290) - add a simplistic RPM spec file to make it easier to (un)install casperjs
- fixed [#290](https://github.com/n1k0/casperjs/issues/#290) - add a simplistic RPM spec file to make it easier to (un)install casperjs
- fixed [`utils.betterTypeOf()`](http://casperjs.org/api.html#casper.betterTypeOf) to properly handle `undefined` and `null` values
- fixed `Casper.die()` and `Casper.evaluateOrDie()` were not printing the error onto the console
- added [`Casper.getFormValues()`](http://casperjs.org/api.html#casper.getFormValues) to check for the field values of a given form
- added [`Tester.assertTextDoesntExist()`](http://casperjs.org/api.html#tester.assertTextDoesntExist)
......
......@@ -34,16 +34,26 @@
* Provides a better typeof operator equivalent, able to retrieve the array
* type.
*
* CAVEAT: this function does not necessarilly map to classical js "type" names,
* notably a `null` will map to "null" instead of "object".
*
* @param mixed input
* @return String
* @see http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
*/
function betterTypeOf(input) {
"use strict";
try {
return Object.prototype.toString.call(input).match(/^\[object\s(.*)\]$/)[1].toLowerCase();
} catch (e) {
return typeof input;
switch (input) {
case undefined:
return 'undefined';
case null:
return 'null';
default:
try {
return Object.prototype.toString.call(input).match(/^\[object\s(.*)\]$/)[1].toLowerCase();
} catch (e) {
return typeof input;
}
}
}
exports.betterTypeOf = betterTypeOf;
......
......@@ -4,6 +4,26 @@ var utils = require('utils'),
t = casper.test,
x = require('casper').selectXPath;
t.comment('betterTypeOf()');
(function() {
var testCases = [
{subject: 1, expected: 'number'},
{subject: '1', expected: 'string'},
{subject: {}, expected: 'object'},
{subject: [], expected: 'array'},
{subject: undefined, expected: 'undefined'},
{subject: null, expected: 'null'},
{subject: function(){}, expected: 'function'},
{subject: window, expected: 'domwindow'},
{subject: new Date(), expected: 'date'},
{subject: new RegExp(), expected: 'regexp'}
];
testCases.forEach(function(testCase) {
t.assertEquals(utils.betterTypeOf(testCase.subject), testCase.expected,
require('utils').format('betterTypeOf() detects expected type "%s"', testCase.subject));
});
})();
t.comment('cleanUrl()');
(function() {
var testCases = {
......