truncate tool and testCases; then autoupdated index file

This commit is contained in:
Chesterkxng
2024-07-01 16:10:53 +00:00
parent 9db2d108b6
commit 2a5240724d
5 changed files with 240 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
export type SplitOperatorType = 'symbol' | 'regex';
export function truncateList(
splitOperatorType: SplitOperatorType,
input: string,
splitSeparator: string,
joinSeparator: string,
end: boolean,
length?: number,
): string {
let array: string[];
let truncatedArray: string[];
switch (splitOperatorType) {
case 'symbol':
array = input.split(splitSeparator);
break;
case 'regex':
array = input.split(new RegExp(splitSeparator));
break;
}
if (length !== undefined) {
if (length < 0) {
throw new Error("Length value must be a positive number.")
}
truncatedArray = end ? array.slice(0, length) : array.slice(array.length - length, array.length);
return truncatedArray.join(joinSeparator);
}
throw new Error("Length value isn't a value number.");
}