work for #183
All checks were successful
PR Check / lint-and-format (pull_request) Successful in 19s
All checks were successful
PR Check / lint-and-format (pull_request) Successful in 19s
This commit is contained in:
parent
ab262152a6
commit
e963114cc5
2 changed files with 72 additions and 12 deletions
|
@ -1,4 +1,4 @@
|
|||
import { showDate, strToSeconds } from "../../utils/time";
|
||||
import { nextTimeUnit, showDate, strToSeconds, TimeSecond } from "../../utils/time";
|
||||
|
||||
describe("Date with correct timezone", () => {
|
||||
const map = new Map([["u_time_at", "@"]]);
|
||||
|
@ -39,10 +39,48 @@ describe("String time to seconds", () => {
|
|||
});
|
||||
}
|
||||
{
|
||||
// todo: see https://git.mylloon.fr/ConfrerieDuKassoulait/Botanique/issues/183
|
||||
const name = "12h30";
|
||||
test(name, () => {
|
||||
/* expect(strToSeconds(name)).toBe(45000); */
|
||||
expect(strToSeconds(name)).toBe(45000);
|
||||
});
|
||||
}
|
||||
{
|
||||
const name = "12s30";
|
||||
test(name, () => {
|
||||
expect(strToSeconds(name)).toBe(42);
|
||||
});
|
||||
}
|
||||
{
|
||||
const name = "1w30h20";
|
||||
test(name, () => {
|
||||
expect(strToSeconds(name)).toBe(714000);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe("Next time unit", () => {
|
||||
{
|
||||
const name = TimeSecond.Minute;
|
||||
test(name.toString(), () => {
|
||||
expect(nextTimeUnit(name)).toBe(TimeSecond.Second);
|
||||
});
|
||||
}
|
||||
{
|
||||
const name = TimeSecond.Hour;
|
||||
test(name.toString(), () => {
|
||||
expect(nextTimeUnit(name)).toBe(TimeSecond.Minute);
|
||||
});
|
||||
}
|
||||
{
|
||||
const name = TimeSecond.Second;
|
||||
test(name.toString(), () => {
|
||||
expect(nextTimeUnit(name)).toBe(TimeSecond.Second);
|
||||
});
|
||||
}
|
||||
{
|
||||
const name = TimeSecond.Year;
|
||||
test(name.toString(), () => {
|
||||
expect(nextTimeUnit(name)).toBe(TimeSecond.Week);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
@ -22,7 +22,7 @@ export const showDate = (tz: string, locale: Map<string, unknown>, date: Date) =
|
|||
return `${formattedDate[0]} ${locale.get("u_time_at")} ${formattedDate[1]}`;
|
||||
};
|
||||
|
||||
enum TimeSecond {
|
||||
export enum TimeSecond {
|
||||
Year = 31536000,
|
||||
Week = 604800,
|
||||
Day = 86400,
|
||||
|
@ -31,6 +31,18 @@ enum TimeSecond {
|
|||
Second = 1,
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next time unit. For example the next unit after Hour is Minute
|
||||
* @param currentUnit Current time unit
|
||||
* @returns The next time unit
|
||||
*/
|
||||
export const nextTimeUnit = (currentUnit: number) => {
|
||||
const units = Object.values(TimeSecond) as number[];
|
||||
|
||||
const index = units.indexOf(currentUnit);
|
||||
return units[index + 1] || TimeSecond.Second;
|
||||
};
|
||||
|
||||
/**
|
||||
* Take a cooldown, for example 2min and transform it to seconds, here: 120s
|
||||
* @param time time in human format
|
||||
|
@ -42,14 +54,15 @@ export const strToSeconds = (time: string) => {
|
|||
return -1;
|
||||
}
|
||||
|
||||
const noUnit = "unmarked";
|
||||
const regex = RegexC(
|
||||
`(?<${TimeSecond[TimeSecond.Year]}>[0-9]+(?=[y|a]))|(?<${
|
||||
TimeSecond[TimeSecond.Week]
|
||||
}>[0-9]+(?=[w]))|(?<${TimeSecond[TimeSecond.Day]}>[0-9]+(?=[d|j]))|(?<${
|
||||
TimeSecond[TimeSecond.Hour]
|
||||
}>[0-9]+(?=[h]))|(?<${TimeSecond[TimeSecond.Minute]}>[0-9]+(?=[m]))|(?<${
|
||||
TimeSecond[TimeSecond.Second]
|
||||
}>[0-9]+(?=[s]?))`,
|
||||
`(?<${TimeSecond[TimeSecond.Year]}>[0-9]+(?=[y|a]))|` +
|
||||
`(?<${TimeSecond[TimeSecond.Week]}>[0-9]+(?=[w]))|` +
|
||||
`(?<${TimeSecond[TimeSecond.Day]}>[0-9]+(?=[d|j]))|` +
|
||||
`(?<${TimeSecond[TimeSecond.Hour]}>[0-9]+(?=[h]))|` +
|
||||
`(?<${TimeSecond[TimeSecond.Minute]}>[0-9]+(?=[m]))|` +
|
||||
`(?<${TimeSecond[TimeSecond.Second]}>[0-9]+(?=[s]))|` +
|
||||
`(?<${noUnit}>[0-9]+)`,
|
||||
RegExpFlags.Global | RegExpFlags.Insensitive,
|
||||
);
|
||||
|
||||
|
@ -60,10 +73,19 @@ export const strToSeconds = (time: string) => {
|
|||
}
|
||||
|
||||
let res = 0;
|
||||
let lastUnit = TimeSecond.Second;
|
||||
data.forEach((match) => {
|
||||
Object.entries(match.groups!).forEach(([key, value]) => {
|
||||
if (value) {
|
||||
res += +value * TimeSecond[key as keyof typeof TimeSecond];
|
||||
let unit;
|
||||
if (key === noUnit) {
|
||||
unit = nextTimeUnit(lastUnit);
|
||||
res += +value * unit;
|
||||
} else {
|
||||
unit = TimeSecond[key as keyof typeof TimeSecond];
|
||||
res += +value * unit;
|
||||
}
|
||||
lastUnit = unit;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue