-
-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new --matrix option to multiply commands
- Loading branch information
Showing
5 changed files
with
202 additions
and
0 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,116 @@ | ||
import { CommandInfo } from '../command'; | ||
import { combinations, ExpandMatrices } from './expand-matrices'; | ||
|
||
const createCommandInfo = (command: string): CommandInfo => ({ | ||
command, | ||
name: '', | ||
}); | ||
|
||
describe('ExpandMatrices', () => { | ||
it('should replace placeholders with matrix values', () => { | ||
const matrices = [ | ||
['a', 'b'], | ||
['1', '2'], | ||
]; | ||
const expandMatrices = new ExpandMatrices(matrices); | ||
const commandInfo = createCommandInfo('echo {1} and {2}'); | ||
|
||
const result = expandMatrices.parse(commandInfo); | ||
|
||
expect(result).toEqual([ | ||
{ command: 'echo a and 1', name: '' }, | ||
{ command: 'echo a and 2', name: '' }, | ||
{ command: 'echo b and 1', name: '' }, | ||
{ command: 'echo b and 2', name: '' }, | ||
]); | ||
}); | ||
|
||
it('should handle escaped placeholders', () => { | ||
const matrices = [['a', 'b']]; | ||
const expandMatrices = new ExpandMatrices(matrices); | ||
const commandInfo = createCommandInfo('echo \\{1} and {1}'); | ||
|
||
const result = expandMatrices.parse(commandInfo); | ||
|
||
expect(result).toEqual([ | ||
{ command: 'echo {1} and a', name: '' }, | ||
{ command: 'echo {1} and b', name: '' }, | ||
]); | ||
}); | ||
|
||
it('should replace placeholders with empty string if index is out of bounds', () => { | ||
const matrices = [['a']]; | ||
const expandMatrices = new ExpandMatrices(matrices); | ||
const commandInfo = createCommandInfo('echo {2}'); | ||
|
||
const result = expandMatrices.parse(commandInfo); | ||
|
||
expect(result).toEqual([{ command: 'echo ', name: '' }]); | ||
}); | ||
}); | ||
|
||
describe('combinations', () => { | ||
it('should return all possible combinations of the given dimensions', () => { | ||
const dimensions = [ | ||
['a', 'b'], | ||
['1', '2'], | ||
]; | ||
|
||
const result = combinations(dimensions); | ||
|
||
expect(result).toEqual([ | ||
['a', '1'], | ||
['a', '2'], | ||
['b', '1'], | ||
['b', '2'], | ||
]); | ||
}); | ||
|
||
it('should handle single dimension', () => { | ||
const dimensions = [['a', 'b']]; | ||
|
||
const result = combinations(dimensions); | ||
|
||
expect(result).toEqual([['a'], ['b']]); | ||
}); | ||
|
||
it('should handle empty dimensions', () => { | ||
const dimensions: string[][] = []; | ||
|
||
const result = combinations(dimensions); | ||
|
||
expect(result).toEqual([[]]); | ||
}); | ||
|
||
it('should handle dimensions with empty arrays', () => { | ||
const dimensions = [['a', 'b'], []]; | ||
|
||
const result = combinations(dimensions); | ||
|
||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should handle dimensions with multiple empty arrays', () => { | ||
const dimensions = [[], []]; | ||
|
||
const result = combinations(dimensions); | ||
|
||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should handle dimensions with some empty arrays', () => { | ||
const dimensions = [['a', 'b'], [], ['x', 'y']]; | ||
|
||
const result = combinations(dimensions); | ||
|
||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should handle dimensions with all empty arrays', () => { | ||
const dimensions = [[], [], []]; | ||
|
||
const result = combinations(dimensions); | ||
|
||
expect(result).toEqual([]); | ||
}); | ||
}); |
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,62 @@ | ||
import { quote } from 'shell-quote'; | ||
|
||
import { CommandInfo } from '../command'; | ||
import { CommandParser } from './command-parser'; | ||
|
||
/** | ||
* Replace placeholders with new commands for each combination of matrices. | ||
*/ | ||
export class ExpandMatrices implements CommandParser { | ||
private _bindings: string[][]; | ||
|
||
constructor(private readonly matrices: readonly string[][]) { | ||
this.matrices = matrices; | ||
this._bindings = combinations(matrices); | ||
} | ||
|
||
parse(commandInfo: CommandInfo) { | ||
return this._bindings.map((binding) => this.replacePlaceholders(commandInfo, binding)); | ||
} | ||
|
||
private replacePlaceholders(commandInfo: CommandInfo, binding: string[]): CommandInfo { | ||
const command = commandInfo.command.replace( | ||
/\\?\{([0-9]*)?\}/g, | ||
(match, placeholderTarget) => { | ||
// Don't replace the placeholder if it is escaped by a backslash. | ||
if (match.startsWith('\\')) { | ||
return match.slice(1); | ||
} | ||
|
||
let index = 0; | ||
if (placeholderTarget && !isNaN(placeholderTarget)) { | ||
index = parseInt(placeholderTarget, 10) - 1; | ||
} | ||
|
||
// Replace numeric placeholder if value exists in additional arguments. | ||
if (index < binding.length) { | ||
return quote([binding[index]]); | ||
} | ||
|
||
// Replace placeholder with empty string | ||
// if value doesn't exist in additional arguments. | ||
return ''; | ||
}, | ||
); | ||
|
||
return { ...commandInfo, command }; | ||
} | ||
} | ||
|
||
/** | ||
* Returns all possible combinations of the given dimensions. | ||
*/ | ||
export function combinations(dimensions: readonly string[][]): string[][] { | ||
return dimensions.reduce( | ||
(acc, dimension) => { | ||
return acc.flatMap((accItem) => | ||
dimension.map((dimensionItem) => accItem.concat(dimensionItem)), | ||
); | ||
}, | ||
[[]] as string[][], | ||
); | ||
} |
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