argv-split
Advanced tools
Comparing version 3.0.0 to 3.1.0
41
index.js
'use strict' | ||
module.exports = split | ||
// split.join = join | ||
split.join = join | ||
@@ -158,3 +158,3 @@ | ||
if (typeof str !== 'string') { | ||
type_error(`Str must be a string. Received ${str}`, 'NON_STRING') | ||
type_error(`\`str\` must be a string. Received ${str}`, 'NON_STRING') | ||
} | ||
@@ -208,17 +208,28 @@ | ||
// function join (args, options = {}) { | ||
// const quote = options.quote || "'" | ||
const REGEX_NEED_QUOTE = /\s|"|'/ | ||
// return args.map(function (arg) { | ||
// if (!arg) { | ||
// return | ||
// } | ||
function join(args, { | ||
quote = DOUBLE_QUOTE | ||
} = {}) { | ||
if (![SINGLE_QUOTE, DOUBLE_QUOTE].includes(quote)) { | ||
type_error( | ||
`\`options.quote\` must be either ${SINGLE_QUOTE} or ${DOUBLE_QUOTE}. Received ${quote}` , 'INVALID_QUOTE' | ||
) | ||
} | ||
// return /\c+/.test(arg) | ||
// // a b c -> 'a b c' | ||
// // a 'b' -> 'a \'b\'' | ||
// ? quote + arg.replace("'", "\\'") + quote | ||
// : arg | ||
const process_arg = arg => { | ||
if (!REGEX_NEED_QUOTE.test(arg)) { | ||
return arg | ||
} | ||
// }).join(WHITE_SPACE) | ||
// } | ||
if (!arg.includes(quote)) { | ||
return quote + arg + quote | ||
} | ||
// escape quote | ||
const escaped = arg.replace(new RegExp(quote, 'g'), `\\${quote}`) | ||
return quote + escaped + quote | ||
} | ||
return args.map(process_arg).join(WHITE_SPACE) | ||
} |
{ | ||
"name": "argv-split", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Split argv(argument vector) and handle special cases, such as quoted values.", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -93,18 +93,30 @@ [![Build Status](https://github.com/kaelzhang/node-argv-split/actions/workflows/nodejs.yml/badge.svg)](https://github.com/kaelzhang/node-argv-split/actions/workflows/nodejs.yml) | ||
```sh | ||
$ npm install argv-split --save | ||
$ npm i argv-split | ||
``` | ||
### split(string) | ||
### split(string) -> Array<string> | ||
Splits a string, and balance quoted parts. The usage is quite simple, see examples above. | ||
Returns `Array` | ||
Returns `Array<string>` | ||
### ~~split.join()~~ | ||
### split.join(args, options?) -> string | ||
Temporarily removed in `2.0.0`, and will be added in `2.1.0` | ||
Join the given array of argument vectors into a valid argument string | ||
New in `3.1.0` | ||
- **args** `Array<string>` arguments to be joined | ||
- **options?** `Object=` | ||
- **quote** `string="` should we use single quote or double quote when a certain argument needs to be quoted. Defaults to `"` | ||
```js | ||
'command ' + join(['foo "bar', "'baz"]) | ||
// command "foo \"bar" "'baz" | ||
``` | ||
## License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9101
183
122
0