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
Showing
3 changed files
with
41 additions
and
26 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,5 @@ | ||
default: min | ||
min: | ||
uglifyjs -cm < index.js > autocast.min.js | ||
clean: | ||
rm -f autocast.min.js |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -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; | ||
} | ||
}()); |