Skip to content

Commit

Permalink
chore: refactor to dayjs
Browse files Browse the repository at this point in the history
  • Loading branch information
qlaffont committed Jan 22, 2024
1 parent a92d01a commit 00cbd15
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 127 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ Complete Intl/I18n solution for browser and node. Old Owner: [@flexper](https://

```js
const { rosetty } = require('rosetty');
const enGB = require('dayjs/locale/en-gb');
const { enGB } = require('date-fns/locale');

const r = rosetty(
{
en: {
dict: {
test: 'This is a test',
},
locale: enLocale,
locale: enGB,
},
},
'en'
Expand Down Expand Up @@ -51,17 +51,18 @@ console.log(r.t('test')); // This is a test
| listFormat | [Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/ListFormat) | Language-sensitive list formatting <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/ListFormat> |
| numberFormat | [Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat) | Language-sensitive list formatting <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat> |
| pluralRules | [Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/PluralRules) | Plural-sensitive formatting and plural-related language rules <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/PluralRules> |
| format | [Documentation](https://day.js.org/docs/en/display/format) | Return the formatted date string in the given format |
| formatDistance | [Documentation](https://day.js.org/docs/en/display/from) | Return the distance between the given dates in words. |
| formatDistanceToNow | [Documentation](https://day.js.org/docs/en/display/from-now) | Return the distance between the given date and now in words. |
| formatDuration | [Documentation](https://day.js.org/docs/en/durations/humanize) | Return human-readable duration string i.e. "9 months 2 days" |
| format | [Documentation](https://date-fns.org/v3.3.0/docs/format) | Return the formatted date string in the given format <https://date-fns.org/v3.3.0/docs/format> |
| formatRelative | [Documentation](https://date-fns.org/v3.3.0/docs/formatRelative) | Represent the date in words relative to the given base date. <https://date-fns.org/v3.3.0/docs/formatRelative> |
| formatDistance | [Documentation](https://date-fns.org/v3.3.0/docs/formatDistance) | Return the distance between the given dates in words. <https://date-fns.org/v3.3.0/docs/formatDistance> |
| formatDistanceToNow | [Documentation](https://date-fns.org/v3.3.0/docs/formatDistanceToNow) | Return the distance between the given date and now in words. <https://date-fns.org/v3.3.0/docs/formatDistanceToNow> |
| formatDuration | [Documentation](https://date-fns.org/v3.3.0/docs/formatDuration) | Return human-readable duration string i.e. "9 months 2 days" <https://date-fns.org/v3.3.0/docs/formatDuration> | |

### WARNING FOR LOCALE !

**You need to import locale from `dayjs` package.**
**You need to import locale from `date-fns` package.**

```js
const enGB = require('dayjs/locale/en-gb');
const { enGB } = require('date-fns/locale');
```

## Maintain
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.1.0",
"version": "3.0.0",
"license": "MIT",
"main": "./dist/index.cjs",
"typings": "dist/index.d.ts",
Expand Down Expand Up @@ -84,7 +84,7 @@
"typescript": "5.2.2"
},
"dependencies": {
"dayjs": "^1.11.9",
"date-fns": "^3.3.0",
"rosetta": "^1.1.0"
},
"types": "./dist/index.d.ts",
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

175 changes: 77 additions & 98 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-explicit-any */

import DAYJS from 'dayjs';
import duration from 'dayjs/plugin/duration';
import localizedFormat from 'dayjs/plugin/localizedFormat';
import relativeTime from 'dayjs/plugin/relativeTime';
import type { Locale } from 'date-fns';
import {
format,
formatDistance,
formatDistanceToNow,
formatDuration,
formatRelative,
} from 'date-fns';
import rosetta from 'rosetta';

type BreakDownObject<O, R = void> = {
Expand All @@ -16,11 +20,6 @@ type BreakDownObject<O, R = void> = {
: never;
};

const dayjs = DAYJS;
dayjs.extend(relativeTime);
dayjs.extend(duration);
dayjs.extend(localizedFormat);

export type ObjectDotNotation<O, R = void> = O extends string
? R extends string
? R
Expand All @@ -29,7 +28,7 @@ export type ObjectDotNotation<O, R = void> = O extends string

export interface Language {
dict: Record<string, unknown>;
locale: ILocale;
locale: Locale;
}

export interface RosettyReturn<T> {
Expand All @@ -43,70 +42,43 @@ export interface RosettyReturn<T> {
) => string | undefined;
displayNames: (
langCode: string,
options: Partial<DisplayNamesOptions>
options: Intl.DisplayNamesOptions
) => string | undefined;
listFormat: (
list: string[],
options: Partial<IntlListFormatOptions>
options: Intl.ListFormatOptions
) => string | undefined;
numberFormat: (
value: number,
options: Partial<NumberFormatOptions>
options: Intl.NumberFormatOptions
) => string | undefined;
pluralRules: (
value: number,
options: {
type: 'cardinal' | 'ordinal';
}
options: Intl.PluralRulesOptions
) => string | undefined;
format: (date: number | Date, stringFormat: string) => string;
formatRelative: (date: number | Date, baseDate: number | Date) => string;
formatDistance: (date: number | Date, baseDate: number | Date) => string;
formatDistanceToNow: (date: number | Date) => string;
formatDuration: (duration: object) => string;
}

export interface DisplayNamesOptions {
localeMatcher: 'lookup' | 'best fit';
style: 'narrow' | 'short' | 'long';
type:
| 'calendar'
| 'language'
| 'region'
| 'script'
| 'currency'
| 'dateTimeField';
fallback: 'code' | 'none';
}

export interface IntlListFormatOptions {
localeMatcher: 'lookup' | 'best fit';
style: 'narrow' | 'short' | 'long';
type: 'conjunction' | 'disjunction' | 'unit';
}

export interface NumberFormatOptions {
compactDisplay: 'short' | 'long';
currency: string;
currencyDisplay: 'symbol' | 'code' | 'name' | 'narrowSymbol';
currencySign: 'standard' | 'accounting';
localeMatcher: 'lookup' | 'best fit';
notation: 'standard' | 'scientific' | 'engineering' | 'compact';
numberingSystem: string;
signDisplay: 'auto' | 'always' | 'never' | 'negative' | 'exceptZero';
style: 'decimal' | 'percent' | 'currency' | 'unit';
unit: string;
unitDisplay: 'long' | 'short' | 'narrow';
useGrouping: 'always' | 'auto' | 'false' | 'true' | 'min2';
roundingMode: 'ceil' | 'floor' | 'expand' | 'trunc';
roundingPriority: 'auto' | 'morePrecision' | 'lessPrecision';
roundingIncrement: number;
trailingZeroDisplay: 'auto' | 'stripIfInteger' | 'lessPrecision';
minimumIntegerDigits: number;
minimumFractionDigits: number;
maximumFractionDigits: number;
minimumSignificantDigits: number;
maximumSignificantDigits: number;
format: (
date: number | Date,
stringFormat: string,
options?: Parameters<typeof format>[2]
) => string;
formatRelative: (
date: number | Date,
baseDate: number | Date,
options?: Parameters<typeof formatRelative>[2]
) => string;
formatDistance: (
date: number | Date,
baseDate: number | Date,
options?: Parameters<typeof formatDistance>[2]
) => string;
formatDistanceToNow: (
date: number | Date,
options?: Parameters<typeof formatDistanceToNow>[1]
) => string;
formatDuration: (
duration: object,
options?: Parameters<typeof formatDuration>[1]
) => string;
}

export const rosetty = <T>(
Expand Down Expand Up @@ -186,53 +158,60 @@ export const rosetty = <T>(
}
},
//Intl Polyfill
displayNames: (langCode: string, options: Partial<DisplayNamesOptions>) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
displayNames: (langCode: string, options: Intl.DisplayNamesOptions) => {
return new Intl.DisplayNames(
[actualConfig?.locale.name as string],
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
[actualConfig?.locale.code as string],
options
).of(langCode);
},
listFormat: (list: string[], options: Partial<IntlListFormatOptions>) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
return new Intl.ListFormat(actualConfig?.locale.name, options).format(
listFormat: (list: string[], options: Intl.ListFormatOptions) => {
return new Intl.ListFormat(actualConfig?.locale.code, options).format(
list
);
},
numberFormat: (value: number, options: Partial<NumberFormatOptions>) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
return new Intl.NumberFormat(actualConfig?.locale.name, options).format(
numberFormat: (value: number, options: Intl.NumberFormatOptions) => {
return new Intl.NumberFormat(actualConfig?.locale.code, options).format(
value
);
},
pluralRules: (
value: number,
options: {
type: 'cardinal' | 'ordinal';
}
) => {
return new Intl.PluralRules(actualConfig?.locale.name, options).select(
pluralRules: (value: number, options: Intl.PluralRulesOptions) => {
return new Intl.PluralRules(actualConfig?.locale.code, options).select(
value
);
},
//Date FNS i18n
format: (date: number | Date, stringFormat: string) =>
dayjs(date).locale(actualConfig!.locale).format(stringFormat),
/**
* @deprecated Since version 2.0. Will be deleted in version 3.0. Use formatDistance instead.
*/
formatRelative: (date: number | Date, baseDate: number | Date) =>
dayjs(date).locale(actualConfig!.locale).from(dayjs(baseDate), true),
formatDistance: (date: number | Date, baseDate: number | Date) =>
dayjs(date).locale(actualConfig!.locale).from(dayjs(baseDate), true),
formatDistanceToNow: (date: number | Date) =>
dayjs(date).locale(actualConfig!.locale).fromNow(true),
formatDuration: (duration: object) =>
dayjs.duration(duration).locale(actualConfig!.locale.name).humanize(),
format: (
date: number | Date,
stringFormat: string,
options?: Parameters<typeof format>[2]
) =>
format(date, stringFormat, { ...options, locale: actualConfig!.locale }),
formatRelative: (
date: number | Date,
baseDate: number | Date,
options?: Parameters<typeof formatRelative>[2]
) =>
formatRelative(date, baseDate, {
...options,
locale: actualConfig!.locale,
}),
formatDistance: (
date: number | Date,
baseDate: number | Date,
options?: Parameters<typeof formatDistance>[2]
) =>
formatDistance(date, baseDate, {
...options,
locale: actualConfig!.locale,
}),
formatDistanceToNow: (
date: number | Date,
options?: Parameters<typeof formatDistanceToNow>[1]
) =>
formatDistanceToNow(date, { ...options, locale: actualConfig!.locale }),
formatDuration: (
duration: object,
options?: Parameters<typeof formatDuration>[1]
) => formatDuration(duration, { ...options, locale: actualConfig!.locale }),
};
};
Loading

0 comments on commit 00cbd15

Please sign in to comment.