-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5b6f66c
Showing
5 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
npm-debug.log | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
autocast | ||
======== | ||
|
||
Easily and automatically cast common datatypes in JavaScript | ||
|
||
Install | ||
------ | ||
|
||
Install locally to use as a madule | ||
|
||
npm install autocast | ||
|
||
Usage | ||
----- | ||
|
||
``` js | ||
var autocast = require('autocast'); | ||
``` | ||
|
||
Example | ||
------- | ||
|
||
``` js | ||
var autocast = require('autocast'), | ||
x; | ||
|
||
x = autocast('5') | ||
// x => 5 | ||
x = autocast('5.8') | ||
// x => 5.8 | ||
x = autocast('5.8.8') | ||
// x => '5.8.8' | ||
x = autocast('null') | ||
// x => null | ||
x = autocast('true') | ||
// x => true | ||
x = autocast('false') | ||
// x => false | ||
x = autocast('normal string') | ||
// x => 'normal string' | ||
``` | ||
|
||
Tests | ||
----- | ||
|
||
npm test | ||
|
||
License | ||
------- | ||
|
||
MIT Licensed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Common strings to cast | ||
*/ | ||
var common_strings = { | ||
'true': true, | ||
'false': false, | ||
'undefined': undefined, | ||
'null': null | ||
}; | ||
|
||
/** | ||
* Given a value, try and cast it | ||
*/ | ||
module.exports = function(s) { | ||
// Try to cast it to a number | ||
if (+s) return +s; | ||
|
||
// Try to make it a comman string | ||
for (var key in common_strings) { | ||
if (s === key) return common_strings[key]; | ||
} | ||
|
||
// Give up | ||
return s; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"author": "Dave Eddy <dave@daveeddy.com> (http://www.daveeddy.com)", | ||
"name": "autocast", | ||
"description": "Easily and automatically cast common datatypes in JavaScript", | ||
"version": "0.0.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/bahamas10/node-autocast.git" | ||
}, | ||
"scripts": { | ||
"test": "node tests/test.js" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"optionalDependencies": {}, | ||
"engines": { | ||
"node": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env node | ||
/** | ||
* Tests for autocast | ||
*/ | ||
|
||
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'; | ||
|
||
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 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); |