From 2c1b15d81159bd8d3bfaad0aa24cf2d4444919ce Mon Sep 17 00:00:00 2001 From: Jason Humphrey Date: Tue, 11 Apr 2017 23:30:24 -0500 Subject: [PATCH 1/6] Remove loads --- index.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 3500649..358b565 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,11 @@ module.exports = parse -var _ = require('lodash') var typpy = require('typpy') function stripTrimLower (value) { - return _.replace(_.trim(_.toLower(value)), /[""'']/ig, '') + return value.replace(/[""'']/ig, '').trim().toLowerCase() } -// function isBoolean (value) { -// return !(checkBoolean(value) === null) -// } - function toBoolean (value) { return checkBoolean(value) || false } @@ -32,13 +27,14 @@ function checkBoolean (value) { function parseObject (value) { if (typpy(value, Array)) { - return _.map(value, function (n, key) { + return value.map(function (n, key) { return parse(n) }) } else if (typpy(value, Object)) { - return _.forIn(value, function (n, key) { - value[key] = parse(n) - }) + for (var n in value) { + value[n] = parse(value[n]) + } + return value } return {} @@ -64,7 +60,7 @@ function parseType (value, type) { switch (typeName) { case 'string': if (typeof value === 'object') return JSON.stringify(value) - return _.toString(value) + return value.toString() case 'function': if (typpy(value, Function)) { @@ -99,7 +95,7 @@ function parseType (value, type) { return toBoolean(value) case 'number': - return _.toNumber(value) + return Number(value) case 'undefined': return undefined @@ -173,7 +169,7 @@ function parse (value, type) { } // Order Matter because if it is a one or zero boolean will come back with a awnser too. if you want it to be a boolean you must specify - var num = _.toNumber(value) + var num = Number(value) if (typpy(num, Number)) { return num } @@ -184,6 +180,6 @@ function parse (value, type) { } // DEFUALT SECTION - bascially if we catch nothing we assume that you just have a string - var string = _.toString(orignalValue) + var string = orignalValue.toString() return string } From 41c9d7c4c5bb7e9924e21cd6e969861251e47224 Mon Sep 17 00:00:00 2001 From: Jason Humphrey Date: Tue, 11 Apr 2017 23:59:46 -0500 Subject: [PATCH 2/6] #11 change toString to String --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 358b565..4ad42d9 100644 --- a/index.js +++ b/index.js @@ -60,7 +60,7 @@ function parseType (value, type) { switch (typeName) { case 'string': if (typeof value === 'object') return JSON.stringify(value) - return value.toString() + return String(value) case 'function': if (typpy(value, Function)) { @@ -180,6 +180,6 @@ function parse (value, type) { } // DEFUALT SECTION - bascially if we catch nothing we assume that you just have a string - var string = orignalValue.toString() + var string = String(orignalValue) return string } From 5609db89620c7764b89e21553dc614818ac695dc Mon Sep 17 00:00:00 2001 From: Jason Humphrey Date: Wed, 14 Mar 2018 00:42:48 -0500 Subject: [PATCH 3/6] #11 updated the comments --- index.js | 133 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 93 insertions(+), 40 deletions(-) diff --git a/index.js b/index.js index 4ad42d9..9389ecf 100644 --- a/index.js +++ b/index.js @@ -2,14 +2,36 @@ module.exports = parse var typpy = require('typpy') +/** + * + * @name stripTrimLower + * @function + * @param {Value} value strip trim & lower case the string + * @return {Value} parsed string + * + */ function stripTrimLower (value) { return value.replace(/[""'']/ig, '').trim().toLowerCase() } - +/** + * + * @name toBoolean + * @function + * @param {Value} value parse to boolean + * @return {Boolean} parsed boolean + * + */ function toBoolean (value) { return checkBoolean(value) || false } - +/** + * + * @name checkBoolean + * @function + * @param {Value} value is any value + * @return {Boolean} is a boolean value + * + */ function checkBoolean (value) { if (!value) { return false @@ -24,7 +46,14 @@ function checkBoolean (value) { return null } - +/** + * + * @name parseObject + * @function + * @param {Value} value parse object + * @return {Value} parsed object + * + */ function parseObject (value) { if (typpy(value, Array)) { return value.map(function (n, key) { @@ -36,22 +65,40 @@ function parseObject (value) { } return value } - return {} } - +/** + * + * @name parseFunction + * @function + * @param {Value} value function + * @return {Value} returned value from the called value function + * + */ function parseFunction (value) { return parse(value()) } - +/** + * + * @name parseType + * @function + * @param {Value} value inputed value + * @param {Type} type inputed type + * @return {Value} parsed type + * + */ function parseType (value, type) { - // Currently they send a string - handle String or Number or Boolean? + /** + * Currently they send a string - handle String or Number or Boolean? + */ if ((value && value.constructor === type) || typpy(value, type)) { return value } var typeName = type - // Convert the constructor into a string + /** + * Convert the constructor into a string + */ if (type && type.name) { typeName = type.name.toLowerCase() } @@ -66,46 +113,35 @@ function parseType (value, type) { if (typpy(value, Function)) { return value } - return function (cb) { if (typeof cb === 'function') { cb(value) } return value } - case 'date': return new Date(value) - case 'object': var jsonParsed try { jsonParsed = JSON.parse(value) } catch (e) {} - if (typpy(jsonParsed, Object) || typpy(jsonParsed, Array)) { return parse(jsonParsed) } else if (!typpy(jsonParsed, 'undefined')) { return {} } - return parseObject(value) - case 'boolean': return toBoolean(value) - case 'number': return Number(value) - case 'undefined': return undefined - case 'null': return null - case 'array': return [value] - default: if (typeof type === 'function') { return new type(value) // eslint-disable-line @@ -113,42 +149,61 @@ function parseType (value, type) { throw new Error('Unsupported type.') } } - +/** + * Parse + * auto-parse any value you happen to send in + * (String, Number, Boolean, Array, Object, Function, undefined and null). + * You send it we will try to find a way to parse it. + * We now support sending in a string of what type + * (e.g. "boolean") or constructor (e.g. Boolean) + * + * Usage: + * + * ```js + * Parse({}) // => "object" + * Parse('42'); // => 42 + * Parse.get('[]'); // => [] + * ``` + * + * @name Parse + * @function + * @param {Value} input The input value. + * @param {Constructor|String} target The target type. + * @return {String|Function|Date|Object|Boolean|Number|Undefined|Null|Array} + */ function parse (value, type) { if (type) { return parseType(value, type) } - var orignalValue = value - - // PRE RULE - check for null be cause null can be typeof object which can through off parsing + /** + * PRE RULE - check for null be cause null can be typeof object which can through off parsing + */ if (value === null) { return null } - - // TYPEOF SECTION - Use to check and do specific things based off of know the type - // Check against undefined + /** + * TYPEOF SECTION - Use to check and do specific things based off of know the type + * Check against undefined + */ if (value === void 0) { return undefined } - if (typeof value === 'number' || typeof value === 'boolean') { return value } - if (typeof value === 'function') { return parseFunction(value) } - if (typeof value === 'object') { return parseObject(value) } - - // STRING SECTION - If we made it this far that means it is a string that we must do something with to parse + /** + * STRING SECTION - If we made it this far that means it is a string that we must do something with to parse + */ if (value === 'NaN') { return NaN } - var jsonParsed = null try { jsonParsed = JSON.parse(value) @@ -157,29 +212,27 @@ function parse (value, type) { if (jsonParsed && typeof jsonParsed === 'object') { return parse(jsonParsed) } - value = stripTrimLower(value) - if (value === 'undefined' || value === '') { return undefined } - if (value === 'null') { return null } - - // Order Matter because if it is a one or zero boolean will come back with a awnser too. if you want it to be a boolean you must specify + /** + * Order Matter because if it is a one or zero boolean will come back with a awnser too. if you want it to be a boolean you must specify + */ var num = Number(value) if (typpy(num, Number)) { return num } - var boo = checkBoolean(value) if (typpy(boo, Boolean)) { return boo } - - // DEFUALT SECTION - bascially if we catch nothing we assume that you just have a string + /** + * DEFAULT SECTION - bascially if we catch nothing we assume that you just have a string + */ var string = String(orignalValue) return string } From 80d990a6b045daac9c5438591620f0dff47a6cd5 Mon Sep 17 00:00:00 2001 From: Jason Humphrey Date: Wed, 14 Mar 2018 00:43:07 -0500 Subject: [PATCH 4/6] #11 updated my package json --- package.json | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 2d170ad..ad37349 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,12 @@ "description": "auto-parse any value you happen to send in (String, Number, Boolean,Array, Object, Function, undefined and null). You send it we will try to find a way to parse it.support sending in a string of what type (e.g. boolean) or constructor (e.g. Boolean)", "main": "index.js", "dependencies": { - "lodash": "^4.16.3", - "typpy": "^2.3.6" + "typpy": "^2.3.10" }, "devDependencies": { - "chai": "^3.5.0", - "mocha": "^3.1.0", - "standard": "^8.3.0" + "chai": "^4.1.2", + "mocha": "^5.0.4", + "standard": "^11.0.0" }, "scripts": { "test": "npm run mocha && npm run standard", From f8b421d0b97e3fa2f4a2f8d513b47dfcb029c3b4 Mon Sep 17 00:00:00 2001 From: Jason Humphrey Date: Wed, 14 Mar 2018 00:47:54 -0500 Subject: [PATCH 5/6] #11 added a standard fix --- index.js | 22 +++++++++++----------- package.json | 3 ++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 9389ecf..defaa13 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ module.exports = parse var typpy = require('typpy') /** - * + * * @name stripTrimLower * @function * @param {Value} value strip trim & lower case the string @@ -14,7 +14,7 @@ function stripTrimLower (value) { return value.replace(/[""'']/ig, '').trim().toLowerCase() } /** - * + * * @name toBoolean * @function * @param {Value} value parse to boolean @@ -25,11 +25,11 @@ function toBoolean (value) { return checkBoolean(value) || false } /** - * + * * @name checkBoolean * @function * @param {Value} value is any value - * @return {Boolean} is a boolean value + * @return {Boolean} is a boolean value * */ function checkBoolean (value) { @@ -47,7 +47,7 @@ function checkBoolean (value) { return null } /** - * + * * @name parseObject * @function * @param {Value} value parse object @@ -68,7 +68,7 @@ function parseObject (value) { return {} } /** - * + * * @name parseFunction * @function * @param {Value} value function @@ -79,7 +79,7 @@ function parseFunction (value) { return parse(value()) } /** - * + * * @name parseType * @function * @param {Value} value inputed value @@ -151,9 +151,9 @@ function parseType (value, type) { } /** * Parse - * auto-parse any value you happen to send in - * (String, Number, Boolean, Array, Object, Function, undefined and null). - * You send it we will try to find a way to parse it. + * auto-parse any value you happen to send in + * (String, Number, Boolean, Array, Object, Function, undefined and null). + * You send it we will try to find a way to parse it. * We now support sending in a string of what type * (e.g. "boolean") or constructor (e.g. Boolean) * @@ -169,7 +169,7 @@ function parseType (value, type) { * @function * @param {Value} input The input value. * @param {Constructor|String} target The target type. - * @return {String|Function|Date|Object|Boolean|Number|Undefined|Null|Array} + * @return {String|Function|Date|Object|Boolean|Number|Undefined|Null|Array} */ function parse (value, type) { if (type) { diff --git a/package.json b/package.json index ad37349..edb21a1 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "scripts": { "test": "npm run mocha && npm run standard", "mocha": "node node_modules/mocha/bin/mocha test/mocha.test.js ", - "standard": "node node_modules/standard/bin/cmd.js || standard" + "standard": "node node_modules/standard/bin/cmd.js || standard", + "standard:fix": "node node_modules/standard/bin/cmd.js --fix || standard --fix" }, "repository": { "type": "git", From f67a5e5597ac1d4c887e911fde27bd9ebc2079df Mon Sep 17 00:00:00 2001 From: Jason Humphrey Date: Wed, 14 Mar 2018 00:53:41 -0500 Subject: [PATCH 6/6] release 1.4.0 --- LICENSE | 2 +- README.md | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/LICENSE b/LICENSE index c168542..23c5da9 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2014 - 2017 Green Pioneer Solutions +Copyright (c) 2014 - 2018 Green Pioneer Solutions Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index f52ca77..e69dfbc 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ var autoParse = require('auto-parse') The MIT License (MIT) -Copyright (c) 2014-2017 Green Pioneer +Copyright (c) 2014-2018 Green Pioneer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/package.json b/package.json index edb21a1..08ef1c9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "auto-parse", - "version": "1.3.0", + "version": "1.4.0", "description": "auto-parse any value you happen to send in (String, Number, Boolean,Array, Object, Function, undefined and null). You send it we will try to find a way to parse it.support sending in a string of what type (e.g. boolean) or constructor (e.g. Boolean)", "main": "index.js", "dependencies": {