-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(matcher): allow ignoring {} by using $, ie ${a} should match with…
… {a} there is a refactor too, trying to separate braceExpand logic in a utility function fix #37
- Loading branch information
1 parent
faaa075
commit 470f07d
Showing
8 changed files
with
202 additions
and
91 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.
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
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 { braceExpand } from './braceExpand'; | ||
|
||
describe('braceExpand', () => { | ||
it('expands with same order', () => { | ||
expect(braceExpand('a{b,f,m}p')).toMatchObject(['abp', 'afp', 'amp']); | ||
}); | ||
|
||
describe('when ${ is present', () => { | ||
it('ignores {}', () => { | ||
expect(braceExpand('${13}')).toMatchObject(['{13}']); | ||
expect(braceExpand('${13}$${ab}')).toMatchObject(['{13}${ab}']); | ||
expect(braceExpand('${13}a$b${ab}')).toMatchObject(['{13}a$b{ab}']); | ||
expect(braceExpand('${a,b}${c,d}')).toMatchObject(['{a,b}{c,d}']); | ||
expect(braceExpand('x${a,b}x${c,d}x')).toMatchObject(['x{a,b}x{c,d}x']); | ||
expect(braceExpand('x${a,b}x{y,w}${c,d}x')).toMatchObject([ | ||
'x{a,b}xy{c,d}x', | ||
'x{a,b}xw{c,d}x' | ||
]); | ||
}); | ||
}); | ||
|
||
describe('when there is an empty option', () => { | ||
it('expands as many times as empty spaces', () => { | ||
expect(braceExpand('-v{,,,,}')).toMatchObject([ | ||
'-v', | ||
'-v', | ||
'-v', | ||
'-v', | ||
'-v' | ||
]); | ||
}); | ||
}); | ||
}); |
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,57 @@ | ||
import { decomposeString } from './decomposeString'; | ||
|
||
/** | ||
* Generate an array of string depending on {} blocks. | ||
* | ||
* @param {string} pattern Pattern string that can contains {} blocks. | ||
* @returns {string[]} Returns an array of string with all the combinations. | ||
* @example | ||
* ```javascript | ||
* braceExpand('${13}') | ||
* // => ['{13}'] | ||
* | ||
* braceExpand('a{b,f,m}p') | ||
* // => ['abp','afp','amp'] | ||
* | ||
* braceExpand('-v{,,}') | ||
* // => ['-v','-v','-v'] | ||
* ``` | ||
*/ | ||
const braceExpand = (pattern: string): string[] => { | ||
if (!pattern.match(/{.*}/)) { | ||
return [pattern]; | ||
} | ||
|
||
return expand(pattern, true); | ||
}; | ||
|
||
const expand = (str: string, isTop?: boolean): string[] => { | ||
const expansions = [] as string[]; | ||
const balance = decomposeString('{', '}', str); | ||
if (balance.start < 0) return [str]; | ||
|
||
const parts = balance.body.split(','); | ||
// no need to expand pre, since it is guaranteed to be free of brace-sets | ||
const pre = balance.pre; | ||
const postParts = balance.post.length ? expand(balance.post, false) : ['']; | ||
|
||
if (/\$$/.test(balance.pre)) { | ||
postParts.forEach((postPart) => { | ||
const expansion = `${balance.pre.slice(0, -1)}{${ | ||
balance.body | ||
}}${postPart}`; | ||
expansions.push(expansion); | ||
}); | ||
} else { | ||
parts.forEach((part: string) => { | ||
postParts.forEach((postPart) => { | ||
const expansion = pre + part + postPart; | ||
if (!isTop || expansion) expansions.push(expansion); | ||
}); | ||
}); | ||
} | ||
|
||
return expansions; | ||
}; | ||
|
||
export { braceExpand }; |