TravBot-v3/src/core/wrappers.ts

74 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-10-15 09:23:24 +00:00
export class GenericWrapper<T> {
2020-12-15 01:44:28 +00:00
protected readonly value: T;
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
public constructor(value: T) {
this.value = value;
}
}
2020-10-15 09:23:24 +00:00
export class NumberWrapper extends GenericWrapper<number> {
2020-12-15 01:44:28 +00:00
/**
* Pluralises a word and chooses a suffix attached to the root provided.
* - pluralise("credit", "s") = credit/credits
* - pluralise("part", "ies", "y") = party/parties
* - pluralise("sheep") = sheep
*/
2020-12-15 07:56:09 +00:00
public pluralise(word: string, plural = "", singular = "", excludeNumber = false): string {
2020-12-15 01:44:28 +00:00
let result = excludeNumber ? "" : `${this.value} `;
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
if (this.value === 1) result += word + singular;
else result += word + plural;
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
return result;
}
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
/**
* Pluralises a word for changes.
* - (-1).pluraliseSigned() = '-1 credits'
* - (0).pluraliseSigned() = '+0 credits'
* - (1).pluraliseSigned() = '+1 credit'
*/
2020-12-15 07:56:09 +00:00
public pluraliseSigned(word: string, plural = "", singular = "", excludeNumber = false): string {
2020-12-15 01:44:28 +00:00
const sign = this.value >= 0 ? "+" : "";
2020-12-15 07:56:09 +00:00
return `${sign}${this.pluralise(word, plural, singular, excludeNumber)}`;
2020-12-15 01:44:28 +00:00
}
}
2020-10-15 09:23:24 +00:00
export class StringWrapper extends GenericWrapper<string> {
2020-12-15 01:44:28 +00:00
public replaceAll(before: string, after: string): string {
let result = this.value;
2020-10-15 09:23:24 +00:00
2020-12-15 07:56:09 +00:00
while (result.indexOf(before) !== -1) result = result.replace(before, after);
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
return result;
}
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
public toTitleCase(): string {
return this.value.replace(
/([^\W_]+[^\s-]*) */g,
(txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
);
}
2020-08-14 16:35:53 +00:00
}
2020-10-15 09:23:24 +00:00
export class ArrayWrapper<T> extends GenericWrapper<T[]> {
2020-12-15 01:44:28 +00:00
/** Returns a random element from this array. */
public random(): T {
return this.value[Math.floor(Math.random() * this.value.length)];
}
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
/**
* Splits up this array into a specified length.
* `$([1,2,3,4,5,6,7,8,9,10]).split(3)` = `[[1,2,3],[4,5,6],[7,8,9],[10]]`
*/
public split(lengthOfEachSection: number): T[][] {
2020-12-15 07:56:09 +00:00
const amountOfSections = Math.ceil(this.value.length / lengthOfEachSection);
2020-12-15 01:44:28 +00:00
const sections: T[][] = new Array(amountOfSections);
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
for (let index = 0; index < amountOfSections; index++)
2020-12-15 07:56:09 +00:00
sections[index] = this.value.slice(index * lengthOfEachSection, (index + 1) * lengthOfEachSection);
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
return sections;
}
2020-10-15 09:23:24 +00:00
}