-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: escape arguments containing spaces (#982)
* fix: escape arguments containing spaces * fix: format args * fix: check if arg starts and ends with quotes * fix: restore functionality for inline * refactor: changes * refactor: changes * refactor: unit tests + inline args * refactor: rename * refactor: escape quotes * fix: add single quote and backtick * chore: import directly from utils * fix: unit tests * refactor: changes * feat: add unit test * fix: github warning
- Loading branch information
1 parent
eaddbf8
commit e202f8f
Showing
3 changed files
with
54 additions
and
12 deletions.
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,33 @@ | ||
import {escapeArguments} from './escapeArguments'; | ||
|
||
describe('utils', () => { | ||
describe('escapeArguments', () => { | ||
it('should handle empty array', () => { | ||
expect(escapeArguments([])).toEqual([]); | ||
}); | ||
|
||
it('should handle array with one argument', () => { | ||
expect(escapeArguments(['abc'])).toEqual(['abc']); | ||
}); | ||
|
||
it('should handle array with multiple arguments', () => { | ||
expect(escapeArguments(['abc', 'def'])).toEqual(['abc', 'def']); | ||
}); | ||
|
||
it('should handle array with multiple arguments with spaces', () => { | ||
expect(escapeArguments(['ab c', 'de f'])).toEqual(['"ab c"', '"de f"']); | ||
}); | ||
|
||
it('should handle array with multiple arguments with quotes', () => { | ||
expect(escapeArguments(['ab"c', 'de"f'])).toEqual(['ab\\"c', 'de\\"f']); | ||
}); | ||
|
||
it('should handle array with multiple arguments with spaces and quotes', () => { | ||
expect(escapeArguments(['ab"c', 'de f'])).toEqual(['ab\\"c', '"de f"']); | ||
}); | ||
|
||
it('should not escape single quotes', () => { | ||
expect(escapeArguments(["abc' def"])).toEqual([`"abc' def"`]); | ||
}); | ||
}); | ||
}); |
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,7 @@ | ||
export const escapeArguments = (args: string[]) => { | ||
return args.map(arg => { | ||
const escapedArg = arg.replace(/(["'`\\])/g, '\\$1'); | ||
|
||
return /\s/.test(escapedArg) ? `"${escapedArg.replace(/\\'/g, "'")}"` : escapedArg; | ||
}); | ||
}; |