-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #884 from InseeFrLab:feat-share
Feat share file
- Loading branch information
Showing
39 changed files
with
1,699 additions
and
395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const SECOND = 1000; | ||
|
||
export const TIME_UNITS = { | ||
SECOND, | ||
MINUTE: 60 * SECOND, | ||
HOUR: 60 * 60 * SECOND, | ||
DAY: 24 * 60 * 60 * SECOND, | ||
WEEK: 7 * 24 * 60 * 60 * SECOND, | ||
MONTH: 30 * 24 * 60 * 60 * SECOND, | ||
YEAR: 365 * 24 * 60 * 60 * SECOND | ||
}; | ||
|
||
export const DURATION_DIVISOR_KEYS = [ | ||
"second", | ||
"minute", | ||
"hour", | ||
"day", | ||
"week", | ||
"month", | ||
"year" | ||
] as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { assert } from "tsafe/assert"; | ||
import { DurationTranslationFunction } from "./type"; | ||
import { TIME_UNITS, DURATION_DIVISOR_KEYS } from "./constants"; | ||
|
||
export const { formatDuration } = (() => { | ||
const { getDurationUnits } = (() => { | ||
type DurationUnit = { | ||
max: number; | ||
divisor: number; | ||
singular: string; | ||
plural: string; | ||
}; | ||
|
||
function getDurationUnits(t: DurationTranslationFunction): DurationUnit[] { | ||
return DURATION_DIVISOR_KEYS.map(divisorKey => ({ | ||
divisor: (() => { | ||
switch (divisorKey) { | ||
case "second": | ||
return TIME_UNITS.SECOND; | ||
case "minute": | ||
return TIME_UNITS.MINUTE; | ||
case "hour": | ||
return TIME_UNITS.HOUR; | ||
case "day": | ||
return TIME_UNITS.DAY; | ||
case "week": | ||
return TIME_UNITS.WEEK; | ||
case "month": | ||
return TIME_UNITS.MONTH; | ||
case "year": | ||
return TIME_UNITS.YEAR; | ||
} | ||
})(), | ||
max: (() => { | ||
switch (divisorKey) { | ||
case "second": | ||
return TIME_UNITS.MINUTE; | ||
case "minute": | ||
return TIME_UNITS.HOUR; | ||
case "hour": | ||
return 3 * TIME_UNITS.DAY; | ||
case "day": | ||
return TIME_UNITS.WEEK + TIME_UNITS.DAY; | ||
case "week": | ||
return TIME_UNITS.MONTH; | ||
case "month": | ||
return TIME_UNITS.YEAR; | ||
case "year": | ||
return Infinity; | ||
} | ||
})(), | ||
singular: t("singular", { divisorKey }), | ||
plural: t("plural", { divisorKey }) | ||
})); | ||
} | ||
|
||
return { getDurationUnits }; | ||
})(); | ||
|
||
function formatDuration(params: { | ||
durationSeconds: number; | ||
t: DurationTranslationFunction; | ||
}): string { | ||
const { durationSeconds, t } = params; | ||
|
||
for (const unit of getDurationUnits(t)) { | ||
if (durationSeconds * 1000 < unit.max) { | ||
const x = Math.round(durationSeconds / (unit.divisor / 1000)); | ||
return x === 1 ? unit.singular : unit.plural.replace("#", `${x}`); | ||
} | ||
} | ||
assert(false); | ||
} | ||
|
||
return { formatDuration }; | ||
})(); | ||
|
||
export const englishDurationFormatter: DurationTranslationFunction = (key, params) => { | ||
const en = { | ||
singular: { | ||
second: "1 second", | ||
minute: "1 minute", | ||
hour: "1 hour", | ||
day: "1 day", | ||
week: "1 week", | ||
month: "1 month", | ||
year: "1 year" | ||
}, | ||
plural: { | ||
second: "# seconds", | ||
minute: "# minutes", | ||
hour: "# hours", | ||
day: "# days", | ||
week: "# weeks", | ||
month: "# months", | ||
year: "# years" | ||
} | ||
}; | ||
return en[key][params.divisorKey]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { DURATION_DIVISOR_KEYS } from "./constants"; | ||
|
||
export type DurationDivisorKey = (typeof DURATION_DIVISOR_KEYS)[number]; | ||
|
||
export type DurationTranslationFunction = ( | ||
key: "singular" | "plural", | ||
params: { divisorKey: DurationDivisorKey } | ||
) => string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.