forked from atums.world/backend
add invites, fix apiauth query, use luxon for date management, change readme
This commit is contained in:
parent
9fcaac4dfb
commit
9a91f1e7e3
10 changed files with 436 additions and 29 deletions
|
@ -1,3 +1,5 @@
|
|||
import { DateTime } from "luxon";
|
||||
|
||||
export function timestampToReadable(timestamp?: number): string {
|
||||
const date: Date =
|
||||
timestamp && !isNaN(timestamp) ? new Date(timestamp) : new Date();
|
||||
|
@ -11,3 +13,90 @@ export function isUUID(uuid: string): boolean {
|
|||
|
||||
return regex.test(uuid);
|
||||
}
|
||||
|
||||
export function getNewTimeUTC(
|
||||
durationStr: string,
|
||||
from: DateTime = DateTime.utc(),
|
||||
): string | null {
|
||||
const duration: DurationObject = parseDuration(durationStr);
|
||||
|
||||
const newTime: DateTime = from.plus({
|
||||
years: duration.years,
|
||||
months: duration.months,
|
||||
weeks: duration.weeks,
|
||||
days: duration.days,
|
||||
hours: duration.hours,
|
||||
minutes: duration.minutes,
|
||||
seconds: duration.seconds,
|
||||
});
|
||||
|
||||
return newTime.toSQL({ includeOffset: false });
|
||||
}
|
||||
|
||||
export function parseDuration(input: string): DurationObject {
|
||||
const regex: RegExp = /(\d+)(y|mo|w|d|h|m|s)/g;
|
||||
const matches: RegExpMatchArray[] = [...input.matchAll(regex)];
|
||||
|
||||
const duration: DurationObject = {
|
||||
years: 0,
|
||||
months: 0,
|
||||
weeks: 0,
|
||||
days: 0,
|
||||
hours: 0,
|
||||
minutes: 0,
|
||||
seconds: 0,
|
||||
};
|
||||
|
||||
for (const match of matches) {
|
||||
const value: number = parseInt(match[1], 10);
|
||||
const unit: string = match[2];
|
||||
|
||||
switch (unit) {
|
||||
case "y":
|
||||
duration.years = value;
|
||||
break;
|
||||
case "mo":
|
||||
duration.months = value;
|
||||
break;
|
||||
case "w":
|
||||
duration.weeks = value;
|
||||
break;
|
||||
case "d":
|
||||
duration.days = value;
|
||||
break;
|
||||
case "h":
|
||||
duration.hours = value;
|
||||
break;
|
||||
case "m":
|
||||
duration.minutes = value;
|
||||
break;
|
||||
case "s":
|
||||
duration.seconds = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return duration;
|
||||
}
|
||||
|
||||
export function isValidTimezone(timezone: string): boolean {
|
||||
return DateTime.local().setZone(timezone).isValid;
|
||||
}
|
||||
|
||||
export function generateRandomString(length?: number): string {
|
||||
if (!length) {
|
||||
length = length || Math.floor(Math.random() * 10) + 5;
|
||||
}
|
||||
|
||||
const characters: string =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
let result: string = "";
|
||||
|
||||
for (let i: number = 0; i < length; i++) {
|
||||
result += characters.charAt(
|
||||
Math.floor(Math.random() * characters.length),
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue