Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bahamas10 committed Jun 22, 2012
0 parents commit 5b6f66c
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
npm-debug.log
node_modules
51 changes: 51 additions & 0 deletions README.md
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
25 changes: 25 additions & 0 deletions index.js
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;
};
19 changes: 19 additions & 0 deletions package.json
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": "*"
}
}
26 changes: 26 additions & 0 deletions tests/test.js
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);

0 comments on commit 5b6f66c

Please sign in to comment.