using my boilerplate yuh
This commit is contained in:
25
src/shared/util/random.ts
Normal file
25
src/shared/util/random.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Generate a random ID
|
||||
*
|
||||
* @returns string of size 10 made up of random numbers and letters
|
||||
* @param length the length of the ID to generate
|
||||
* @example "cdtl9l88pj"
|
||||
*/
|
||||
export function generateRandomId(length: number = 10): string {
|
||||
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
let result = '';
|
||||
for (let i = 0; i < length; i += 1) {
|
||||
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random number between min and max
|
||||
* @param min the minimum number
|
||||
* @param max the maximum number
|
||||
* @returns a random number between min and max
|
||||
*/
|
||||
export function rangeRandom(min: number, max: number): number {
|
||||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||
}
|
||||
34
src/shared/util/string.ts
Normal file
34
src/shared/util/string.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Given a string, returns a string with the first letter capitalized.
|
||||
* @input The string to capitalize.
|
||||
*/
|
||||
export function capitalize(input: string): string {
|
||||
try {
|
||||
return input.charAt(0).toUpperCase() + input.substring(1).toLowerCase();
|
||||
} catch (err) {
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a string, returns a string with the first letter capitalized.
|
||||
* @param input capitalize the first letter of this string
|
||||
* @returns the string with the first letter capitalized
|
||||
*/
|
||||
export function capitalizeFirstLetter(input: string): string {
|
||||
return input.charAt(0).toUpperCase() + input.slice(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cuts the
|
||||
* @param input The string to ellipsify.
|
||||
* @param length The length of the string to return.
|
||||
* @returns The ellipsified string.
|
||||
*/
|
||||
export const ellipsify = (input: string, chars: number): string => {
|
||||
let ellipisifed = input;
|
||||
if (input && input.length > chars) {
|
||||
ellipisifed = `${input.substring(0, chars)}...`;
|
||||
}
|
||||
return ellipisifed;
|
||||
};
|
||||
19
src/shared/util/time.ts
Normal file
19
src/shared/util/time.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
export const MILLISECOND = 1;
|
||||
export const SECOND = 1000 * MILLISECOND;
|
||||
export const MINUTE = 60 * SECOND;
|
||||
export const HOUR = 60 * MINUTE;
|
||||
export const DAY = 24 * HOUR;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
export const sleep = (milliseconds: number): Promise<void> => new Promise(resolve => setTimeout(resolve, milliseconds));
|
||||
|
||||
/**
|
||||
* Checks to see if expired by the time first stored and the time frame that it is stored for
|
||||
*
|
||||
* @param time time it was stored
|
||||
* @param threshold time frame it can be stored for
|
||||
* @return true if expired, false if the time frame is still in range
|
||||
*/
|
||||
export const didExpire = (time: number, threshold: number): boolean => time + threshold <= Date.now();
|
||||
Reference in New Issue
Block a user