autocast
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -8,3 +8,4 @@ /** | ||
'undefined': undefined, | ||
'null': null | ||
'null': null, | ||
'NaN': NaN | ||
}; | ||
@@ -16,7 +17,9 @@ | ||
module.exports = function(s) { | ||
var key; | ||
// Try to cast it to a number | ||
if (+s) return +s; | ||
if ((key = +s) == key) return key; | ||
// Try to make it a common string | ||
for (var key in common_strings) { | ||
for (key in common_strings) { | ||
if (s === key) return common_strings[key]; | ||
@@ -23,0 +26,0 @@ } |
{ | ||
"author": "Dave Eddy <dave@daveeddy.com> (http://www.daveeddy.com)", | ||
"name": "autocast", | ||
"description": "Easily and automatically cast common datatypes in JavaScript", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"author": "Dave Eddy <dave@daveeddy.com> (http://www.daveeddy.com)", | ||
"contributors": [ | ||
"Kit Cambridge <kitcambridge@me.com>" | ||
], | ||
"repository": { | ||
@@ -7,0 +10,0 @@ "type": "git", |
@@ -7,21 +7,23 @@ #!/usr/bin/env node | ||
var assert = require('assert'), | ||
autocast = require('../'), | ||
str_int = '5', | ||
str_float = '5.6', | ||
str_fakenumber = '5.6.7', | ||
str_false = 'false', | ||
str_true = 'true', | ||
str_null = 'null', | ||
str_undefined = 'undefined', | ||
str_normalstr = 'Just a string'; | ||
autocast = require('../'); | ||
console.log('Testing Numbers'); | ||
assert.strictEqual(autocast(str_int), 5); | ||
assert.strictEqual(autocast(str_float), 5.6); | ||
assert.strictEqual(autocast(str_fakenumber), '5.6.7'); | ||
console.log('Testing Numbers...'); | ||
assert.strictEqual(autocast('5'), 5); | ||
assert.strictEqual(autocast('5.6'), 5.6); | ||
assert.strictEqual(autocast('5.6.7'), '5.6.7'); | ||
assert.strictEqual(autocast('0'), 0); | ||
assert.strictEqual(typeof autocast('NaN'), 'number'); | ||
assert.ok(isNaN(autocast('NaN'))); | ||
console.log('ok'); | ||
console.log('Testing common data types'); | ||
assert.strictEqual(autocast(str_false), false); | ||
assert.strictEqual(autocast(str_true), true); | ||
assert.strictEqual(autocast(str_null), null); | ||
assert.strictEqual(autocast(str_undefined), undefined); | ||
console.log('Testing common data types...'); | ||
assert.strictEqual(autocast('false'), false); | ||
assert.strictEqual(autocast('true'), true); | ||
assert.strictEqual(autocast('null'), null); | ||
assert.strictEqual(autocast('undefined'), undefined); | ||
console.log('ok'); | ||
console.log('Testing strings...'); | ||
assert.strictEqual(autocast('some string'), 'some string'); | ||
assert.strictEqual(autocast('!'), '!'); | ||
console.log('ok'); |
2507
48