string-argv
Advanced tools
Comparing version 0.2.1 to 0.3.0
# Changelog | ||
## v0.3.0 (2019-04-16) | ||
**Dev Experience Changes** | ||
- Project now compiled with TypeScript and provides typings [7415c2](https://github.com/mccormicka/string-argv/commit/7415c20121bfa02887162b8326042563b903c332) | ||
## v0.2.0 (2019-04-14) | ||
**Parsing Behavior Changes** | ||
- Now parses multiple nested quotes and content when there are no spaces [7d9b897](https://github.com/mccormicka/string-argv/commit/7d9b89730ea112b829f2591e3e9cae4c0d0cc285) |
72
index.js
"use strict"; | ||
module.exports = parseArgsStringToArgv; | ||
module.exports.parseArgsStringToArgv = parseArgsStringToArgv; | ||
exports.__esModule = true; | ||
function parseArgsStringToArgv(value, env, file) { | ||
// ([^\s'"]([^\s'"]*(['"])([^\3]*?)\3)+[^\s'"]*) Matches nested quotes until the first space outside of quotes | ||
// [^\s'"]+ or Match if not a space ' or " | ||
// (['"])([^\5]*?)\5 or Match "quoted text" without quotes | ||
// `\3` and `\5` are a backreference to the quote style (' or ") captured | ||
var myRegexp = /([^\s'"]([^\s'"]*(['"])([^\3]*?)\3)+[^\s'"]*)|[^\s'"]+|(['"])([^\5]*?)\5/gi; | ||
var myString = value; | ||
var myArray = [ | ||
]; | ||
if (env) { | ||
myArray.push(env); | ||
} | ||
if (file) { | ||
myArray.push(file); | ||
} | ||
var match; | ||
do { | ||
// ([^\s'"]([^\s'"]*(['"])([^\3]*?)\3)+[^\s'"]*) Matches nested quotes until the first space outside of quotes | ||
// [^\s'"]+ or Match if not a space ' or " | ||
// (['"])([^\5]*?)\5 or Match "quoted text" without quotes | ||
// `\3` and `\5` are a backreference to the quote style (' or ") captured | ||
var myRegexp = /([^\s'"]([^\s'"]*(['"])([^\3]*?)\3)+[^\s'"]*)|[^\s'"]+|(['"])([^\5]*?)\5/gi; | ||
var myString = value; | ||
var myArray = []; | ||
if (env) { | ||
myArray.push(env); | ||
} | ||
if (file) { | ||
myArray.push(file); | ||
} | ||
var match; | ||
do { | ||
// Each call to exec returns the next regex match as an array | ||
match = myRegexp.exec(myString); | ||
if (match !== null) { | ||
// Index 1 in the array is the captured group if it exists | ||
// Index 0 is the matched text, which we use if no captured group exists | ||
myArray.push(firstString(match[1], match[6], match[0])); | ||
} | ||
} while (match !== null); | ||
return myArray; | ||
match = myRegexp.exec(myString); | ||
if (match !== null) { | ||
// Index 1 in the array is the captured group if it exists | ||
// Index 0 is the matched text, which we use if no captured group exists | ||
myArray.push(firstString(match[1], match[6], match[0])); | ||
} | ||
} while (match !== null); | ||
return myArray; | ||
} | ||
exports["default"] = parseArgsStringToArgv; | ||
exports.parseArgsStringToArgv = parseArgsStringToArgv; | ||
// Accepts any number of arguments, and returns the first one that is a string | ||
// (even an empty string) | ||
function firstString() { | ||
for (var i = 0; i < arguments.length; i++) { | ||
var arg = arguments[i]; | ||
if (typeof arg === "string") { | ||
return arg; | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
} | ||
for (var i = 0; i < args.length; i++) { | ||
var arg = args[i]; | ||
if (typeof arg === "string") { | ||
return arg; | ||
} | ||
} | ||
} |
{ | ||
"name": "string-argv", | ||
"description": "string-argv parses a string into an argument array to mimic process.argv. This is useful when testing Command Line Utilities that you want to pass arguments to.", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"contributors": [ | ||
@@ -20,6 +20,8 @@ { | ||
"scripts": { | ||
"lint": "eslint .", | ||
"test": "jasmine --config=test/config.json && npm run lint" | ||
"build": "tsc -p .", | ||
"prepublishOnly": "npm test", | ||
"test": "npm run build & jasmine --config=test/config.json" | ||
}, | ||
"main": "index", | ||
"types": "index.d.ts", | ||
"engines": { | ||
@@ -39,6 +41,5 @@ "node": ">=0.6.19" | ||
"devDependencies": { | ||
"eslint": "^2.0.0", | ||
"eslint-config-cellule": "^3.0.0", | ||
"jasmine": "^2.4.1" | ||
"jasmine": "^2.4.1", | ||
"typescript": "^3.4.3" | ||
} | ||
} |
6034
2
6
46