Socket
Socket
Sign inDemoInstall

autocast

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.3 to 0.0.4

autocast.min.js

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');
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc