2024-07-01 16:10:53 +00:00
|
|
|
export type SplitOperatorType = 'symbol' | 'regex';
|
|
|
|
|
|
|
|
|
|
export function truncateList(
|
2024-07-09 23:38:29 +01:00
|
|
|
splitOperatorType: SplitOperatorType,
|
|
|
|
|
input: string,
|
|
|
|
|
splitSeparator: string,
|
|
|
|
|
joinSeparator: string,
|
|
|
|
|
end: boolean,
|
|
|
|
|
length?: number
|
2024-07-01 16:10:53 +00:00
|
|
|
): string {
|
2024-07-09 23:38:29 +01:00
|
|
|
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.');
|
2024-07-01 16:10:53 +00:00
|
|
|
}
|
2024-07-09 23:38:29 +01:00
|
|
|
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.");
|
|
|
|
|
}
|