autocast
Advanced tools
Comparing version 0.0.3 to 0.0.4
61
index.js
@@ -1,31 +0,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 && s.getTimezoneOffset) 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; | ||
}; | ||
// export | ||
if (typeof exports === 'undefined') { | ||
window.autocast = autocast; | ||
} else { | ||
module.exports = autocast; | ||
} | ||
// Give up | ||
return s; | ||
}; | ||
}()); |
{ | ||
"name": "autocast", | ||
"description": "Easily and automatically cast common datatypes in JavaScript", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"author": "Dave Eddy <dave@daveeddy.com> (http://www.daveeddy.com)", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -6,12 +6,7 @@ autocast | ||
Install | ||
------ | ||
Install locally to use as a module | ||
npm install autocast | ||
Usage | ||
----- | ||
Node.js | ||
``` js | ||
@@ -21,2 +16,9 @@ var autocast = require('autocast'); | ||
Web | ||
``` html | ||
<script src="autocast.min.js"></script> | ||
<!-- // defines autocast() --> | ||
``` | ||
Example | ||
@@ -26,4 +28,4 @@ ------- | ||
``` js | ||
var autocast = require('autocast'), | ||
x; | ||
var autocast = require('autocast'); | ||
var x; | ||
@@ -38,2 +40,6 @@ x = autocast('5') | ||
// x => null | ||
x = autocast('undefined') | ||
// x => undefined | ||
x = autocast('NaN') | ||
// x => NaN | ||
x = autocast('true') | ||
@@ -47,2 +53,7 @@ // x => true | ||
Install | ||
------ | ||
npm install autocast | ||
Tests | ||
@@ -49,0 +60,0 @@ ----- |
@@ -6,5 +6,4 @@ #!/usr/bin/env node | ||
var assert = require('assert'), | ||
autocast = require('../'), | ||
tmp; | ||
var assert = require('assert'); | ||
var autocast = require('../'); | ||
@@ -33,4 +32,4 @@ console.log('Testing Numbers...'); | ||
console.log('Testing dates...'); | ||
tmp = new Date(); | ||
var tmp = new Date(); | ||
assert.strictEqual(autocast(tmp), tmp); | ||
console.log('ok'); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
3370
7
62
63
0
1