-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict' | ||
|
||
const tokenize = require('../tokenize') | ||
|
||
test.each([ | ||
['eslint --fix .', ['eslint', '--fix', '.']], | ||
['eslint --fix . ', ['eslint', '--fix', '.']], | ||
[' nodemon --help', ['nodemon', '--help']], | ||
[ | ||
'eslint --ext .jsx --ext .js lib/', | ||
['eslint', '--ext', '.jsx', '--ext', '.js', 'lib/'], | ||
], | ||
[ | ||
'git commit -m "remove unused dependencies"', | ||
['git', 'commit', '-m', 'remove unused dependencies'], | ||
], | ||
[ | ||
"git commit -m 'remove unused dependencies'", | ||
['git', 'commit', '-m', 'remove unused dependencies'], | ||
], | ||
['echo "1 2 3" "4 5 6" 7 8 9', ['echo', '1 2 3', '4 5 6', '7', '8', '9']], | ||
[ | ||
'lerna version -m "chore(release): publish" --force-publish', | ||
['lerna', 'version', '-m', 'chore(release): publish', '--force-publish'], | ||
], | ||
[ | ||
"lerna version -m 'chore(release): publish' --force-publish", | ||
['lerna', 'version', '-m', 'chore(release): publish', '--force-publish'], | ||
], | ||
['', []], | ||
])('parses %p', (input, expected) => { | ||
const result = tokenize(input) | ||
expect(result).toEqual(expected) | ||
}) | ||
|
||
test.each([ | ||
[null], | ||
[undefined], | ||
[3], | ||
[[1, 2, 3]], | ||
[{ foo: 'bar' }], | ||
[['array', 'of', 'strings']], | ||
])('throws on %p', (input) => { | ||
expect(() => { | ||
tokenize(input) | ||
}).toThrow() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict' | ||
|
||
/** | ||
* Splits a string into an array based on spaces and quotations. | ||
* | ||
* @param {string} string String to tokenize | ||
* @returns {string[]} array of strings | ||
* | ||
* @example | ||
* tokenize('eslint --ext .jsx --ext .js lib/') | ||
* // Returns ['eslint', '--ext', '.jsx', '--ext', '.js', 'lib/'] | ||
* | ||
* @example | ||
* tokenize('git commit -m "remove unused dependencies"') | ||
* // Returns ['git', 'commit', '-m', 'remove unused dependencies'] | ||
*/ | ||
function tokenize(string) { | ||
if (typeof string !== 'string') { | ||
throw new TypeError('string should be a string') | ||
} | ||
|
||
const tokens = [] | ||
let index = 0 | ||
let quote = '' | ||
|
||
for (const character of string) { | ||
// Add character to token | ||
// if inside quotes and character's not a close quote OR | ||
// if outside quotes and character's not a space or open quote | ||
if ( | ||
(quote && character !== quote) || | ||
(!quote && character !== ' ' && character !== "'" && character !== '"') | ||
) { | ||
// Initialize token if empty | ||
if (!tokens[index]) tokens[index] = '' | ||
tokens[index] += character | ||
} | ||
|
||
// Move to next token if token's not empty AND | ||
// closing quotes OR outside quotes and character's a space | ||
if ( | ||
tokens[index] && | ||
((quote && character === quote) || (!quote && character === ' ')) | ||
) { | ||
index += 1 | ||
} | ||
|
||
// Close quote if character equals quote | ||
// Open quote if character is a quote | ||
if (quote && character === quote) { | ||
quote = '' | ||
} else if (!quote && (character === '"' || character === "'")) | ||
quote = character | ||
} | ||
|
||
return tokens | ||
} | ||
|
||
module.exports = tokenize |