Skip to content

Commit

Permalink
Adding support for use on the web
Browse files Browse the repository at this point in the history
  • Loading branch information
bahamas10 committed Dec 9, 2012
1 parent 6e96aac commit d9af96c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
5 changes: 5 additions & 0 deletions Makefile
@@ -0,0 +1,5 @@
default: min
min:
uglifyjs -cm < index.js > autocast.min.js
clean:
rm -f autocast.min.js
1 change: 1 addition & 0 deletions autocast.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 35 additions & 26 deletions index.js
@@ -1,31 +1,40 @@
/**
* Common strings to cast
*/
var common_strings = {
'true': true,
'false': false,
'undefined': undefined,
'null': null,
'NaN': NaN
};
(function() {
/**
* Common strings to cast
*/
var common_strings = {
'true': true,
'false': false,
'undefined': undefined,
'null': null,
'NaN': NaN
};

/**
* Given a value, try and cast it
*/
module.exports = function(s) {
var key;
/**
* Given a value, try and cast it
*/
function autocast(s) {
var key;

// Don't cast Date objects
if (s instanceof Date) return s;
// Don't cast Date objects
if (s instanceof Date) return s;

// Try to cast it to a number
if ((key = +s) == key) return key;
// Try to cast it to a number
if ((key = +s) == key) return key;

// Try to make it a common string
for (key in common_strings) {
if (s === key) return common_strings[key];
}
// Try to make it a common string
for (key in common_strings) {
if (s === key) return common_strings[key];
}

// Give up
return s;
};

// Give up
return s;
};
// export
if (typeof exports === 'undefined') {
window.autocast = autocast;
} else {
module.exports = autocast;
}
}());

0 comments on commit d9af96c

Please sign in to comment.