Files
test/src/utils/strings.ts
T
Kyle Geib db14bf902f ISSUE-031 - Progress checkpoint.
A lot of work going on here.
* Moved WorkspaceControls component to a new sub directory
* Renamed overlay images
* Started a static ChyronDriverBasic component (using Hamilton as an example)
* Created a fancy DriverNumber rendering system
* Defined DriverNumber style for Mercedes and no one else yet
* A bunch of new states for the grid: cars, drivers, teams, etc
* Defined a bunch of data for the drivers and teams
* Created another string utility function to generate repeat strings; unit tests included
2022-01-21 14:14:45 -05:00

45 lines
1.4 KiB
TypeScript

/**
* Gives you the number of characters that prefix all the given strings.
* I use this to remove redundant repeated characters shared between file names.
* Basically, this is a method to do the " you find in a list.
* @param strings
* @returns number of characters that all strings share from the start
*/
export function findPrefixCount(strings: Array<string>, caseSensitive = true): number {
if (strings.length <= 1) {
return 0; // Cannot find anything. So, most neutral return is 0.
}
let maskLength = strings[0].length;
for (let x = 1; x < strings.length; ++x) {
for (let c = 0; c < maskLength; c++) {
const left = caseSensitive ? strings[0][c] : strings[0][c].toLowerCase();
const right = caseSensitive ? strings[x][c] : strings[x][c].toLowerCase();
if (left !== right) {
maskLength = c;
break;
}
}
}
return maskLength;
}
/**
* Basic function repeats the given string a set number of times and allows for
* a delimiter.
* @param value the string to repeat
* @param count the number of times to repeat
* @param delimiter the string to place between repeated strings
* @returns
*/
export function repeater(value: string, count: number, delimiter = ", "): string {
let output = "";
for (let x = 0; x < count; ++x) {
output += value;
if (delimiter.length !== 0 && x < count - 1) {
output += delimiter;
}
}
return output;
}