Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

argv-split

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

argv-split - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

.babelrc

24

package.json
{
"name": "argv-split",
"version": "1.0.1",
"version": "2.0.0",
"description": "Split argv(argument vector) and handle special cases, such as quoted values.",
"main": "index.js",
"main": "split.js",
"scripts": {
"test": "./node_modules/.bin/mocha --reporter spec ./test/*.js"
"build": "babel index.js --out-file split.js",
"test": "npm run build && node --harmony ./node_modules/.bin/ava --verbose --timeout=10s"
},

@@ -18,3 +19,4 @@ "repository": {

"quote",
"quoted-value"
"quoted-value",
"balance"
],

@@ -29,6 +31,16 @@ "engines": {

},
"ava": {
"require": "babel-register",
"babel": {
"babelrc": true
},
"files": [
"test/*.js"
]
},
"devDependencies": {
"mocha": "*",
"chai": "*"
"ava": "^0.18.2",
"babel-cli": "^6.16.0",
"babel-preset-es2015": "^6.16.0"
}
}

@@ -6,3 +6,3 @@ [![Build Status](https://travis-ci.org/kaelzhang/node-argv-split.svg?branch=master)](https://travis-ci.org/kaelzhang/node-argv-split)

Split argv(argument vector) and handle special cases, such as quoted values.
Split argv(argument vector) and handle special cases, such as quoted or escaped values.

@@ -12,46 +12,91 @@ ## Why?

```js
'--abc "a b c"'.split(' ');
// ['--abc', '"a', 'b', 'c"'] -> Oooooooooops!
const split = require('split')
const mkdir = 'mkdir "foo bar"'
mkdir.split(' ') // ['mkdir', '"foo', 'bar"'] -> Oops!
split(mkdir) // ['mkdir', 'foo bar'] -> Oh yeah!
const mkdir2 = 'mkdir foo\\ bar'.split(' ')
mkdir2.split(' ') // ['mkdir', 'foo\\', 'bar'] -> Oops!
split(mkdir2) // ['mkdir', 'foo bar'] -> Oh yeah!
```
## Install
## `argv-split` handles all special cases with complete unit tests.
```sh
$ npm install argv-split --save
# shell command: javascript array:
foo a\ b # ['foo', 'a b']
foo \' # ['foo', '\\\'']
foo \" # ['foo', '\\"']
foo "a b" # ['foo', 'a b']
foo "a\ b" # ['foo', 'a\\ b']
foo '\' # ['foo', '\\']
foo --abc="a b" # ['foo', '--abc=a b']
foo --abc=a\ b # ['foo', '--abc=a b']
# argv-split also handles carriage returns
foo \
--abc=a\ b # ['foo', '--abc=a b']
# etc
```
## The usage is quite simple: `split(argv)`
```js
var split = require('argv-split');
split('foo \\\n --abc=a\\ b') // ['foo', '--abc=a b']
```
split('--abc "a b c"');
// ['--abc', 'a b c'], Oh yeah !!!!
## Error Codes
### `UNMATCHED_SINGLE`
If a command missed the closing single quote, the error will throw:
```sh
foo --abc 'abc
```
### split(string)
### `UNMATCHED_DOUBLE`
Splits a string, and balance quoted parts.
If a command missed the closing double quote, the error will throw:
```js
split('--abc "a \'b\' c"'); // ['--abc', "a 'b' c"]
split('--abc "a b c'); // ['--abc', '"a', 'b', 'c']
```sh
foo --abc "abc
```
### split.balance(array)
### `ESCAPED_EOF`
Balances an array and join incorrect splited parts.
If a command unexpectedly ends with a `\`, the error will throw:
```js
split.balance(['--abc', '"a', 'b"']); // ['--abc', 'a b']
```sh
foo --abc a\# if there is nothing after \, the error will throw
foo --abc a\ # if there is a whitespace after, then -> ['foo', '--abc', 'a ']
```
### split.join(array, [quote='])
### `NON_STRING`
If the argument passed to `split` is not a string, the error will throw
```js
split.join(['--abc', 'a b']); // "--abc 'a b'"
split(undefined)
```
## Install
```sh
$ npm install argv-split --save
```
### split(string)
Splits a string, and balance quoted parts. The usage is quite simple, see examples above.
Returns `Array`
### ~~split.join()~~
Temporarily removed in `2.0.0`, and will be added in `2.1.0`
## License
MIT

@@ -1,63 +0,108 @@

'use strict';
'use strict'
var expect = require('chai').expect;
var split = require('../');
const test = require('ava')
const split = require('..')
// #4
var command = 'command -a "a \'b\' c" -b \'a "b" c\' -c'
+ ' "a b" -d \'a b\' -e "a" -f \'a\' -g "a b c';
;[
{
d: 'normal',
a: 'a b c',
e: ['a', 'b', 'c']
},
{
d: 'double-quoted',
a: '"a b c"',
e: ['a b c']
},
{
d: 'double-quoted, and trailing normal',
a: '"a b" c',
e: ['a b', 'c']
},
{
d: 'double-quoted, and heading normal',
a: 'c "a b"',
e: ['c', 'a b']
},
{
d: 'single-quoted',
a: "'a b c'",
e: ['a b c']
},
{
d: 'single-quoted, and trailing normal',
a: "'a b' c",
e: ['a b', 'c']
},
{
d: 'single-quoted, and heading normal',
a: "c 'a b'",
e: ['c', 'a b']
},
{
d: 'escaped whitespaces',
a: 'a\\ b',
e: ['a b']
},
{
d: 'escaped whitespaces, and trailing normal',
a: 'a\\ b c',
e: ['a b', 'c']
},
{
d: 'escaped whitespaces, and heading normal',
a: 'c a\\ b',
e: ['c', 'a b']
},
{
d: 'non-starting single quote',
a: "a' b'",
e: ['a b']
},
{
d: 'non-staring single quote with =',
a: "--foo='bar baz'",
e: ['--foo=bar baz']
},
{
d: 'non-starting double quote',
a: 'a" b"',
e: ['a b']
},
{
d: 'non-starting double quote with =',
a: '--foo="bar baz"',
e: ['--foo=bar baz']
},
{
d: 'double-quoted escaped double quote',
a: '"bar\\" baz"',
e: ['bar" baz']
},
{
d: 'single-quoted escaped double quote, should not over unescaped',
a: '\'bar\\" baz\'',
e: ['bar\\" baz']
},
{
d: 'signle-quoted escaped single quote, should throw',
a: "'bar\' baz'",
throws: true
}
var args = [
'command',
'-a', "a 'b' c",
'-b', 'a "b" c',
'-c', 'a b',
'-d', "a b",
'-e', 'a',
'-f', "a",
'-g', '"a', 'b', 'c'
];
].forEach(({d, a, e, throws, only}) => {
const t = only
? test.only
: test
describe("split(string)", function(){
it("quoted", function(){
expect(split(command)).to.deep.equal(args);
});
it("normal", function(){
var command = 'command -a b --abc false';
expect(split(command)).to.deep.equal([
'command',
'-a',
'b',
'--abc',
'false'
]);
});
});
describe("split.balance()", function(){
it("balance arguments", function(){
var splited = command.split(' ');
expect(split.balance(splited)).to.deep.equal(args);
});
});
describe("split.join()", function(){
it("join arguments", function(){
var args = [
'-a',
'a b c'
];
expect(split.join(args)).to.equal('-a \'a b c\'');
});
it("specified quote", function(){
var args = [
'-a',
'a b c'
];
expect(split.join(args, '"')).to.equal('-a "a b c"');
});
});
t(d, t => {
if (throws) {
t.throws(() => {
split(a)
})
return
}
// console.log(split(a), e)
t.deepEqual(split(a), e)
})
})

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc