Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: switch toString w/ toLocaleString to be able to format numbers in formatDiff #269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/format.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { formatDiff, diffSec } from './utils/date';
import { getLocale } from './register';
import { Opts, TDate } from './interface';
import { getLocale } from './register';
import { diffSec, formatDiff } from './utils/date';

/**
* format a TDate into string
Expand All @@ -12,5 +12,5 @@ export const format = (date: TDate, locale?: string, opts?: Opts): string => {
// diff seconds
const sec = diffSec(date, opts && opts.relativeDate);
// format it with locale
return formatDiff(sec, getLocale(locale));
return formatDiff(sec, getLocale(locale), opts?.numberLocale);
};
2 changes: 2 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ export type Opts = {
readonly relativeDate?: TDate;
/** the realtime */
readonly minInterval?: number;
/** the locale used to format the number part of the relative time */
readonly numberLocale?: string;
};
19 changes: 11 additions & 8 deletions src/realtime.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { setTimerId, getTimerId, getDateAttribute } from './utils/dom';
import { formatDiff, diffSec, nextInterval } from './utils/date';
import { getLocale } from './register';
import { LocaleFunc, Opts, TimerPool } from './interface';
import { getLocale } from './register';
import { diffSec, formatDiff, nextInterval } from './utils/date';
import { getDateAttribute, getTimerId, setTimerId } from './utils/dom';

// all realtime timer
const TIMER_POOL: TimerPool = {};
Expand All @@ -20,16 +20,19 @@ function run(node: HTMLElement, date: string, localeFunc: LocaleFunc, opts: Opts
// clear the node's exist timer
clear(getTimerId(node));

const { relativeDate, minInterval } = opts;
const { relativeDate, minInterval, numberLocale } = opts;

// get diff seconds
const diff = diffSec(date, relativeDate);
// render
node.innerText = formatDiff(diff, localeFunc);
node.innerText = formatDiff(diff, localeFunc, numberLocale);

const tid = (setTimeout(() => {
run(node, date, localeFunc, opts);
}, Math.min(Math.max(nextInterval(diff), minInterval || 1) * 1000, 0x7fffffff)) as unknown) as number;
const tid = setTimeout(
() => {
run(node, date, localeFunc, opts);
},
Math.min(Math.max(nextInterval(diff), minInterval || 1) * 1000, 0x7fffffff),
) as unknown as number;

// there is no need to save node in object. Just save the key
TIMER_POOL[tid] = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function toDate(input?: Date | string | number): Date {
* @param localeFunc
* @returns
*/
export function formatDiff(diff: number, localeFunc: LocaleFunc): string {
export function formatDiff(diff: number, localeFunc: LocaleFunc, numberLocale?: string): string {
/**
* if locale is not exist, use defaultLocale.
* if defaultLocale is not exist, use build-in `en`.
Expand Down Expand Up @@ -90,7 +90,7 @@ export function formatDiff(diff: number, localeFunc: LocaleFunc): string {

if (diff > (idx === 0 ? 9 : 1)) idx += 1;

return localeFunc(diff, idx, totalSec)[agoIn].replace('%s', diff.toString());
return localeFunc(diff, idx, totalSec)[agoIn].replace('%s', diff.toLocaleString(numberLocale));
}

/**
Expand Down