mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00

Merging note: the typing changes were fixed in a separate PR. * Put rootDirs into subprojects - typings do not work for any ts or svelte files - if we set the 'rootDirs' in ts/tsconfig.json to '../bazel-bin/ts' and then inherit them from e.g. editor, the root will be changed to '../../bazel-bin/ts', however editor needs look in '../../bazel-bin/ts/editor' instead. * Rename i18n and i18n_helpers to i18n-generated and i18n - This way, we can restrict the awkwardness of importing files outside the ts directory within lib * Fix missing typing of i18n and backend_proto by adding back symlinks * Split up i18n-generated into i18n-{translate,modules} * Change i18n from singleton to functions * Revert "Put rootDirs into subprojects" This partially reverts commite1d4292ce3
. It seems like this might not be necessary after all. However some other change made on this branch seems to have fixed the .svelte.d.ts imports * Introduce i18n-bundles to remove circular import There was a circular import i18n.ts <-> i18n-translate.ts * Create own directory for i18n * Move lib/i18n/translate to lib/translate * This restores tree shaking * Update tsconfig libs and module * es2018-2020 have wide support on all modern browsers including * Switch bundles and langs inside i18n to variables again * Add missing copyright header * Rename translate.ts to ftl.ts * Remove the symlinks again I added them to fix to have completion for tr, however this would have also have meant to abandon the tree shaking. As we want to have tree shaking, it's also not necessary to have the symlinks anymore * Revert "Update tsconfig libs and module" This reverts commit0a96776a47
. * move withCollapsedWhitespace back to i18n/utils * Add back /ts as in rootDirs
179 lines
5.2 KiB
TypeScript
179 lines
5.2 KiB
TypeScript
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
import * as tr from "./ftl";
|
|
|
|
export const SECOND = 1.0;
|
|
export const MINUTE = 60.0 * SECOND;
|
|
export const HOUR = 60.0 * MINUTE;
|
|
export const DAY = 24.0 * HOUR;
|
|
export const MONTH = 30.0 * DAY;
|
|
export const YEAR = 12.0 * MONTH;
|
|
|
|
export enum TimespanUnit {
|
|
Seconds,
|
|
Minutes,
|
|
Hours,
|
|
Days,
|
|
Months,
|
|
Years,
|
|
}
|
|
|
|
export function unitName(unit: TimespanUnit): string {
|
|
switch (unit) {
|
|
case TimespanUnit.Seconds:
|
|
return "seconds";
|
|
case TimespanUnit.Minutes:
|
|
return "minutes";
|
|
case TimespanUnit.Hours:
|
|
return "hours";
|
|
case TimespanUnit.Days:
|
|
return "days";
|
|
case TimespanUnit.Months:
|
|
return "months";
|
|
case TimespanUnit.Years:
|
|
return "years";
|
|
}
|
|
}
|
|
|
|
export function naturalUnit(secs: number): TimespanUnit {
|
|
secs = Math.abs(secs);
|
|
if (secs < MINUTE) {
|
|
return TimespanUnit.Seconds;
|
|
} else if (secs < HOUR) {
|
|
return TimespanUnit.Minutes;
|
|
} else if (secs < DAY) {
|
|
return TimespanUnit.Hours;
|
|
} else if (secs < MONTH) {
|
|
return TimespanUnit.Days;
|
|
} else if (secs < YEAR) {
|
|
return TimespanUnit.Months;
|
|
} else {
|
|
return TimespanUnit.Years;
|
|
}
|
|
}
|
|
|
|
/// Number of seconds in a given unit.
|
|
export function unitSeconds(unit: TimespanUnit): number {
|
|
switch (unit) {
|
|
case TimespanUnit.Seconds:
|
|
return SECOND;
|
|
case TimespanUnit.Minutes:
|
|
return MINUTE;
|
|
case TimespanUnit.Hours:
|
|
return HOUR;
|
|
case TimespanUnit.Days:
|
|
return DAY;
|
|
case TimespanUnit.Months:
|
|
return MONTH;
|
|
case TimespanUnit.Years:
|
|
return YEAR;
|
|
}
|
|
}
|
|
|
|
export function unitAmount(unit: TimespanUnit, secs: number): number {
|
|
return secs / unitSeconds(unit);
|
|
}
|
|
|
|
/// Largest unit provided seconds can be divided by without a remainder.
|
|
export function naturalWholeUnit(secs: number): TimespanUnit {
|
|
let unit = naturalUnit(secs);
|
|
while (unit != TimespanUnit.Seconds) {
|
|
const amount = Math.round(unitAmount(unit, secs));
|
|
if (Math.abs(secs - amount * unitSeconds(unit)) < Number.EPSILON) {
|
|
return unit;
|
|
}
|
|
unit -= 1;
|
|
}
|
|
return unit;
|
|
}
|
|
|
|
export function studiedToday(cards: number, secs: number): string {
|
|
const unit = naturalUnit(secs);
|
|
const amount = unitAmount(unit, secs);
|
|
const name = unitName(unit);
|
|
|
|
let secsPer = 0;
|
|
if (cards > 0) {
|
|
secsPer = secs / cards;
|
|
}
|
|
return tr.statisticsStudiedToday({
|
|
unit: name,
|
|
secsPerCard: secsPer,
|
|
cards,
|
|
amount,
|
|
});
|
|
}
|
|
|
|
function i18nFuncForUnit(
|
|
unit: TimespanUnit,
|
|
short: boolean
|
|
): ({ amount: number }) => string {
|
|
if (short) {
|
|
switch (unit) {
|
|
case TimespanUnit.Seconds:
|
|
return tr.statisticsElapsedTimeSeconds;
|
|
case TimespanUnit.Minutes:
|
|
return tr.statisticsElapsedTimeMinutes;
|
|
case TimespanUnit.Hours:
|
|
return tr.statisticsElapsedTimeHours;
|
|
case TimespanUnit.Days:
|
|
return tr.statisticsElapsedTimeDays;
|
|
case TimespanUnit.Months:
|
|
return tr.statisticsElapsedTimeMonths;
|
|
case TimespanUnit.Years:
|
|
return tr.statisticsElapsedTimeYears;
|
|
}
|
|
} else {
|
|
switch (unit) {
|
|
case TimespanUnit.Seconds:
|
|
return tr.schedulingTimeSpanSeconds;
|
|
case TimespanUnit.Minutes:
|
|
return tr.schedulingTimeSpanMinutes;
|
|
case TimespanUnit.Hours:
|
|
return tr.schedulingTimeSpanHours;
|
|
case TimespanUnit.Days:
|
|
return tr.schedulingTimeSpanDays;
|
|
case TimespanUnit.Months:
|
|
return tr.schedulingTimeSpanMonths;
|
|
case TimespanUnit.Years:
|
|
return tr.schedulingTimeSpanYears;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Describe the given seconds using the largest appropriate unit.
|
|
/// If precise is true, show to two decimal places, eg
|
|
/// eg 70 seconds -> "1.17 minutes"
|
|
/// If false, seconds and days are shown without decimals.
|
|
export function timeSpan(seconds: number, short = false): string {
|
|
const unit = naturalUnit(seconds);
|
|
const amount = unitAmount(unit, seconds);
|
|
return i18nFuncForUnit(unit, short)({ amount });
|
|
}
|
|
|
|
export function dayLabel(daysStart: number, daysEnd: number): string {
|
|
const larger = Math.max(Math.abs(daysStart), Math.abs(daysEnd));
|
|
const smaller = Math.min(Math.abs(daysStart), Math.abs(daysEnd));
|
|
if (larger - smaller <= 1) {
|
|
// singular
|
|
if (daysStart >= 0) {
|
|
return tr.statisticsInDaysSingle({ days: daysStart });
|
|
} else {
|
|
return tr.statisticsDaysAgoSingle({ days: -daysStart });
|
|
}
|
|
} else {
|
|
// range
|
|
if (daysStart >= 0) {
|
|
return tr.statisticsInDaysRange({
|
|
daysStart,
|
|
daysEnd: daysEnd - 1,
|
|
});
|
|
} else {
|
|
return tr.statisticsDaysAgoRange({
|
|
daysStart: Math.abs(daysEnd - 1),
|
|
daysEnd: -daysStart,
|
|
});
|
|
}
|
|
}
|
|
}
|