Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tiny-coerce

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tiny-coerce - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

102

lib/tiny-coerce.js

@@ -1,3 +0,1 @@

"use strict";
/**

@@ -7,44 +5,76 @@ * Tiny coercion library for Client or Server

* @author Jason Mulligan <jason.mulligan@avoidwork.com>
* @copyright 2015
* @copyright 2018
* @license BSD-3-Clause
* @link http://avoidwork.github.io/tiny-coerce
* @version 1.0.1
* @version 1.1.0
*/
(function (global) {
const regex = {
false: /^(F|f)alse$/,
null: /^(N|n)ull$/,
json: /^["\[{].*[}\]"]$/,
true: /^(T|t)rue$/
};
var jsonWrap = /^[\[\{]/;
let coerce, walk;
function coerce(value) {
var result = undefined,
tmp = undefined;
walk = arg => {
if (Array.isArray(arg)) {
arg = arg.forEach((i, idx) => {
arg[idx] = coerce(i, true);
});
} else if (arg instanceof Object) {
Object.keys(arg).forEach(key => {
arg[key] = coerce(arg[key], true);
});
}
};
if (value === null || value === undefined) {
result = undefined;
} else if (value === "true") {
result = true;
} else if (value === "false") {
result = false;
} else if (value === "null") {
result = null;
} else if (value === "undefined") {
result = undefined;
} else if (value === "") {
result = value;
} else if (!isNaN(tmp = Number(value))) {
result = tmp;
} else if (jsonWrap.test(value)) {
try {
result = JSON.parse(value);
} catch (e) {
console.warn(e);
coerce = (arg, deep = false) => {
let result;
if (typeof arg !== "string") {
result = arg;
if (deep) {
walk(result);
}
} else {
const value = arg.trim();
let tmp;
if (value.length === 0) {
result = value;
} else if (regex.true.test(value)) {
result = true;
} else if (regex.false.test(value)) {
result = false;
} else if (regex.null.test(value)) {
result = null;
} else if (value === "undefined") {
result = undefined;
} else if (!isNaN(tmp = Number(value))) {
result = tmp;
} else if (regex.json.test(value)) {
let valid;
try {
result = JSON.parse(value);
valid = true;
} catch (e) {
result = value;
valid = false;
}
if (valid && deep) {
walk(result);
}
} else {
result = value;
}
} else {
result = value;
}
return result;
}
};
coerce.version = "1.0.1";
coerce.version = "1.1.0";

@@ -54,9 +84,7 @@ // Node, AMD & window supported

module.exports = coerce;
} else if (typeof define === "function" && define.amd) {
define(function () {
return coerce;
});
} else if (typeof define === "function" && define.amd !== void 0) {
define(() => coerce);
} else {
global.coerce = coerce;
}
})(typeof window !== "undefined" ? window : global);
}(typeof window !== "undefined" ? window : global));
{
"name": "tiny-coerce",
"version": "1.0.1",
"version": "1.1.0",
"description": "Tiny coercion library for Client or Server",

@@ -22,17 +22,20 @@ "main": "lib/tiny-coerce.js",

},
"engineStrict": true,
"engines": {
"node": ">=6.5.0"
},
"homepage": "http://avoidwork.github.io/tiny-coerce",
"devDependencies": {
"babel-eslint": "^4.1.4",
"babel-preset-es2015": "^6.1.2",
"grunt": "^0.4.5",
"grunt-babel": "^6.0.0",
"grunt-cli": "^0.1.13",
"grunt-contrib-concat": "^0.1.3",
"grunt-contrib-nodeunit": "^0.4.1",
"grunt-contrib-uglify": "^0.9.1",
"grunt-contrib-watch": "^0.2.0",
"grunt-eslint": "^17.3.1",
"grunt-sed": "^0.1.1"
"babel-core": "^6.26.3",
"babel-minify": "^0.4.3",
"grunt": "^1.0.3",
"grunt-cli": "^1.2.0",
"grunt-contrib-concat": "^1.0.1",
"grunt-contrib-nodeunit": "^2.0.0",
"grunt-contrib-uglify": "^3.3.0",
"grunt-contrib-watch": "^1.1.0",
"grunt-eslint": "^21.0.0",
"grunt-replace": "^1.0.1"
},
"dependencies": {}
}
# tiny-coerce
[![build status](https://secure.travis-ci.org/avoidwork/tiny-coerce.svg)](http://travis-ci.org/avoidwork/tiny-coerce)
Tiny library for String to primitive coercion for Client or Server. It's great for DOM data attributes, localStorage,
String to primitive coercion for Client or Server. It's great for DOM data attributes, localStorage,
and other cases where your need to store a String.
## API
##### coerce (arg[, deep = false])
Returns a coercion of `arg`. Deep coercion is optional with the second parameter.
First parameter is trimmed before coercion!
## Example
```javascript
const coerce = require("tiny-coerce");
console.log(coerce("true")); // true
console.log(coerce("null")); // null
console.log(coerce({a: {b: "50"}}, true).a.b) // 50
```
## License
Copyright (c) 2015 Jason Mulligan
Copyright (c) 2018 Jason Mulligan
Licensed under the BSD-3 license

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc