-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(flags): Handle space separated and double quoted multi-valued fla…
…gs (#236) * add tokenization of flags * fix lint and unit test * update dist folder * update dist folder * fix lint and unit test --------- Co-authored-by: Gulom Alimov <[email protected]>
- Loading branch information
Showing
5 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,22 @@ | ||
// tokenize functions splits a string of CLI flags by whitespace, additionally handling double-quoted | ||
// and space separated string values | ||
export function tokenize (input: string): string[] { | ||
// Regular expression to find quoted strings or sequences of non-whitespace characters | ||
const regex = /"([^"]*)"|\S+/g | ||
|
||
const tokens: string[] = [] | ||
let match: RegExpExecArray | null | ||
|
||
// Use the exec method to find all matches in the input string | ||
while ((match = regex.exec(input)) !== null) { | ||
// match[1] will hold the matched content inside quotes if it exists, | ||
// otherwise use match[0] which covers the non-quoted matches | ||
if (match[1] !== undefined) { | ||
tokens.push(match[1]) // Pushes the inside of the quotes to the array | ||
} else { | ||
tokens.push(match[0]) // Pushes the non-quoted match to the array | ||
} | ||
} | ||
|
||
return tokens | ||
} |
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,33 @@ | ||
import { tokenize } from '../src/utils' | ||
|
||
describe('utils.tokenize', () => { | ||
it('should handle normal unquoted words', () => { | ||
const result = tokenize('--flag1 flag1value --anotherFlag') | ||
expect(result).toEqual(['--flag1', 'flag1value', '--anotherFlag']) | ||
}) | ||
|
||
it('should handle single quoted group', () => { | ||
const result = tokenize('--flag1 "multi value"') | ||
expect(result).toEqual(['--flag1', 'multi value']) | ||
}) | ||
|
||
it('should handle multiple quoted groups', () => { | ||
const result = tokenize('--flag1 "multi value" --anotherFlag "another multi value" --flag333') | ||
expect(result).toEqual(['--flag1', 'multi value', '--anotherFlag', 'another multi value', '--flag333']) | ||
}) | ||
|
||
it('should handle empty quotes', () => { | ||
const result = tokenize('--flag1 "" --flag2') | ||
expect(result).toEqual(['--flag1', '', '--flag2']) | ||
}) | ||
|
||
it('should correctly tokenize empty input', () => { | ||
const result = tokenize('') | ||
expect(result).toEqual([]) | ||
}) | ||
|
||
it('should handle quotes without spaces in between', () => { | ||
const result = tokenize('"first""second""third"') | ||
expect(result).toEqual(['first', 'second', 'third']) | ||
}) | ||
}) |