-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
54 lines (50 loc) · 1.46 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Split a command into an array.
*
* @example
* ```js
* const arr = split( 'git commit -m "some message with spaces"' );
* console.log( arr ); // [ "git", "commit", "-m", "some message with spaces" ]
* ```
*
* @param {string} command Command to split.
* @returns {Array}
*/
export function split( command: string ): string[] {
if ( typeof command !== 'string' ) {
throw new Error( 'Command must be a string' );
}
const r = command.match( /[^"\s]+|"(?:\\"|[^"])*"/g );
if ( ! r ) {
return [];
}
return r.map( function ( expr ) {
const isQuoted = expr.charAt( 0 ) === '"' && expr.charAt( expr.length - 1 ) === '"';
return isQuoted ? expr.slice( 1, -1 ) : expr;
} );
}
/**
* Split a command into an object with the attributes `command` and `args`.
*
* @example
* ```js
* const obj = splitToObject( 'git commit -m "some message with spaces"' );
* console.log( obj.command ); // git
* console.log( obj.args ); // [ "commit", "-m", "some message with spaces" ]
* ```
*
* @param {string} command Command to split.
* @returns {object}
*/
export function splitToObject( command: string ): { command?: string, args?: string[] } {
const cmds = split( command );
switch( cmds.length ) {
case 0: return {};
case 1: return { command: cmds[ 0 ] };
default: {
const first = cmds[ 0 ];
cmds.shift();
return { command: first, args: cmds };
}
}
}