Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial Commit

  • Loading branch information...
commit 5b6f66c107abc9cd7064e149c2798f85a1fd4a19 0 parents
@bahamas10 authored
Showing with 123 additions and 0 deletions.
  1. +2 −0  .gitignore
  2. +51 −0 README.md
  3. +25 −0 index.js
  4. +19 −0 package.json
  5. +26 −0 tests/test.js
2  .gitignore
@@ -0,0 +1,2 @@
+npm-debug.log
+node_modules
51 README.md
@@ -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 index.js
@@ -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 package.json
@@ -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 tests/test.js
@@ -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);
Please sign in to comment.
Something went wrong with that request. Please try again.