Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial Commit

  • Loading branch information...
commit 5b6f66c107abc9cd7064e149c2798f85a1fd4a19 0 parents
Dave Eddy authored
2  .gitignore
... ... @@ -0,0 +1,2 @@
  1 +npm-debug.log
  2 +node_modules
51 README.md
Source Rendered
... ... @@ -0,0 +1,51 @@
  1 +autocast
  2 +========
  3 +
  4 +Easily and automatically cast common datatypes in JavaScript
  5 +
  6 +Install
  7 +------
  8 +
  9 +Install locally to use as a madule
  10 +
  11 + npm install autocast
  12 +
  13 +Usage
  14 +-----
  15 +
  16 +``` js
  17 +var autocast = require('autocast');
  18 +```
  19 +
  20 +Example
  21 +-------
  22 +
  23 +``` js
  24 +var autocast = require('autocast'),
  25 + x;
  26 +
  27 +x = autocast('5')
  28 +// x => 5
  29 +x = autocast('5.8')
  30 +// x => 5.8
  31 +x = autocast('5.8.8')
  32 +// x => '5.8.8'
  33 +x = autocast('null')
  34 +// x => null
  35 +x = autocast('true')
  36 +// x => true
  37 +x = autocast('false')
  38 +// x => false
  39 +x = autocast('normal string')
  40 +// x => 'normal string'
  41 +```
  42 +
  43 +Tests
  44 +-----
  45 +
  46 + npm test
  47 +
  48 +License
  49 +-------
  50 +
  51 +MIT Licensed
25 index.js
... ... @@ -0,0 +1,25 @@
  1 +/**
  2 + * Common strings to cast
  3 + */
  4 +var common_strings = {
  5 + 'true': true,
  6 + 'false': false,
  7 + 'undefined': undefined,
  8 + 'null': null
  9 +};
  10 +
  11 +/**
  12 + * Given a value, try and cast it
  13 + */
  14 +module.exports = function(s) {
  15 + // Try to cast it to a number
  16 + if (+s) return +s;
  17 +
  18 + // Try to make it a comman string
  19 + for (var key in common_strings) {
  20 + if (s === key) return common_strings[key];
  21 + }
  22 +
  23 + // Give up
  24 + return s;
  25 +};
19 package.json
... ... @@ -0,0 +1,19 @@
  1 +{
  2 + "author": "Dave Eddy <dave@daveeddy.com> (http://www.daveeddy.com)",
  3 + "name": "autocast",
  4 + "description": "Easily and automatically cast common datatypes in JavaScript",
  5 + "version": "0.0.0",
  6 + "repository": {
  7 + "type": "git",
  8 + "url": "git://github.com/bahamas10/node-autocast.git"
  9 + },
  10 + "scripts": {
  11 + "test": "node tests/test.js"
  12 + },
  13 + "dependencies": {},
  14 + "devDependencies": {},
  15 + "optionalDependencies": {},
  16 + "engines": {
  17 + "node": "*"
  18 + }
  19 +}
26 tests/test.js
... ... @@ -0,0 +1,26 @@
  1 +#!/usr/bin/env node
  2 +/**
  3 + * Tests for autocast
  4 + */
  5 +
  6 +var assert = require('assert'),
  7 + autocast = require('../'),
  8 + str_int = '5',
  9 + str_float = '5.6',
  10 + str_fakenumber = '5.6.7',
  11 + str_false = 'false',
  12 + str_true = 'true',
  13 + str_null = 'null',
  14 + str_undefined = 'undefined',
  15 + str_normalstr = 'Just a string';
  16 +
  17 +console.log('Testing Numbers');
  18 +assert.strictEqual(autocast(str_int), 5);
  19 +assert.strictEqual(autocast(str_float), 5.6);
  20 +assert.strictEqual(autocast(str_fakenumber), '5.6.7');
  21 +
  22 +console.log('Testing common data types');
  23 +assert.strictEqual(autocast(str_false), false);
  24 +assert.strictEqual(autocast(str_true), true);
  25 +assert.strictEqual(autocast(str_null), null);
  26 +assert.strictEqual(autocast(str_undefined), undefined);

0 comments on commit 5b6f66c

Please sign in to comment.
Something went wrong with that request. Please try again.