@nano_kit/intl 1.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +95 -0
  3. package/dist/context.d.ts +38 -0
  4. package/dist/context.d.ts.map +1 -0
  5. package/dist/deflat.d.ts +49 -0
  6. package/dist/deflat.d.ts.map +1 -0
  7. package/dist/direction.d.ts +15 -0
  8. package/dist/direction.d.ts.map +1 -0
  9. package/dist/format/datetime.d.ts +37 -0
  10. package/dist/format/datetime.d.ts.map +1 -0
  11. package/dist/format/duration.d.ts +29 -0
  12. package/dist/format/duration.d.ts.map +1 -0
  13. package/dist/format/format.d.ts +9 -0
  14. package/dist/format/format.d.ts.map +1 -0
  15. package/dist/format/index.d.ts +16 -0
  16. package/dist/format/index.d.ts.map +1 -0
  17. package/dist/format/intlformat.d.ts +7 -0
  18. package/dist/format/intlformat.d.ts.map +1 -0
  19. package/dist/format/list.d.ts +28 -0
  20. package/dist/format/list.d.ts.map +1 -0
  21. package/dist/format/markup.d.ts +17 -0
  22. package/dist/format/markup.d.ts.map +1 -0
  23. package/dist/format/match.d.ts +26 -0
  24. package/dist/format/match.d.ts.map +1 -0
  25. package/dist/format/number.d.ts +37 -0
  26. package/dist/format/number.d.ts.map +1 -0
  27. package/dist/format/params.d.ts +31 -0
  28. package/dist/format/params.d.ts.map +1 -0
  29. package/dist/format/plural.d.ts +21 -0
  30. package/dist/format/plural.d.ts.map +1 -0
  31. package/dist/format/range.d.ts +36 -0
  32. package/dist/format/range.d.ts.map +1 -0
  33. package/dist/format/raw.d.ts +13 -0
  34. package/dist/format/raw.d.ts.map +1 -0
  35. package/dist/format/relativetime.d.ts +31 -0
  36. package/dist/format/relativetime.d.ts.map +1 -0
  37. package/dist/format/rich.d.ts +33 -0
  38. package/dist/format/rich.d.ts.map +1 -0
  39. package/dist/format/text.d.ts +34 -0
  40. package/dist/format/text.d.ts.map +1 -0
  41. package/dist/format/types.d.ts +21 -0
  42. package/dist/format/types.d.ts.map +1 -0
  43. package/dist/index.d.ts +7 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +425 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/intl.d.ts +11 -0
  48. package/dist/intl.d.ts.map +1 -0
  49. package/dist/types.d.ts +30 -0
  50. package/dist/types.d.ts.map +1 -0
  51. package/dist/utils.d.ts +2 -0
  52. package/dist/utils.d.ts.map +1 -0
  53. package/package.json +47 -0
@@ -0,0 +1,21 @@
1
+ import type { AnyFormat, Format, FormatsInput } from './types.js';
2
+ import { type MatchFn, type CasesFn, cases } from './match.js';
3
+ export type PluralForm = Intl.LDMLPluralRule;
4
+ export type PluralKey = PluralForm | `${number}`;
5
+ export { cases as forms };
6
+ /**
7
+ * Creates a plural formatter.
8
+ * @param param - Parameter name used to select plural form and replace `{param}` placeholders.
9
+ * @param forms - Plural form formatters keyed by LDML plural rule or exact numeric value.
10
+ * @returns Formatter that resolves plural forms and returns a parameterized message function.
11
+ */
12
+ export declare function plural<const K extends string, const B extends Partial<Record<PluralKey, AnyFormat>>>(param: K, forms?: CasesFn<K, B>): Format<FormatsInput<B>, MatchFn<K, B>>;
13
+ /**
14
+ * Creates a plural formatter with `Intl.PluralRules` options.
15
+ * @param param - Parameter name used to select plural form and replace `{param}` placeholders.
16
+ * @param options - `Intl.PluralRules` options.
17
+ * @param forms - Plural form formatters keyed by LDML plural rule or exact numeric value.
18
+ * @returns Formatter that resolves plural forms and returns a parameterized message function.
19
+ */
20
+ export declare function plural<const K extends string, const B extends Partial<Record<PluralKey, AnyFormat>>>(param: K, options: Intl.PluralRulesOptions, forms?: CasesFn<K, B>): Format<FormatsInput<B>, MatchFn<K, B>>;
21
+ //# sourceMappingURL=plural.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plural.d.ts","sourceRoot":"","sources":["../../src/format/plural.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,SAAS,EACT,MAAM,EACN,YAAY,EACb,MAAM,YAAY,CAAA;AACnB,OAAO,EACL,KAAK,OAAO,EACZ,KAAK,OAAO,EAEZ,KAAK,EACN,MAAM,YAAY,CAAA;AAEnB,MAAM,MAAM,UAAU,GAAG,IAAI,CAAC,cAAc,CAAA;AAE5C,MAAM,MAAM,SAAS,GAAG,UAAU,GAAG,GAAG,MAAM,EAAE,CAAA;AAEhD,OAAO,EAAE,KAAK,IAAI,KAAK,EAAE,CAAA;AAEzB;;;;;GAKG;AACH,wBAAgB,MAAM,CACpB,KAAK,CAAC,CAAC,SAAS,MAAM,EACtB,KAAK,CAAC,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,EAErD,KAAK,EAAE,CAAC,EACR,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,GACpB,MAAM,CACP,YAAY,CAAC,CAAC,CAAC,EACf,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CACd,CAAA;AAED;;;;;;GAMG;AACH,wBAAgB,MAAM,CACpB,KAAK,CAAC,CAAC,SAAS,MAAM,EACtB,KAAK,CAAC,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,EAErD,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,IAAI,CAAC,kBAAkB,EAChC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,GACpB,MAAM,CACP,YAAY,CAAC,CAAC,CAAC,EACf,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CACd,CAAA","sourcesContent":["import { isFunction } from '@nano_kit/store'\nimport { memo } from '../utils.js'\nimport type {\n AnyFormat,\n Format,\n FormatsInput\n} from './types.js'\nimport {\n type MatchFn,\n type CasesFn,\n match,\n cases\n} from './match.js'\n\nexport type PluralForm = Intl.LDMLPluralRule\n\nexport type PluralKey = PluralForm | `${number}`\n\nexport { cases as forms }\n\n/**\n * Creates a plural formatter.\n * @param param - Parameter name used to select plural form and replace `{param}` placeholders.\n * @param forms - Plural form formatters keyed by LDML plural rule or exact numeric value.\n * @returns Formatter that resolves plural forms and returns a parameterized message function.\n */\nexport function plural<\n const K extends string,\n const B extends Partial<Record<PluralKey, AnyFormat>>\n>(\n param: K,\n forms?: CasesFn<K, B>\n): Format<\n FormatsInput<B>,\n MatchFn<K, B>\n>\n\n/**\n * Creates a plural formatter with `Intl.PluralRules` options.\n * @param param - Parameter name used to select plural form and replace `{param}` placeholders.\n * @param options - `Intl.PluralRules` options.\n * @param forms - Plural form formatters keyed by LDML plural rule or exact numeric value.\n * @returns Formatter that resolves plural forms and returns a parameterized message function.\n */\nexport function plural<\n const K extends string,\n const B extends Partial<Record<PluralKey, AnyFormat>>\n>(\n param: K,\n options: Intl.PluralRulesOptions,\n forms?: CasesFn<K, B>\n): Format<\n FormatsInput<B>,\n MatchFn<K, B>\n>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function plural<\n const K extends string,\n const B extends Partial<Record<PluralKey, AnyFormat>>\n>(\n param: K,\n optionsOrForms?: Intl.PluralRulesOptions | CasesFn<K, B>,\n maybeForms?: CasesFn<K, B>\n): Format<\n FormatsInput<B>,\n MatchFn<K, B>\n> {\n let options: Intl.PluralRulesOptions | undefined\n let forms: CasesFn<K, B> | undefined\n\n if (isFunction(optionsOrForms)) {\n forms = optionsOrForms\n } else {\n options = optionsOrForms\n forms = maybeForms\n }\n\n const rules = memo((locale: string) => new Intl.PluralRules(locale, options))\n\n return match(param, forms, (count, locale, forms) => {\n if (count in forms) {\n return count as string\n }\n\n let form = rules(locale).select(count as number)\n\n if (!(form in forms)) {\n form = 'other' in forms ? 'other' : 'many'\n }\n\n return form\n })\n}\n"]}
@@ -0,0 +1,36 @@
1
+ import type { IntlFormatFn } from './intlformat.js';
2
+ import type { Format, IntlFormatRange } from './types.js';
3
+ type RangeInput<I> = readonly [from: I, to: I];
4
+ type RangeFrom<F extends IntlFormatRange<I>, I, O, R> = (optionsOrFallback?: O | false | I, maybeOptions?: O | false, format?: IntlFormatFn<F, I, O>) => Format<I | undefined, R | undefined>;
5
+ /**
6
+ * Creates a range formatter in raw mode.
7
+ * @param format - Formatter factory that supports a custom range formatting function.
8
+ * @param options - Pass `false` to return the input range without formatting.
9
+ * @returns Formatter that returns the input range or `undefined`.
10
+ */
11
+ export declare function range<F extends IntlFormatRange<I>, I, O, R>(format: RangeFrom<F, I, O, R>, options: false): Format<RangeInput<I> | undefined>;
12
+ /**
13
+ * Creates a locale-aware range formatter.
14
+ * @param format - Formatter factory that supports a custom range formatting function.
15
+ * @param options - Formatter options.
16
+ * @returns Formatter that returns a formatted range string or `undefined`.
17
+ */
18
+ export declare function range<F extends IntlFormatRange<I>, I, O, R>(format: RangeFrom<F, I, O, R>, options?: O): Format<RangeInput<I> | undefined, string | undefined>;
19
+ /**
20
+ * Creates a range formatter in raw mode with a fallback range.
21
+ * @param format - Formatter factory that supports a custom range formatting function.
22
+ * @param fallback - Range returned when the input is `undefined` or `null`.
23
+ * @param options - Pass `false` to return the input range or fallback without formatting.
24
+ * @returns Formatter that returns the input range or fallback.
25
+ */
26
+ export declare function range<F extends IntlFormatRange<I>, I, O, R>(format: RangeFrom<F, I, O, R>, fallback: RangeInput<I>, options: false): Format<RangeInput<I> | undefined, RangeInput<I>>;
27
+ /**
28
+ * Creates a locale-aware range formatter with a fallback range.
29
+ * @param format - Formatter factory that supports a custom range formatting function.
30
+ * @param fallback - Range used when the input is `undefined` or `null`.
31
+ * @param options - Formatter options.
32
+ * @returns Formatter that returns a formatted range string or `undefined`.
33
+ */
34
+ export declare function range<F extends IntlFormatRange<I>, I, O, R>(format: RangeFrom<F, I, O, R>, fallback: RangeInput<I>, options?: O): Format<RangeInput<I> | undefined, string | undefined>;
35
+ export {};
36
+ //# sourceMappingURL=range.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"range.d.ts","sourceRoot":"","sources":["../../src/format/range.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AACnD,OAAO,KAAK,EACV,MAAM,EACN,eAAe,EAChB,MAAM,YAAY,CAAA;AAEnB,KAAK,UAAU,CAAC,CAAC,IAAI,SAAS,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAA;AAE9C,KAAK,SAAS,CAAC,CAAC,SAAS,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CACtD,iBAAiB,CAAC,EAAE,CAAC,GAAG,KAAK,GAAG,CAAC,EACjC,YAAY,CAAC,EAAE,CAAC,GAAG,KAAK,EACxB,MAAM,CAAC,EAAE,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,KAC3B,MAAM,CAAC,CAAC,GAAG,SAAS,EAAE,CAAC,GAAG,SAAS,CAAC,CAAA;AASzC;;;;;GAKG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EACzD,MAAM,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAC7B,OAAO,EAAE,KAAK,GACb,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAA;AAEpC;;;;;GAKG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EACzD,MAAM,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAC7B,OAAO,CAAC,EAAE,CAAC,GACV,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AAExD;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EACzD,MAAM,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAC7B,QAAQ,EAAE,UAAU,CAAC,CAAC,CAAC,EACvB,OAAO,EAAE,KAAK,GACb,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,SAAS,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAA;AAEnD;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EACzD,MAAM,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAC7B,QAAQ,EAAE,UAAU,CAAC,CAAC,CAAC,EACvB,OAAO,CAAC,EAAE,CAAC,GACV,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA","sourcesContent":["import type { IntlFormatFn } from './intlformat.js'\nimport type {\n Format,\n IntlFormatRange\n} from './types.js'\n\ntype RangeInput<I> = readonly [from: I, to: I]\n\ntype RangeFrom<F extends IntlFormatRange<I>, I, O, R> = (\n optionsOrFallback?: O | false | I,\n maybeOptions?: O | false,\n format?: IntlFormatFn<F, I, O>\n) => Format<I | undefined, R | undefined>\n\nfunction rangeFormat(\n fmt: IntlFormatRange<unknown>,\n [from, to]: RangeInput<unknown>\n) {\n return fmt.formatRange(from, to)\n}\n\n/**\n * Creates a range formatter in raw mode.\n * @param format - Formatter factory that supports a custom range formatting function.\n * @param options - Pass `false` to return the input range without formatting.\n * @returns Formatter that returns the input range or `undefined`.\n */\nexport function range<F extends IntlFormatRange<I>, I, O, R>(\n format: RangeFrom<F, I, O, R>,\n options: false\n): Format<RangeInput<I> | undefined>\n\n/**\n * Creates a locale-aware range formatter.\n * @param format - Formatter factory that supports a custom range formatting function.\n * @param options - Formatter options.\n * @returns Formatter that returns a formatted range string or `undefined`.\n */\nexport function range<F extends IntlFormatRange<I>, I, O, R>(\n format: RangeFrom<F, I, O, R>,\n options?: O\n): Format<RangeInput<I> | undefined, string | undefined>\n\n/**\n * Creates a range formatter in raw mode with a fallback range.\n * @param format - Formatter factory that supports a custom range formatting function.\n * @param fallback - Range returned when the input is `undefined` or `null`.\n * @param options - Pass `false` to return the input range or fallback without formatting.\n * @returns Formatter that returns the input range or fallback.\n */\nexport function range<F extends IntlFormatRange<I>, I, O, R>(\n format: RangeFrom<F, I, O, R>,\n fallback: RangeInput<I>,\n options: false\n): Format<RangeInput<I> | undefined, RangeInput<I>>\n\n/**\n * Creates a locale-aware range formatter with a fallback range.\n * @param format - Formatter factory that supports a custom range formatting function.\n * @param fallback - Range used when the input is `undefined` or `null`.\n * @param options - Formatter options.\n * @returns Formatter that returns a formatted range string or `undefined`.\n */\nexport function range<F extends IntlFormatRange<I>, I, O, R>(\n format: RangeFrom<F, I, O, R>,\n fallback: RangeInput<I>,\n options?: O\n): Format<RangeInput<I> | undefined, string | undefined>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function range<F extends IntlFormatRange<I>, I, O, R>(\n format: RangeFrom<F, I, O, R>,\n optionsOrFallback?: O | false | RangeInput<I>,\n maybeOptions?: O | false\n) {\n return format(\n optionsOrFallback as I,\n maybeOptions,\n rangeFormat as IntlFormatFn<F, I, O>\n )\n}\n"]}
@@ -0,0 +1,13 @@
1
+ import type { Format } from './types.js';
2
+ /**
3
+ * Creates a pass-through formatter.
4
+ * @returns Formatter that returns the input value or `undefined`.
5
+ */
6
+ export declare function raw<T>(): Format<T | undefined>;
7
+ /**
8
+ * Creates a pass-through formatter with a fallback value.
9
+ * @param fallback - Value returned when the input is `undefined` or `null`.
10
+ * @returns Formatter that returns the input value or fallback.
11
+ */
12
+ export declare function raw<T>(fallback: T): Format<T | undefined, T>;
13
+ //# sourceMappingURL=raw.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"raw.d.ts","sourceRoot":"","sources":["../../src/format/raw.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAExC;;;GAGG;AACH,wBAAgB,GAAG,CAAC,CAAC,KAAK,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC,CAAA;AAE/C;;;;GAIG;AACH,wBAAgB,GAAG,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,SAAS,EAAE,CAAC,CAAC,CAAA","sourcesContent":["import type { Format } from './types.js'\n\n/**\n * Creates a pass-through formatter.\n * @returns Formatter that returns the input value or `undefined`.\n */\nexport function raw<T>(): Format<T | undefined>\n\n/**\n * Creates a pass-through formatter with a fallback value.\n * @param fallback - Value returned when the input is `undefined` or `null`.\n * @returns Formatter that returns the input value or fallback.\n */\nexport function raw<T>(fallback: T): Format<T | undefined, T>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function raw<T>(fallback?: T) {\n return (_ctx: unknown, input?: T) => input ?? fallback\n}\n"]}
@@ -0,0 +1,31 @@
1
+ import type { Format } from './types.js';
2
+ export type RelativeTimeOptions = Intl.RelativeTimeFormatOptions & {
3
+ unit: Intl.RelativeTimeFormatUnit;
4
+ };
5
+ /**
6
+ * Creates a relative time formatter in raw mode.
7
+ * @param options - Pass `false` to return the input value without formatting.
8
+ * @returns Formatter that returns the input value or `undefined`.
9
+ */
10
+ export declare function relativetime(options: false): Format<number | undefined>;
11
+ /**
12
+ * Creates a locale-aware relative time formatter.
13
+ * @param options - `Intl.RelativeTimeFormat` options with a required `unit`.
14
+ * @returns Formatter that returns a formatted string or `undefined`.
15
+ */
16
+ export declare function relativetime(options?: RelativeTimeOptions): Format<number | undefined, string | undefined>;
17
+ /**
18
+ * Creates a relative time formatter in raw mode with a fallback value.
19
+ * @param fallback - Value returned when the input is `undefined` or `null`.
20
+ * @param options - Pass `false` to return the input value or fallback without formatting.
21
+ * @returns Formatter that returns the input value or fallback.
22
+ */
23
+ export declare function relativetime(fallback: number, options: false): Format<number | undefined, number>;
24
+ /**
25
+ * Creates a locale-aware relative time formatter with a fallback value.
26
+ * @param fallback - Value used when the input is `undefined` or `null`.
27
+ * @param options - `Intl.RelativeTimeFormat` options with a required `unit`.
28
+ * @returns Formatter that returns a formatted string or `undefined`.
29
+ */
30
+ export declare function relativetime(fallback: number, options?: RelativeTimeOptions): Format<number | undefined, string | undefined>;
31
+ //# sourceMappingURL=relativetime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"relativetime.d.ts","sourceRoot":"","sources":["../../src/format/relativetime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAGxC,MAAM,MAAM,mBAAmB,GAAG,IAAI,CAAC,yBAAyB,GAAG;IACjE,IAAI,EAAE,IAAI,CAAC,sBAAsB,CAAA;CAClC,CAAA;AAUD;;;;GAIG;AACH,wBAAgB,YAAY,CAC1B,OAAO,EAAE,KAAK,GACb,MAAM,CAAC,MAAM,GAAG,SAAS,CAAC,CAAA;AAE7B;;;;GAIG;AACH,wBAAgB,YAAY,CAC1B,OAAO,CAAC,EAAE,mBAAmB,GAC5B,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AAEjD;;;;;GAKG;AACH,wBAAgB,YAAY,CAC1B,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,KAAK,GACb,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,MAAM,CAAC,CAAA;AAErC;;;;;GAKG;AACH,wBAAgB,YAAY,CAC1B,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,mBAAmB,GAC5B,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA","sourcesContent":["import type { Format } from './types.js'\nimport { intlformat } from './intlformat.js'\n\nexport type RelativeTimeOptions = Intl.RelativeTimeFormatOptions & {\n unit: Intl.RelativeTimeFormatUnit\n}\n\nfunction relativeTimeFormat(\n fmt: Intl.RelativeTimeFormat,\n value: number,\n options: RelativeTimeOptions | undefined\n) {\n return fmt.format(value, options!.unit)\n}\n\n/**\n * Creates a relative time formatter in raw mode.\n * @param options - Pass `false` to return the input value without formatting.\n * @returns Formatter that returns the input value or `undefined`.\n */\nexport function relativetime(\n options: false\n): Format<number | undefined>\n\n/**\n * Creates a locale-aware relative time formatter.\n * @param options - `Intl.RelativeTimeFormat` options with a required `unit`.\n * @returns Formatter that returns a formatted string or `undefined`.\n */\nexport function relativetime(\n options?: RelativeTimeOptions\n): Format<number | undefined, string | undefined>\n\n/**\n * Creates a relative time formatter in raw mode with a fallback value.\n * @param fallback - Value returned when the input is `undefined` or `null`.\n * @param options - Pass `false` to return the input value or fallback without formatting.\n * @returns Formatter that returns the input value or fallback.\n */\nexport function relativetime(\n fallback: number,\n options: false\n): Format<number | undefined, number>\n\n/**\n * Creates a locale-aware relative time formatter with a fallback value.\n * @param fallback - Value used when the input is `undefined` or `null`.\n * @param options - `Intl.RelativeTimeFormat` options with a required `unit`.\n * @returns Formatter that returns a formatted string or `undefined`.\n */\nexport function relativetime(\n fallback: number,\n options?: RelativeTimeOptions\n): Format<number | undefined, string | undefined>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function relativetime(\n optionsOrFallback?: RelativeTimeOptions | false | number,\n maybeOptions?: RelativeTimeOptions | false\n) {\n return intlformat(\n Intl.RelativeTimeFormat,\n relativeTimeFormat,\n optionsOrFallback,\n maybeOptions\n )\n}\n"]}
@@ -0,0 +1,33 @@
1
+ import type { Format } from './types.js';
2
+ export type RichChunks<T> = (string | T)[];
3
+ export type RichTag<T> = (chunks: RichChunks<T>) => T;
4
+ export type RichTags<T> = Record<string, RichTag<T>>;
5
+ /**
6
+ * Creates a rich-text formatter.
7
+ * @param tags - Tag handlers used to map rich-text tags to output chunks.
8
+ * @returns Formatter that returns mapped chunks or `undefined`.
9
+ */
10
+ export declare function rich<T>(tags: RichTags<T>): Format<string | undefined, RichChunks<T> | undefined>;
11
+ /**
12
+ * Creates a rich-text formatter with a fallback message.
13
+ * @param fallback - Message used when the input is `undefined` or `null`.
14
+ * @param tags - Tag handlers used to map rich-text tags to output chunks.
15
+ * @returns Formatter that returns mapped chunks.
16
+ */
17
+ export declare function rich<T>(fallback: string, tags: RichTags<T>): Format<string | undefined, RichChunks<T>>;
18
+ /**
19
+ * Creates a rich-text formatter.
20
+ * @param fallbackOrTags - Fallback message or tag handlers.
21
+ * @param maybeTags - Tag handlers used when fallback is provided.
22
+ * @returns Formatter that returns mapped chunks or `undefined`.
23
+ */
24
+ export declare function rich<T>(fallbackOrTags: string | RichTags<T> | undefined, maybeTags?: RichTags<T>): Format<string | undefined, RichChunks<T> | undefined>;
25
+ /**
26
+ * Maps rich-text tags in a string to chunks using provided tag handlers.
27
+ * Unknown or malformed tags are ignored as markup and only their text content is kept.
28
+ * @param input - Rich-text input string.
29
+ * @param tags - Tag handlers used to map known tags.
30
+ * @returns Mapped chunks.
31
+ */
32
+ export declare function mapTags<T>(input: string, tags: RichTags<T>): RichChunks<T>;
33
+ //# sourceMappingURL=rich.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rich.d.ts","sourceRoot":"","sources":["../../src/format/rich.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAExC,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,CAAA;AAE1C,MAAM,MAAM,OAAO,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAA;AAErD,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAA;AAEpD;;;;GAIG;AACH,wBAAgB,IAAI,CAAC,CAAC,EACpB,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,GAChB,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAA;AAExD;;;;;GAKG;AACH,wBAAgB,IAAI,CAAC,CAAC,EACpB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,GAChB,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAA;AAE5C;;;;;GAKG;AACH,wBAAgB,IAAI,CAAC,CAAC,EACpB,cAAc,EAAE,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,SAAS,EAChD,SAAS,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,GACtB,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAA;AAmCxD;;;;;;GAMG;AAEH,wBAAgB,OAAO,CAAC,CAAC,EACvB,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,GAChB,UAAU,CAAC,CAAC,CAAC,CA+Cf","sourcesContent":["import type { Format } from './types.js'\n\nexport type RichChunks<T> = (string | T)[]\n\nexport type RichTag<T> = (chunks: RichChunks<T>) => T\n\nexport type RichTags<T> = Record<string, RichTag<T>>\n\n/**\n * Creates a rich-text formatter.\n * @param tags - Tag handlers used to map rich-text tags to output chunks.\n * @returns Formatter that returns mapped chunks or `undefined`.\n */\nexport function rich<T>(\n tags: RichTags<T>\n): Format<string | undefined, RichChunks<T> | undefined>\n\n/**\n * Creates a rich-text formatter with a fallback message.\n * @param fallback - Message used when the input is `undefined` or `null`.\n * @param tags - Tag handlers used to map rich-text tags to output chunks.\n * @returns Formatter that returns mapped chunks.\n */\nexport function rich<T>(\n fallback: string,\n tags: RichTags<T>\n): Format<string | undefined, RichChunks<T>>\n\n/**\n * Creates a rich-text formatter.\n * @param fallbackOrTags - Fallback message or tag handlers.\n * @param maybeTags - Tag handlers used when fallback is provided.\n * @returns Formatter that returns mapped chunks or `undefined`.\n */\nexport function rich<T>(\n fallbackOrTags: string | RichTags<T> | undefined,\n maybeTags?: RichTags<T>\n): Format<string | undefined, RichChunks<T> | undefined>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function rich<T>(\n fallbackOrTags: string | RichTags<T> | undefined,\n maybeTags?: RichTags<T>\n): Format<string | undefined, RichChunks<T> | undefined> {\n let fallback: string | undefined\n let tags: RichTags<T>\n\n if (maybeTags) {\n fallback = fallbackOrTags as string\n tags = maybeTags\n } else {\n tags = fallbackOrTags as RichTags<T>\n }\n\n return (_ctx: unknown, input?: string) => {\n const text = input ?? fallback\n\n if (text === undefined) {\n return text\n }\n\n return mapTags(text, tags)\n }\n}\n\nconst tagRegex = /<\\/?([A-Za-z][\\w.-]*)>/g\n\ninterface RichFrame<T> {\n tag: string\n chunks: RichChunks<T>\n}\n\n/**\n * Maps rich-text tags in a string to chunks using provided tag handlers.\n * Unknown or malformed tags are ignored as markup and only their text content is kept.\n * @param input - Rich-text input string.\n * @param tags - Tag handlers used to map known tags.\n * @returns Mapped chunks.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function mapTags<T>(\n input: string,\n tags: RichTags<T>\n): RichChunks<T> {\n const root: RichFrame<T> = {\n tag: '',\n chunks: []\n }\n const stack = [root]\n let lastIndex = 0\n let match: RegExpExecArray | null\n\n while (match = tagRegex.exec(input)) {\n const [rawTag, tag] = match\n const current = stack[stack.length - 1]\n\n if (match.index > lastIndex) {\n current.chunks.push(input.slice(lastIndex, match.index))\n }\n\n if (tag in tags) {\n if (rawTag[1] === '/') {\n if (current.tag === tag) {\n stack.pop()\n stack[stack.length - 1].chunks.push(\n tags[tag](current.chunks)\n )\n }\n } else {\n stack.push({\n tag,\n chunks: []\n })\n }\n }\n\n lastIndex = tagRegex.lastIndex\n }\n\n if (lastIndex < input.length) {\n stack[stack.length - 1].chunks.push(input.slice(lastIndex))\n }\n\n for (let i = stack.length - 1; i > 0; i--) {\n const frame = stack[i]\n\n stack[i - 1].chunks.push(...frame.chunks)\n }\n\n return root.chunks\n}\n"]}
@@ -0,0 +1,34 @@
1
+ import type { Format } from './types.js';
2
+ /**
3
+ * Creates a string formatter.
4
+ * @returns Formatter that returns the input string or `undefined`.
5
+ */
6
+ export declare function text(): Format<string | undefined>;
7
+ /**
8
+ * Creates a string formatter with a fallback value.
9
+ * @param fallback - String returned when the input is `undefined` or `null`.
10
+ * @returns Formatter that returns the input string or fallback.
11
+ */
12
+ export declare function text(fallback: string): Format<string | undefined, string>;
13
+ /**
14
+ * Creates a formatter that uppercases another text formatter output.
15
+ * @param format - Text formatter to transform.
16
+ * @returns Formatter with uppercased output.
17
+ */
18
+ export declare function uppercase<T>(format: Format<T, string | undefined>): Format<T, string | undefined>;
19
+ export declare function uppercase<T>(format: Format<T, string>): Format<T, string>;
20
+ /**
21
+ * Creates a formatter that lowercases another text formatter output.
22
+ * @param format - Text formatter to transform.
23
+ * @returns Formatter with lowercased output.
24
+ */
25
+ export declare function lowercase<T>(format: Format<T, string | undefined>): Format<T, string | undefined>;
26
+ export declare function lowercase<T>(format: Format<T, string>): Format<T, string>;
27
+ /**
28
+ * Creates a formatter that capitalizes another text formatter output.
29
+ * @param format - Text formatter to transform.
30
+ * @returns Formatter with the first character uppercased.
31
+ */
32
+ export declare function capitalize<T>(format: Format<T, string | undefined>): Format<T, string | undefined>;
33
+ export declare function capitalize<T>(format: Format<T, string>): Format<T, string>;
34
+ //# sourceMappingURL=text.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text.d.ts","sourceRoot":"","sources":["../../src/format/text.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAGxC;;;GAGG;AACH,wBAAgB,IAAI,IAAI,MAAM,CAAC,MAAM,GAAG,SAAS,CAAC,CAAA;AAElD;;;;GAIG;AACH,wBAAgB,IAAI,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,MAAM,CAAC,CAAA;AAO1E;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EACzB,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,GACpC,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AAEhC,wBAAgB,SAAS,CAAC,CAAC,EACzB,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,GACxB,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,CAAA;AASpB;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EACzB,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,GACpC,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AAEhC,wBAAgB,SAAS,CAAC,CAAC,EACzB,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,GACxB,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,CAAA;AAepB;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAC1B,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,GACpC,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AAEhC,wBAAgB,UAAU,CAAC,CAAC,EAC1B,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,GACxB,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,CAAA","sourcesContent":["import type { Format } from './types.js'\nimport { raw } from './raw.js'\n\n/**\n * Creates a string formatter.\n * @returns Formatter that returns the input string or `undefined`.\n */\nexport function text(): Format<string | undefined>\n\n/**\n * Creates a string formatter with a fallback value.\n * @param fallback - String returned when the input is `undefined` or `null`.\n * @returns Formatter that returns the input string or fallback.\n */\nexport function text(fallback: string): Format<string | undefined, string>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function text(fallback?: string): Format<string | undefined> {\n return raw(fallback)\n}\n\n/**\n * Creates a formatter that uppercases another text formatter output.\n * @param format - Text formatter to transform.\n * @returns Formatter with uppercased output.\n */\nexport function uppercase<T>(\n format: Format<T, string | undefined>\n): Format<T, string | undefined>\n\nexport function uppercase<T>(\n format: Format<T, string>\n): Format<T, string>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function uppercase<T>(\n format: Format<T, string | undefined>\n): Format<T, string | undefined> {\n return (ctx, input) => format(ctx, input)?.toLocaleUpperCase(ctx.$locale())\n}\n\n/**\n * Creates a formatter that lowercases another text formatter output.\n * @param format - Text formatter to transform.\n * @returns Formatter with lowercased output.\n */\nexport function lowercase<T>(\n format: Format<T, string | undefined>\n): Format<T, string | undefined>\n\nexport function lowercase<T>(\n format: Format<T, string>\n): Format<T, string>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function lowercase<T>(\n format: Format<T, string | undefined>\n): Format<T, string | undefined> {\n return (ctx, input) => format(ctx, input)?.toLocaleLowerCase(ctx.$locale())\n}\n\nfunction toCapitalize(message: string | undefined, locale: string) {\n return message === undefined\n ? message\n : message.charAt(0).toLocaleUpperCase(locale) + message.slice(1)\n}\n\n/**\n * Creates a formatter that capitalizes another text formatter output.\n * @param format - Text formatter to transform.\n * @returns Formatter with the first character uppercased.\n */\nexport function capitalize<T>(\n format: Format<T, string | undefined>\n): Format<T, string | undefined>\n\nexport function capitalize<T>(\n format: Format<T, string>\n): Format<T, string>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function capitalize<T>(\n format: Format<T, string | undefined>\n): Format<T, string | undefined> {\n return (ctx, input) => toCapitalize(format(ctx, input), ctx.$locale())\n}\n"]}
@@ -0,0 +1,21 @@
1
+ import type { AnyFn } from '@nano_kit/store';
2
+ import type { FormatContext, UnionToIntersection } from '../types.js';
3
+ export type { UnionToIntersection };
4
+ type ObjectParams<T> = T extends object ? T extends AnyFn ? never : T : never;
5
+ export type ParamsOf<T> = T extends (params: infer P) => unknown ? ObjectParams<P> : {};
6
+ export type Format<I, O = I> = (ctx: FormatContext, input?: I) => O;
7
+ export type AnyFormat<O = unknown> = Format<any, O>;
8
+ export type FormatInput<F> = F extends Format<infer I, unknown> ? I : never;
9
+ export type FormatOutput<F> = F extends Format<never, infer O> ? O : never;
10
+ export type FormatsInput<T extends Partial<Record<PropertyKey, AnyFormat>>> = Partial<{
11
+ [K in keyof T]: FormatInput<T[K]>;
12
+ }>;
13
+ export type FnWithParams<P> = (params: P) => string | undefined;
14
+ export type FnWithAnyParams = FnWithParams<any>;
15
+ export interface IntlFormat<T> {
16
+ format(value: T, ...args: unknown[]): string;
17
+ }
18
+ export interface IntlFormatRange<T> extends IntlFormat<T> {
19
+ formatRange(from: T, to: T): string;
20
+ }
21
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/format/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;AAC5C,OAAO,KAAK,EACV,aAAa,EACb,mBAAmB,EACpB,MAAM,aAAa,CAAA;AAEpB,YAAY,EAAE,mBAAmB,EAAE,CAAA;AAEnC,KAAK,YAAY,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,GACnC,CAAC,SAAS,KAAK,GACb,KAAK,GACL,CAAC,GACH,KAAK,CAAA;AAET,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,OAAO,GAAG,YAAY,CAAC,CAAC,CAAC,GAAG,EAAE,CAAA;AAEvF,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,EAAE,aAAa,EAAE,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,CAAA;AAEnE,MAAM,MAAM,SAAS,CAAC,CAAC,GAAG,OAAO,IAAI,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;AAEnD,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;AAE3E,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;AAE1E,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC,IAAI,OAAO,CAAC;KACnF,CAAC,IAAI,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAClC,CAAC,CAAA;AAEF,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,KAAK,MAAM,GAAG,SAAS,CAAA;AAE/D,MAAM,MAAM,eAAe,GAAG,YAAY,CAAC,GAAG,CAAC,CAAA;AAE/C,MAAM,WAAW,UAAU,CAAC,CAAC;IAC3B,MAAM,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,CAAA;CAC7C;AAED,MAAM,WAAW,eAAe,CAAC,CAAC,CAAE,SAAQ,UAAU,CAAC,CAAC,CAAC;IACvD,WAAW,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,MAAM,CAAA;CACpC","sourcesContent":["import type { AnyFn } from '@nano_kit/store'\nimport type {\n FormatContext,\n UnionToIntersection\n} from '../types.js'\n\nexport type { UnionToIntersection }\n\ntype ObjectParams<T> = T extends object\n ? T extends AnyFn\n ? never\n : T\n : never\n\nexport type ParamsOf<T> = T extends (params: infer P) => unknown ? ObjectParams<P> : {}\n\nexport type Format<I, O = I> = (ctx: FormatContext, input?: I) => O\n\nexport type AnyFormat<O = unknown> = Format<any, O>\n\nexport type FormatInput<F> = F extends Format<infer I, unknown> ? I : never\n\nexport type FormatOutput<F> = F extends Format<never, infer O> ? O : never\n\nexport type FormatsInput<T extends Partial<Record<PropertyKey, AnyFormat>>> = Partial<{\n [K in keyof T]: FormatInput<T[K]>\n}>\n\nexport type FnWithParams<P> = (params: P) => string | undefined\n\nexport type FnWithAnyParams = FnWithParams<any>\n\nexport interface IntlFormat<T> {\n format(value: T, ...args: unknown[]): string\n}\n\nexport interface IntlFormatRange<T> extends IntlFormat<T> {\n formatRange(from: T, to: T): string\n}\n"]}
@@ -0,0 +1,7 @@
1
+ export type * from './types.js';
2
+ export * from './format/index.js';
3
+ export * from './context.js';
4
+ export * from './deflat.js';
5
+ export * from './intl.js';
6
+ export * from './direction.js';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mBAAmB,YAAY,CAAA;AAC/B,cAAc,mBAAmB,CAAA;AACjC,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,gBAAgB,CAAA","sourcesContent":["export type * from './types.js'\nexport * from './format/index.js'\nexport * from './context.js'\nexport * from './deflat.js'\nexport * from './intl.js'\nexport * from './direction.js'\n"]}