rasper
Advanced tools
Comparing version 0.2.8 to 0.2.9
122
index.js
module.exports = (args = process.argv.slice(1)) => { | ||
if (!Array.isArray(args)) throw new Error('Rasper should receive an Array') | ||
if (!Array.isArray(args)) throw new Error('Rasper should receive an Array') | ||
const rasper = { _: [] } | ||
const rasper = { _: [] } | ||
args | ||
.map(getValue) | ||
.map(removeDashs) | ||
.filter(removeUndefined) | ||
.map(setValue) | ||
args | ||
.map(getValue) | ||
.map(removeDashs) | ||
.filter(removeUndefined) | ||
.map(setValue) | ||
return rasper | ||
return rasper | ||
function hasDash (value) { | ||
return String(value)[0] === '-' | ||
} | ||
function hasDash (value) { | ||
return String(value)[0] === '-' | ||
} | ||
function hasEqual (value) { | ||
return String(value).indexOf('=') !== -1 | ||
} | ||
function hasEqual (value) { | ||
return String(value).indexOf('=') !== -1 | ||
} | ||
function getValue (value, index, self) { | ||
const next = self[index + 1] | ||
const prev = self[index - 1] | ||
function getValue (value, index, self) { | ||
const next = self[index + 1] | ||
const prev = self[index - 1] | ||
if (!hasDash(value)) { | ||
if (!hasDash(prev) || hasDash(prev) && hasEqual(prev)) { | ||
rasper._.push(parser(value)) | ||
} | ||
if (hasDash(prev) && !hasEqual(prev)) { | ||
return prev + '=' + value | ||
} | ||
} | ||
if (hasDash(value) && !hasEqual(value)) { | ||
if (hasDash(next) || !next) { | ||
return value + '=' + 'true' | ||
} | ||
if (!hasDash(next) && hasEqual(next)) { | ||
return value + '=' + next | ||
} | ||
} | ||
if (!hasDash(value)) { | ||
if (!hasDash(prev) || (hasDash(prev) && hasEqual(prev))) { | ||
rasper._.push(parser(value)) | ||
} | ||
if (hasDash(prev) && !hasEqual(prev)) { | ||
return prev + '=' + value | ||
} | ||
} | ||
if (hasDash(value) && !hasEqual(value)) { | ||
if (hasDash(next) || !next) { | ||
return value + '=' + 'true' | ||
} | ||
if (!hasDash(next) && hasEqual(next)) { | ||
return value + '=' + next | ||
} | ||
} | ||
return value | ||
} | ||
return value | ||
} | ||
function removeDashs (value) { | ||
return value.match(/[^-].*/)[0] | ||
} | ||
function removeDashs (value) { | ||
return value.match(/[^-].*/)[0] | ||
} | ||
function removeUndefined (value) { | ||
return value.split('=')[1] !== undefined | ||
} | ||
function removeUndefined (value) { | ||
return value.split('=')[1] !== undefined | ||
} | ||
function kebabToCamel (value) { | ||
return value.replace(/-([a-z])/g, (_, k) => k.toUpperCase()) | ||
} | ||
function kebabToCamel (value) { | ||
return value.replace(/-([a-z])/g, (_, k) => k.toUpperCase()) | ||
} | ||
function setValue (value) { | ||
const key = parser(kebabToCamel(value.split('=')[0])) | ||
const val = parser(value.split('=')[1]) | ||
function setValue (value) { | ||
const key = parser(kebabToCamel(value.split('=')[0])) | ||
const val = parser(value.split('=')[1]) | ||
rasper[key] = val | ||
} | ||
rasper[key] = val | ||
} | ||
function parser (value) { | ||
if (value === 'true') { | ||
return true | ||
} | ||
if (value === 'false') { | ||
return false | ||
} | ||
if (!isNaN(value)) { | ||
return Number(value) | ||
} | ||
function parser (value) { | ||
if (value === 'true') { | ||
return true | ||
} | ||
if (value === 'false') { | ||
return false | ||
} | ||
if (!isNaN(value)) { | ||
return Number(value) | ||
} | ||
return value | ||
} | ||
return value | ||
} | ||
} |
140
package.json
{ | ||
"name": "rasper", | ||
"version": "0.2.8", | ||
"description": "Node.js parse argument options make ease.", | ||
"homepage": "https://cjpatoilo.com/rasper", | ||
"repository": "cjpatoilo/rasper", | ||
"license": "MIT", | ||
"author": "CJ Patoilo <cjpatoilo@gmail.com>", | ||
"private": false, | ||
"main": "index.js", | ||
"keywords": [ | ||
"🐥", | ||
"arg", | ||
"argument", | ||
"arguments", | ||
"argv", | ||
"bin", | ||
"cli", | ||
"cmd", | ||
"command", | ||
"console", | ||
"flags", | ||
"helper", | ||
"input", | ||
"line", | ||
"option", | ||
"options", | ||
"parser", | ||
"util", | ||
"utility" | ||
], | ||
"ignore": [ | ||
"artwork.psd", | ||
".appveyor.yml", | ||
".editorconfig", | ||
".github", | ||
".gitignore", | ||
".travis.yml" | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"ava": "^2.4.0", | ||
"boeing": "latest", | ||
"husky": "^3.0.9", | ||
"inject-ga": "^0.2.4", | ||
"lint-staged": "^9.4.2", | ||
"marshmallow": "latest", | ||
"nyc": "^14.1.1", | ||
"prettier-standard": "^15.0.1", | ||
"rimraf": "^3.0.0" | ||
}, | ||
"scripts": { | ||
"deploy": "marshmallow -o docs -i artwork.psd -f -m && inject-ga docs/index.html -o docs/index.html -c 'UA-24389952-13' && boeing docs && rimraf docs", | ||
"lint": "prettier-standard --check", | ||
"test": "nyc ava" | ||
}, | ||
"engines": { | ||
"node": "^12.13.0" | ||
}, | ||
"prettier": { | ||
"jsxSingleQuote": false, | ||
"trailingComma": "all" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged", | ||
"pre-push": "npm run deploy" | ||
} | ||
}, | ||
"lint-staged": { | ||
"src/**/*": [ | ||
"prettier-standard --format", | ||
"git add" | ||
] | ||
} | ||
"name": "rasper", | ||
"version": "0.2.9", | ||
"description": "Node.js parse argument options make ease.", | ||
"homepage": "https://cjpatoilo.com/rasper", | ||
"repository": "cjpatoilo/rasper", | ||
"license": "MIT", | ||
"author": "CJ Patoilo <cjpatoilo@gmail.com>", | ||
"private": false, | ||
"main": "index.js", | ||
"keywords": [ | ||
"🐥", | ||
"arg", | ||
"argument", | ||
"arguments", | ||
"argv", | ||
"bin", | ||
"cli", | ||
"cmd", | ||
"command", | ||
"console", | ||
"flags", | ||
"helper", | ||
"input", | ||
"line", | ||
"option", | ||
"options", | ||
"parser", | ||
"util", | ||
"utility" | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"ava": "^3.8.2", | ||
"boeing": "latest", | ||
"husky": "^4.2.5", | ||
"inject-ga": "^0.2.4", | ||
"lint-staged": "^10.2.9", | ||
"marshmallow": "latest", | ||
"nyc": "^15.1.0", | ||
"prettier-standard": "^16.3.0", | ||
"rimraf": "^3.0.2" | ||
}, | ||
"scripts": { | ||
"deploy": "marshmallow -o docs -i artwork.psd -f -m && inject-ga docs/index.html -o docs/index.html -c 'UA-24389952-13' && boeing docs && rimraf docs", | ||
"lint": "prettier-standard --check", | ||
"test": "nyc ava" | ||
}, | ||
"engines": { | ||
"node": "^12.18.0", | ||
"npm": "^6.14.5" | ||
}, | ||
"prettier": { | ||
"jsxSingleQuote": false, | ||
"trailingComma": "all" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"**/*": [ | ||
"prettier-standard --format", | ||
"git add" | ||
] | ||
} | ||
} |
@@ -13,3 +13,2 @@ <a href="https://github.com/cjpatoilo/rasper"><img width="100%" src="https://cjpatoilo.com/rasper/artwork.png" alt="Rasper - Node.js parse argument options make ease."></a> | ||
## Why it's awesome | ||
@@ -19,3 +18,2 @@ | ||
## Getting Started | ||
@@ -41,3 +39,2 @@ | ||
## Usage | ||
@@ -64,10 +61,8 @@ | ||
## Contributing | ||
Want to contribute? Follow these [recommendations](https://github.com/cjpatoilo/rasper#contributing). | ||
Want to contribute? Follow these [recommendations](https://github.com/cjpatoilo/rasper/contribute). | ||
## License | ||
Designed with ♥ by [CJ Patoilo](https://twitter.com/cjpatoilo). Licensed under the [MIT License](https://github.com/cjpatoilo/rasper#license). | ||
Designed with ♥ by [CJ Patoilo](https://twitter.com/cjpatoilo). Licensed under the [MIT License](https://cjpatoilo.com/license). |
6463
4
63
65