Skip to content

Commit

Permalink
Merge branch 'release/1.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenPioneer committed Mar 14, 2018
2 parents 70ce07f + f67a5e5 commit 6e21b49
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 63 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
157 changes: 103 additions & 54 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
module.exports = parse

var _ = require('lodash')
var typpy = require('typpy')

/**
*
* @name stripTrimLower
* @function
* @param {Value} value strip trim & lower case the string
* @return {Value} parsed string
*
*/
function stripTrimLower (value) {
return _.replace(_.trim(_.toLower(value)), /[""'']/ig, '')
return value.replace(/[""'']/ig, '').trim().toLowerCase()
}

// function isBoolean (value) {
// return !(checkBoolean(value) === null)
// }

/**
*
* @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
Expand All @@ -29,33 +46,59 @@ 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 _.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 {}
}

/**
*
* @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()
}
Expand All @@ -64,95 +107,103 @@ function parseType (value, type) {
switch (typeName) {
case 'string':
if (typeof value === 'object') return JSON.stringify(value)
return _.toString(value)
return String(value)

case 'function':
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 _.toNumber(value)

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
}
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)
Expand All @@ -161,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
var num = _.toNumber(value)
/**
* 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
var string = _.toString(orignalValue)
/**
* DEFAULT SECTION - bascially if we catch nothing we assume that you just have a string
*/
var string = String(orignalValue)
return string
}
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{
"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": {
"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",
"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",
Expand Down

0 comments on commit 6e21b49

Please sign in to comment.