@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
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 - present, TrigenSoftware
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # @nano_kit/intl
2
+
3
+ [![ESM-only package][package]][package-url]
4
+ [![NPM version][npm]][npm-url]
5
+ [![Dependencies status][deps]][deps-url]
6
+ [![Install size][size]][size-url]
7
+ [![Build status][build]][build-url]
8
+ [![Coverage status][coverage]][coverage-url]
9
+
10
+ [package]: https://img.shields.io/badge/package-ESM--only-ffe536.svg
11
+ [package-url]: https://nodejs.org/api/esm.html
12
+
13
+ [npm]: https://img.shields.io/npm/v/@nano_kit/intl.svg
14
+ [npm-url]: https://npmjs.com/package/@nano_kit/intl
15
+
16
+ [deps]: https://img.shields.io/librariesio/release/npm/@nano_kit/intl
17
+ [deps-url]: https://libraries.io/npm/@nano_kit%2Fintl
18
+
19
+ [size]: https://deno.bundlejs.com/badge?q=@nano_kit/intl
20
+ [size-url]: https://bundlejs.com/?q=@nano_kit/intl
21
+
22
+ [build]: https://img.shields.io/github/actions/workflow/status/TrigenSoftware/nano_kit/tests.yml?branch=main
23
+ [build-url]: https://github.com/TrigenSoftware/nano_kit/actions
24
+
25
+ [coverage]: https://img.shields.io/codecov/c/github/TrigenSoftware/nano_kit.svg
26
+ [coverage-url]: https://app.codecov.io/gh/TrigenSoftware/nano_kit
27
+
28
+ A lightweight internationalization library for Nano Kit applications.
29
+
30
+ - **Reactive**. Locale changes update messages through `@nano_kit/store` signals.
31
+ - **Type-safe**. Message schemes are checked against translation data.
32
+ - **Composable**. Build messages from small formats like `text`, `params`, `plural`, `match`, and `number`.
33
+ - **Tree-shakeable**. Import only the formats your app uses.
34
+
35
+ ## Installation
36
+
37
+ ```bash
38
+ pnpm add @nano_kit/store @nano_kit/intl
39
+ # or
40
+ npm install @nano_kit/store @nano_kit/intl
41
+ # or
42
+ yarn add @nano_kit/store @nano_kit/intl
43
+ ```
44
+
45
+ ## Quick Start
46
+
47
+ Here is a minimal example with a reactive locale and typed messages:
48
+
49
+ ```ts
50
+ import {
51
+ resolved,
52
+ signal
53
+ } from '@nano_kit/store'
54
+ import {
55
+ intl,
56
+ params,
57
+ plural,
58
+ text
59
+ } from '@nano_kit/intl'
60
+
61
+ const $locale = signal<'en' | 'de'>('en')
62
+
63
+ const { messages } = intl(
64
+ $locale,
65
+ resolved(() => fetch(`/locales/${$locale()}.json`).then(res => res.json()))
66
+ )
67
+
68
+ const [$t, $loading] = messages('common', {
69
+ title: text(),
70
+ greeting: params({
71
+ name: text()
72
+ }),
73
+ unread: plural('count')
74
+ })
75
+
76
+ /* Text message */
77
+ $t().title
78
+
79
+ /* Parameterized message */
80
+ $t().greeting({
81
+ name: 'Ada'
82
+ })
83
+
84
+ /* Pluralized message */
85
+ $t().unread({
86
+ count: 5
87
+ })
88
+
89
+ /* Switching locale */
90
+ $locale('de')
91
+ ```
92
+
93
+ ## Documentation
94
+
95
+ For comprehensive guides, advanced patterns, and API reference, visit the [documentation website](https://nano-kit.js.org/intl).
@@ -0,0 +1,38 @@
1
+ import { type Accessor, type ReadableSignal } from '@nano_kit/store';
2
+ import type { TranslationData, Loader, MessagesScheme, AnyTranslationData, MessagesAccessor } from './types.js';
3
+ /**
4
+ * Runtime context used by message formats.
5
+ *
6
+ * It owns the active locale, loads translation data, and exposes messages helpers
7
+ * that bind a messages scheme to the current namespace data.
8
+ * @template T - Translation data shape keyed by namespace name.
9
+ */
10
+ export declare class IntlContext<L extends string, T extends TranslationData<T> = AnyTranslationData> {
11
+ #private;
12
+ /**
13
+ * Active locale accessor.
14
+ */
15
+ readonly $locale: Accessor<L>;
16
+ /**
17
+ * Loading state for full-data loaders.
18
+ */
19
+ readonly $loading: ReadableSignal<boolean>;
20
+ /**
21
+ * Loading error for full-data loaders.
22
+ */
23
+ readonly $error: ReadableSignal<unknown>;
24
+ /**
25
+ * Creates an internationalization context.
26
+ * @param $locale - Active locale accessor.
27
+ * @param loader - Messages loader bound to the active locale.
28
+ */
29
+ constructor($locale: Accessor<L>, loader: Loader<T>);
30
+ /**
31
+ * Creates reactive messages for a single namespace.
32
+ * @param namespace - Namespace key to read from loaded translation data.
33
+ * @param scheme - Messages scheme compatible with the namespace data shape.
34
+ * @returns A tuple with formatted messages, pending state, and loading error.
35
+ */
36
+ messages<K extends keyof T, S extends MessagesScheme<T[K]> = {}>(namespace: K, scheme?: S): readonly [MessagesAccessor<T[K], S>, ReadableSignal<boolean>, ReadableSignal<unknown>];
37
+ }
38
+ //# sourceMappingURL=context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,cAAc,EAMpB,MAAM,iBAAiB,CAAA;AACxB,OAAO,KAAK,EACV,eAAe,EACf,MAAM,EACN,cAAc,EAGd,kBAAkB,EAClB,gBAAgB,EACjB,MAAM,YAAY,CAAA;AAwBnB;;;;;;GAMG;AACH,qBAAa,WAAW,CACtB,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,eAAe,CAAC,CAAC,CAAC,GAAG,kBAAkB;;IAGjD;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAA;IAC7B;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,cAAc,CAAC,OAAO,CAAC,CAAA;IAC1C;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC,OAAO,CAAC,CAAA;IAExC;;;;OAIG;IACH,YACE,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,EACpB,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EAWlB;IAED;;;;;OAKG;IACH,QAAQ,CAAC,CAAC,SAAS,MAAM,CAAC,EAAE,CAAC,SAAS,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,EAC7D,SAAS,EAAE,CAAC,EACZ,MAAM,GAAE,CAAW,aAoCqB,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,oDAIlE;CACF","sourcesContent":["import {\n type Accessor,\n type ReadableSignal,\n type EmptyValue,\n type AnySignal,\n isFunction,\n signal,\n computed\n} from '@nano_kit/store'\nimport type {\n TranslationData,\n Loader,\n MessagesScheme,\n Messages,\n AnyNamespaceTranslationData,\n AnyTranslationData,\n MessagesAccessor\n} from './types.js'\n\nconst proxyHandler: ProxyHandler<AnySignal & Record<string, unknown>> = {\n /* eslint-disable */\n get($signal, key: string, receiver) {\n if (!(key in $signal)) {\n if (key[0] === '$') {\n const prop = key.slice(1)\n\n return $signal[key] = computed(() => $signal()[prop])\n } else {\n const $prop = receiver[`$${key}`]\n\n return $signal[key] = (params: unknown) => computed(\n () => $prop()(params)\n )\n }\n }\n\n return $signal[key]\n }\n /* eslint-enable */\n}\n\n/**\n * Runtime context used by message formats.\n *\n * It owns the active locale, loads translation data, and exposes messages helpers\n * that bind a messages scheme to the current namespace data.\n * @template T - Translation data shape keyed by namespace name.\n */\nexport class IntlContext<\n L extends string,\n T extends TranslationData<T> = AnyTranslationData\n> {\n readonly #loader: Loader<T>\n /**\n * Active locale accessor.\n */\n readonly $locale: Accessor<L>\n /**\n * Loading state for full-data loaders.\n */\n readonly $loading: ReadableSignal<boolean>\n /**\n * Loading error for full-data loaders.\n */\n readonly $error: ReadableSignal<unknown>\n\n /**\n * Creates an internationalization context.\n * @param $locale - Active locale accessor.\n * @param loader - Messages loader bound to the active locale.\n */\n constructor(\n $locale: Accessor<L>,\n loader: Loader<T>\n ) {\n this.$locale = $locale\n this.#loader = loader\n\n if (isFunction(loader)) {\n this.$loading = signal(false)\n this.$error = signal(undefined)\n } else {\n [, this.$error, this.$loading] = loader\n }\n }\n\n /**\n * Creates reactive messages for a single namespace.\n * @param namespace - Namespace key to read from loaded translation data.\n * @param scheme - Messages scheme compatible with the namespace data shape.\n * @returns A tuple with formatted messages, pending state, and loading error.\n */\n messages<K extends keyof T, S extends MessagesScheme<T[K]> = {}>(\n namespace: K,\n scheme: S = {} as S\n ) {\n const entries = Object.entries(scheme)\n const loaderResult = this.#loader\n let $data: ReadableSignal<T[K] | EmptyValue>\n let $error: ReadableSignal<unknown>\n let $pending: ReadableSignal<boolean>\n\n if (isFunction(loaderResult)) {\n [$data, $error, $pending] = loaderResult(namespace, entries)\n } else {\n let $translations\n\n [$translations, $error, $pending] = loaderResult\n\n $data = computed(() => $translations()?.[namespace])\n }\n\n const $messages = computed(() => {\n const data: AnyNamespaceTranslationData | EmptyValue = $data()\n let result: Record<string, unknown> = data ?? {}\n\n if (entries.length > 0) {\n result = {\n ...result\n }\n\n for (const [key, format] of entries) {\n result[key] = format(this, data?.[key])\n }\n }\n\n return result as Messages<T[K], S>\n })\n\n return [\n new Proxy($messages, proxyHandler) as MessagesAccessor<T[K], S>,\n $pending,\n $error\n ] as const\n }\n}\n"]}
@@ -0,0 +1,49 @@
1
+ import type { UnionToIntersection } from './types.js';
2
+ type Split<T extends string, S extends string = '.'> = string extends T ? [T] : T extends `${infer H}${S}${infer R}` ? [H, ...Split<R, S>] : [T];
3
+ type TupleOf<N extends number, T extends unknown[] = []> = T['length'] extends N ? T : TupleOf<N, [...T, unknown]>;
4
+ type Add<A extends unknown[], B extends unknown[]> = [...A, ...B];
5
+ type IsAtDepth<L extends unknown[], D extends number> = L extends [...TupleOf<D>, ...unknown[]] ? true : false;
6
+ type Simplify<T> = T extends (...args: unknown[]) => unknown ? T : T extends object ? {
7
+ [K in keyof T]: Simplify<T[K]>;
8
+ } : T;
9
+ type PathObject<P extends readonly string[], V> = P extends readonly [infer H extends string, ...infer R extends string[]] ? {
10
+ [K in H]: R extends [] ? V : PathObject<R, V>;
11
+ } : V;
12
+ interface DeflatOpenTree<V> {
13
+ [key: string]: V | DeflatOpenTree<V>;
14
+ }
15
+ type DeflatOpenRecord<T extends Record<string, unknown>, D extends number, L extends unknown[]> = IsAtDepth<L, D> extends true ? DeflatOpenTree<DeflatValue<T[string], D, [...L, unknown]>> : Record<string, DeflatValue<T[string], D, [...L, unknown]>>;
16
+ type DeflatValue<T, D extends number, L extends unknown[]> = T extends Record<string, unknown> ? string extends keyof T ? DeflatOpenRecord<T, D, L> : DeflatAt<T, D, L> : T;
17
+ type DeflatEntry<K extends string, V, D extends number, L extends unknown[]> = IsAtDepth<L, D> extends true ? PathObject<Split<K>, DeflatValue<V, D, Add<L, TupleOf<Split<K>['length']>>>> : {
18
+ [P in K]: DeflatValue<V, D, [...L, unknown]>;
19
+ };
20
+ type DeflatAt<T extends Record<string, unknown>, D extends number, L extends unknown[]> = Simplify<UnionToIntersection<{
21
+ [K in keyof T & string]: DeflatEntry<K, T[K], D, L>;
22
+ }[keyof T & string]>>;
23
+ export type Deflat<T extends Record<string, unknown>, D extends number = 0> = string extends keyof T ? DeflatOpenRecord<T, D, []> : DeflatAt<T, D, []>;
24
+ export type DeflatFromDepth<T extends Record<string, unknown>> = Simplify<{
25
+ [K in keyof T]: DeflatValue<T[K], 0, []>;
26
+ }>;
27
+ /**
28
+ * Deflates dotted object keys into nested objects.
29
+ * @param input - Object with dotted keys.
30
+ * @param depth - Nesting level where dotted keys should start being deflated.
31
+ * @returns Nested object built from dotted keys.
32
+ */
33
+ export declare function deflat<const T extends Record<string, unknown>>(input: T): Deflat<T>;
34
+ /**
35
+ * Deflates dotted object keys into nested objects starting from a depth.
36
+ * @param input - Object with dotted keys.
37
+ * @param depth - Nesting level where dotted keys should start being deflated.
38
+ * @returns Nested object built from dotted keys.
39
+ */
40
+ export declare function deflat<const T extends Record<string, unknown>, const D extends number>(input: T, depth: D): Deflat<T, D>;
41
+ /**
42
+ * Deflates dotted object keys into nested objects starting from a depth.
43
+ * @param input - Object with dotted keys.
44
+ * @param depth - Nesting level where dotted keys should start being deflated.
45
+ * @returns Nested object built from dotted keys.
46
+ */
47
+ export declare function deflat<const T extends Record<string, unknown>>(input: T, depth: number): DeflatFromDepth<T>;
48
+ export {};
49
+ //# sourceMappingURL=deflat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deflat.d.ts","sourceRoot":"","sources":["../src/deflat.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAErD,KAAK,KAAK,CACR,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,MAAM,GAAG,GAAG,I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,MAAM,EAAE,OAAO,CAAC,EACvC,KAAK,CAAC,CAAC,SAAS,MAAM,EAEtB,KAAK,EAAE,CAAC,EACR,KAAK,EAAE,CAAC,GACP,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;AAEf;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,KAAK,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5D,KAAK,EAAE,CAAC,EACR,KAAK,EAAE,MAAM,GACZ,eAAe,CAAC,CAAC,CAAC,CAAA","sourcesContent":["import type { UnionToIntersection } from './types.js'\n\ntype Split<\n T extends string,\n S extends string = '.'\n> = string extends T\n ? [T]\n : T extends `${infer H}${S}${infer R}`\n ? [H, ...Split<R, S>]\n : [T]\n\ntype TupleOf<N extends number, T extends unknown[] = []> = T['length'] extends N\n ? T\n : TupleOf<N, [...T, unknown]>\n\ntype Add<A extends unknown[], B extends unknown[]> = [...A, ...B]\n\ntype IsAtDepth<\n L extends unknown[],\n D extends number\n> = L extends [...TupleOf<D>, ...unknown[]] ? true : false\n\ntype Simplify<T> = T extends (...args: unknown[]) => unknown\n ? T\n : T extends object\n ? {\n [K in keyof T]: Simplify<T[K]>\n }\n : T\n\ntype PathObject<\n P extends readonly string[],\n V\n> = P extends readonly [infer H extends string, ...infer R extends string[]]\n ? {\n [K in H]: R extends []\n ? V\n : PathObject<R, V>\n }\n : V\n\ninterface DeflatOpenTree<V> {\n [key: string]: V | DeflatOpenTree<V>\n}\n\ntype DeflatOpenRecord<\n T extends Record<string, unknown>,\n D extends number,\n L extends unknown[]\n> = IsAtDepth<L, D> extends true\n ? DeflatOpenTree<DeflatValue<T[string], D, [...L, unknown]>>\n : Record<string, DeflatValue<T[string], D, [...L, unknown]>>\n\ntype DeflatValue<\n T,\n D extends number,\n L extends unknown[]\n> = T extends Record<string, unknown>\n ? string extends keyof T\n ? DeflatOpenRecord<T, D, L>\n : DeflatAt<T, D, L>\n : T\n\ntype DeflatEntry<\n K extends string,\n V,\n D extends number,\n L extends unknown[]\n> = IsAtDepth<L, D> extends true\n ? PathObject<Split<K>, DeflatValue<V, D, Add<L, TupleOf<Split<K>['length']>>>>\n : {\n [P in K]: DeflatValue<V, D, [...L, unknown]>\n }\n\ntype DeflatAt<\n T extends Record<string, unknown>,\n D extends number,\n L extends unknown[]\n> = Simplify<UnionToIntersection<{\n [K in keyof T & string]: DeflatEntry<K, T[K], D, L>\n}[keyof T & string]>>\n\nexport type Deflat<\n T extends Record<string, unknown>,\n D extends number = 0\n> = string extends keyof T\n ? DeflatOpenRecord<T, D, []>\n : DeflatAt<T, D, []>\n\nexport type DeflatFromDepth<T extends Record<string, unknown>> = Simplify<{\n [K in keyof T]: DeflatValue<T[K], 0, []>\n}>\n\nfunction isObject(value: unknown): value is Record<string, unknown> {\n return value !== null\n && typeof value === 'object'\n && Object.getPrototypeOf(value) === Object.prototype\n}\n\nfunction ensure(\n target: Record<string, unknown>,\n path: string[],\n offset = 0\n) {\n let container = target\n\n for (let i = 0, len = path.length - offset, key; i < len; i++) {\n key = path[i]\n\n if (!isObject(container[key])) {\n container[key] = {}\n }\n\n container = container[key] as Record<string, unknown>\n }\n\n return container\n}\n\nfunction set(\n target: Record<string, unknown>,\n path: string[],\n value: unknown\n) {\n const key = path[path.length - 1]\n const container = ensure(target, path, 1)\n\n container[key] = value\n}\n\nfunction assign(\n target: Record<string, unknown>,\n source: Record<string, unknown>,\n depth: number,\n level: number\n) {\n for (const [key, value] of Object.entries(source)) {\n const path = level >= depth ? key.split('.') : [key]\n\n if (isObject(value)) {\n assign(\n ensure(target, path),\n value,\n depth,\n level + path.length\n )\n } else {\n set(target, path, value)\n }\n }\n}\n\n/**\n * Deflates dotted object keys into nested objects.\n * @param input - Object with dotted keys.\n * @param depth - Nesting level where dotted keys should start being deflated.\n * @returns Nested object built from dotted keys.\n */\nexport function deflat<const T extends Record<string, unknown>>(\n input: T\n): Deflat<T>\n\n/**\n * Deflates dotted object keys into nested objects starting from a depth.\n * @param input - Object with dotted keys.\n * @param depth - Nesting level where dotted keys should start being deflated.\n * @returns Nested object built from dotted keys.\n */\nexport function deflat<\n const T extends Record<string, unknown>,\n const D extends number\n>(\n input: T,\n depth: D\n): Deflat<T, D>\n\n/**\n * Deflates dotted object keys into nested objects starting from a depth.\n * @param input - Object with dotted keys.\n * @param depth - Nesting level where dotted keys should start being deflated.\n * @returns Nested object built from dotted keys.\n */\nexport function deflat<const T extends Record<string, unknown>>(\n input: T,\n depth: number\n): DeflatFromDepth<T>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function deflat(\n input: Record<string, unknown>,\n depth = 0\n) {\n const output: Record<string, unknown> = {}\n\n assign(output, input, depth, 0)\n\n return output\n}\n"]}
@@ -0,0 +1,15 @@
1
+ import { type Accessor } from '@nano_kit/store';
2
+ export declare const rtlLocales: Set<string>;
3
+ /**
4
+ * Resolves text direction for a locale.
5
+ * @param locale - BCP 47 locale code.
6
+ * @returns `rtl` for right-to-left languages, otherwise `ltr`.
7
+ */
8
+ export declare function getDirection(locale: string): "ltr" | "rtl";
9
+ /**
10
+ * Creates a computed text direction from a locale accessor.
11
+ * @param $locale - Active locale accessor.
12
+ * @returns Signal with `rtl` or `ltr` direction.
13
+ */
14
+ export declare function direction<T extends string>($locale: Accessor<T>): import("@nano_kit/store").ReadableSignal<"ltr" | "rtl">;
15
+ //# sourceMappingURL=direction.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"direction.d.ts","sourceRoot":"","sources":["../src/direction.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,QAAQ,EAEd,MAAM,iBAAiB,CAAA;AAExB,eAAO,MAAM,UAAU,aAarB,CAAA;AAEF;;;;GAIG;AAEH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,iBAI1C;AAED;;;;GAIG;AAEH,wBAAgB,SAAS,CAAC,CAAC,SAAS,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,2DAE/D","sourcesContent":["import {\n type Accessor,\n computed\n} from '@nano_kit/store'\n\nexport const rtlLocales = new Set([\n 'ar', // Arabic\n 'arc', // Aramaic\n 'dv', // Divehi\n 'fa', // Persian\n 'ha', // Hausa\n 'he', // Hebrew\n 'khw', // Khowar\n 'ks', // Kashmiri\n 'ku', // Kurdish\n 'ps', // Pashto\n 'ur', // Urdu\n 'yi' // Yiddish\n])\n\n/**\n * Resolves text direction for a locale.\n * @param locale - BCP 47 locale code.\n * @returns `rtl` for right-to-left languages, otherwise `ltr`.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function getDirection(locale: string) {\n const lang = new Intl.Locale(locale).language\n\n return rtlLocales.has(lang) ? 'rtl' : 'ltr'\n}\n\n/**\n * Creates a computed text direction from a locale accessor.\n * @param $locale - Active locale accessor.\n * @returns Signal with `rtl` or `ltr` direction.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function direction<T extends string>($locale: Accessor<T>) {\n return computed(() => getDirection($locale()))\n}\n"]}
@@ -0,0 +1,37 @@
1
+ import type { Format } from './types.js';
2
+ import { type IntlFormatFn } from './intlformat.js';
3
+ /**
4
+ * Creates a date/time formatter in raw mode.
5
+ * @param options - Pass `false` to return the input value without formatting.
6
+ * @returns Formatter that returns the input value or `undefined`.
7
+ */
8
+ export declare function datetime(options: false): Format<Date | number | undefined>;
9
+ /**
10
+ * Creates a locale-aware date/time formatter.
11
+ * @param options - `Intl.DateTimeFormat` options.
12
+ * @returns Formatter that returns a formatted string or `undefined`.
13
+ */
14
+ export declare function datetime(options?: Intl.DateTimeFormatOptions): Format<Date | number | undefined, string | undefined>;
15
+ /**
16
+ * Creates a date/time formatter in raw mode with a fallback value.
17
+ * @param fallback - Value returned when the input is `undefined` or `null`.
18
+ * @param options - Pass `false` to return the input value or fallback without formatting.
19
+ * @returns Formatter that returns the input value or fallback.
20
+ */
21
+ export declare function datetime(fallback: Date | number, options: false): Format<Date | number | undefined, Date | number>;
22
+ /**
23
+ * Creates a locale-aware date/time formatter with a fallback value.
24
+ * @param fallback - Value used when the input is `undefined` or `null`.
25
+ * @param options - `Intl.DateTimeFormat` options.
26
+ * @returns Formatter that returns a formatted string or `undefined`.
27
+ */
28
+ export declare function datetime(fallback: Date | number, options?: Intl.DateTimeFormatOptions): Format<Date | number | undefined, string | undefined>;
29
+ /**
30
+ * Creates a locale-aware date/time formatter with a custom formatting function.
31
+ * @param optionsOrFallback - `Intl.DateTimeFormat` options, `false`, or fallback value.
32
+ * @param maybeOptions - `Intl.DateTimeFormat` options or `false` when fallback is provided.
33
+ * @param format - Custom formatting function.
34
+ * @returns Formatter that returns a formatted string or `undefined`.
35
+ */
36
+ export declare function datetime(optionsOrFallback?: Intl.DateTimeFormatOptions | false | Date | number, maybeOptions?: Intl.DateTimeFormatOptions | false, format?: IntlFormatFn<Intl.DateTimeFormat, Date | number, Intl.DateTimeFormatOptions>): Format<Date | number | undefined, string | undefined>;
37
+ //# sourceMappingURL=datetime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"datetime.d.ts","sourceRoot":"","sources":["../../src/format/datetime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACxC,OAAO,EACL,KAAK,YAAY,EAGlB,MAAM,iBAAiB,CAAA;AAExB;;;;GAIG;AACH,wBAAgB,QAAQ,CACtB,OAAO,EAAE,KAAK,GACb,MAAM,CAAC,IAAI,GAAG,MAAM,GAAG,SAAS,CAAC,CAAA;AAEpC;;;;GAIG;AACH,wBAAgB,QAAQ,CACtB,OAAO,CAAC,EAAE,IAAI,CAAC,qBAAqB,GACnC,MAAM,CAAC,IAAI,GAAG,MAAM,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AAExD;;;;;GAKG;AACH,wBAAgB,QAAQ,CACtB,QAAQ,EAAE,IAAI,GAAG,MAAM,EACvB,OAAO,EAAE,KAAK,GACb,MAAM,CAAC,IAAI,GAAG,MAAM,GAAG,SAAS,EAAE,IAAI,GAAG,MAAM,CAAC,CAAA;AAEnD;;;;;GAKG;AACH,wBAAgB,QAAQ,CACtB,QAAQ,EAAE,IAAI,GAAG,MAAM,EACvB,OAAO,CAAC,EAAE,IAAI,CAAC,qBAAqB,GACnC,MAAM,CAAC,IAAI,GAAG,MAAM,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AAExD;;;;;;GAMG;AACH,wBAAgB,QAAQ,CACtB,iBAAiB,CAAC,EAAE,IAAI,CAAC,qBAAqB,GAAG,KAAK,GAAG,IAAI,GAAG,MAAM,EACtE,YAAY,CAAC,EAAE,IAAI,CAAC,qBAAqB,GAAG,KAAK,EACjD,MAAM,CAAC,EAAE,YAAY,CAAC,IAAI,CAAC,cAAc,EAAE,IAAI,GAAG,MAAM,EAAE,IAAI,CAAC,qBAAqB,CAAC,GACpF,MAAM,CAAC,IAAI,GAAG,MAAM,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA","sourcesContent":["import type { Format } from './types.js'\nimport {\n type IntlFormatFn,\n defaultFormat,\n intlformat\n} from './intlformat.js'\n\n/**\n * Creates a date/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 datetime(\n options: false\n): Format<Date | number | undefined>\n\n/**\n * Creates a locale-aware date/time formatter.\n * @param options - `Intl.DateTimeFormat` options.\n * @returns Formatter that returns a formatted string or `undefined`.\n */\nexport function datetime(\n options?: Intl.DateTimeFormatOptions\n): Format<Date | number | undefined, string | undefined>\n\n/**\n * Creates a date/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 datetime(\n fallback: Date | number,\n options: false\n): Format<Date | number | undefined, Date | number>\n\n/**\n * Creates a locale-aware date/time formatter with a fallback value.\n * @param fallback - Value used when the input is `undefined` or `null`.\n * @param options - `Intl.DateTimeFormat` options.\n * @returns Formatter that returns a formatted string or `undefined`.\n */\nexport function datetime(\n fallback: Date | number,\n options?: Intl.DateTimeFormatOptions\n): Format<Date | number | undefined, string | undefined>\n\n/**\n * Creates a locale-aware date/time formatter with a custom formatting function.\n * @param optionsOrFallback - `Intl.DateTimeFormat` options, `false`, or fallback value.\n * @param maybeOptions - `Intl.DateTimeFormat` options or `false` when fallback is provided.\n * @param format - Custom formatting function.\n * @returns Formatter that returns a formatted string or `undefined`.\n */\nexport function datetime(\n optionsOrFallback?: Intl.DateTimeFormatOptions | false | Date | number,\n maybeOptions?: Intl.DateTimeFormatOptions | false,\n format?: IntlFormatFn<Intl.DateTimeFormat, Date | number, Intl.DateTimeFormatOptions>\n): Format<Date | number | undefined, string | undefined>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function datetime(\n optionsOrFallback?: Intl.DateTimeFormatOptions | false | Date | number,\n maybeOptions?: Intl.DateTimeFormatOptions | false,\n format: IntlFormatFn<Intl.DateTimeFormat, Date | number, Intl.DateTimeFormatOptions> = defaultFormat\n) {\n return intlformat(\n Intl.DateTimeFormat,\n format,\n optionsOrFallback,\n maybeOptions\n )\n}\n"]}
@@ -0,0 +1,29 @@
1
+ import type { Format } from './types.js';
2
+ export type Duration = Partial<Record<Intl.DurationFormatUnit, number>>;
3
+ /**
4
+ * Creates a duration formatter in raw mode.
5
+ * @param options - Pass `false` to return the input value without formatting.
6
+ * @returns Formatter that returns the input value or `undefined`.
7
+ */
8
+ export declare function duration(options: false): Format<Duration | undefined>;
9
+ /**
10
+ * Creates a locale-aware duration formatter.
11
+ * @param options - `Intl.DurationFormat` options.
12
+ * @returns Formatter that returns a formatted string or `undefined`.
13
+ */
14
+ export declare function duration(options?: Intl.DurationFormatOptions): Format<Duration | undefined, string | undefined>;
15
+ /**
16
+ * Creates a duration formatter in raw mode with a fallback value.
17
+ * @param fallback - Value returned when the input is `undefined` or `null`.
18
+ * @param options - Pass `false` to return the input value or fallback without formatting.
19
+ * @returns Formatter that returns the input value or fallback.
20
+ */
21
+ export declare function duration(fallback: Duration, options: false): Format<Duration | undefined, Duration>;
22
+ /**
23
+ * Creates a locale-aware duration formatter with a fallback value.
24
+ * @param fallback - Value used when the input is `undefined` or `null`.
25
+ * @param options - `Intl.DurationFormat` options.
26
+ * @returns Formatter that returns a formatted string or `undefined`.
27
+ */
28
+ export declare function duration(fallback: Duration, options?: Intl.DurationFormatOptions): Format<Duration | undefined, string | undefined>;
29
+ //# sourceMappingURL=duration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"duration.d.ts","sourceRoot":"","sources":["../../src/format/duration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAMxC,MAAM,MAAM,QAAQ,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,kBAAkB,EAAE,MAAM,CAAC,CAAC,CAAA;AAEvE;;;;GAIG;AACH,wBAAgB,QAAQ,CACtB,OAAO,EAAE,KAAK,GACb,MAAM,CAAC,QAAQ,GAAG,SAAS,CAAC,CAAA;AAE/B;;;;GAIG;AACH,wBAAgB,QAAQ,CACtB,OAAO,CAAC,EAAE,IAAI,CAAC,qBAAqB,GACnC,MAAM,CAAC,QAAQ,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AAEnD;;;;;GAKG;AACH,wBAAgB,QAAQ,CACtB,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,KAAK,GACb,MAAM,CAAC,QAAQ,GAAG,SAAS,EAAE,QAAQ,CAAC,CAAA;AAEzC;;;;;GAKG;AACH,wBAAgB,QAAQ,CACtB,QAAQ,EAAE,QAAQ,EAClB,OAAO,CAAC,EAAE,IAAI,CAAC,qBAAqB,GACnC,MAAM,CAAC,QAAQ,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA","sourcesContent":["import type { Format } from './types.js'\nimport {\n defaultFormat,\n intlformat\n} from './intlformat.js'\n\nexport type Duration = Partial<Record<Intl.DurationFormatUnit, number>>\n\n/**\n * Creates a duration 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 duration(\n options: false\n): Format<Duration | undefined>\n\n/**\n * Creates a locale-aware duration formatter.\n * @param options - `Intl.DurationFormat` options.\n * @returns Formatter that returns a formatted string or `undefined`.\n */\nexport function duration(\n options?: Intl.DurationFormatOptions\n): Format<Duration | undefined, string | undefined>\n\n/**\n * Creates a duration 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 duration(\n fallback: Duration,\n options: false\n): Format<Duration | undefined, Duration>\n\n/**\n * Creates a locale-aware duration formatter with a fallback value.\n * @param fallback - Value used when the input is `undefined` or `null`.\n * @param options - `Intl.DurationFormat` options.\n * @returns Formatter that returns a formatted string or `undefined`.\n */\nexport function duration(\n fallback: Duration,\n options?: Intl.DurationFormatOptions\n): Format<Duration | undefined, string | undefined>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function duration(\n optionsOrFallback?: Intl.DurationFormatOptions | false | Duration,\n maybeOptions?: Intl.DurationFormatOptions | false\n) {\n return intlformat<Intl.DurationFormat, Duration, Intl.DurationFormatOptions>(\n Intl.DurationFormat,\n defaultFormat,\n optionsOrFallback,\n maybeOptions\n )\n}\n"]}
@@ -0,0 +1,9 @@
1
+ import type { AnyFormat, Format, FormatInput, FormatOutput } from './types.js';
2
+ export type Formatter<F extends AnyFormat> = (value: FormatInput<F>) => FormatOutput<F>;
3
+ /**
4
+ * Creates a formatter value from another format.
5
+ * @param type - Format used to format values later.
6
+ * @returns Format that returns a value formatter bound to the current context.
7
+ */
8
+ export declare function format<F extends AnyFormat>(type: F): Format<undefined, Formatter<F>>;
9
+ //# sourceMappingURL=format.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../src/format/format.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,SAAS,EACT,MAAM,EACN,WAAW,EACX,YAAY,EACb,MAAM,YAAY,CAAA;AAEnB,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,SAAS,IAAI,CAC3C,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,KAClB,YAAY,CAAC,CAAC,CAAC,CAAA;AAEpB;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,CAAC,SAAS,SAAS,EACxC,IAAI,EAAE,CAAC,GACN,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,CAAA","sourcesContent":["import type { FormatContext } from '../types.js'\nimport type {\n AnyFormat,\n Format,\n FormatInput,\n FormatOutput\n} from './types.js'\n\nexport type Formatter<F extends AnyFormat> = (\n value: FormatInput<F>\n) => FormatOutput<F>\n\n/**\n * Creates a formatter value from another format.\n * @param type - Format used to format values later.\n * @returns Format that returns a value formatter bound to the current context.\n */\nexport function format<F extends AnyFormat>(\n type: F\n): Format<undefined, Formatter<F>>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function format<F extends AnyFormat>(\n type: F\n) {\n return (ctx: FormatContext) => (value: FormatInput<F>) => type(ctx, value)\n}\n"]}
@@ -0,0 +1,16 @@
1
+ export type { Format, AnyFormat, IntlFormat, IntlFormatRange } from './types.js';
2
+ export * from './raw.js';
3
+ export * from './text.js';
4
+ export * from './number.js';
5
+ export * from './datetime.js';
6
+ export * from './range.js';
7
+ export * from './relativetime.js';
8
+ export * from './duration.js';
9
+ export * from './list.js';
10
+ export * from './rich.js';
11
+ export * from './markup.js';
12
+ export * from './format.js';
13
+ export * from './params.js';
14
+ export * from './match.js';
15
+ export * from './plural.js';
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/format/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,MAAM,EACN,SAAS,EACT,UAAU,EACV,eAAe,EAChB,MAAM,YAAY,CAAA;AACnB,cAAc,UAAU,CAAA;AACxB,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,mBAAmB,CAAA;AACjC,cAAc,eAAe,CAAA;AAC7B,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA","sourcesContent":["export type {\n Format,\n AnyFormat,\n IntlFormat,\n IntlFormatRange\n} from './types.js'\nexport * from './raw.js'\nexport * from './text.js'\nexport * from './number.js'\nexport * from './datetime.js'\nexport * from './range.js'\nexport * from './relativetime.js'\nexport * from './duration.js'\nexport * from './list.js'\nexport * from './rich.js'\nexport * from './markup.js'\nexport * from './format.js'\nexport * from './params.js'\nexport * from './match.js'\nexport * from './plural.js'\n"]}
@@ -0,0 +1,7 @@
1
+ import type { FormatContext } from '../types.js';
2
+ import type { IntlFormat } from './types.js';
3
+ export type IntlFormatConstructor<F, O> = new (locale: string, options?: O) => F;
4
+ export type IntlFormatFn<F extends IntlFormat<I>, I, O> = (fmt: F, value: I, options: O | undefined) => string;
5
+ export declare function defaultFormat<F extends IntlFormat<I>, I>(fmt: F, value: I): string;
6
+ export declare function intlformat<F extends IntlFormat<I>, I, O>(IntlFormat: IntlFormatConstructor<NoInfer<F>, O>, format: IntlFormatFn<F, I, O>, optionsOrFallback?: O | false | I, maybeOptions?: O | false): (ctx: FormatContext, input?: I) => string | I | undefined;
7
+ //# sourceMappingURL=intlformat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"intlformat.d.ts","sourceRoot":"","sources":["../../src/format/intlformat.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAEhD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAG5C,MAAM,MAAM,qBAAqB,CAAC,CAAC,EAAE,CAAC,IAAI,KACxC,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,CAAC,KACR,CAAC,CAAA;AAEN,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CACxD,GAAG,EAAE,CAAC,EACN,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,CAAC,GAAG,SAAS,KACnB,MAAM,CAAA;AAEX,wBAAgB,aAAa,CAAC,CAAC,SAAS,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,EACtD,GAAG,EAAE,CAAC,EACN,KAAK,EAAE,CAAC,UAGT;AAED,wBAAgB,UAAU,CACxB,CAAC,SAAS,UAAU,CAAC,CAAC,CAAC,EACvB,CAAC,EACD,CAAC,EAED,UAAU,EAAE,qBAAqB,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAChD,MAAM,EAAE,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAC7B,iBAAiB,CAAC,EAAE,CAAC,GAAG,KAAK,GAAG,CAAC,EACjC,YAAY,CAAC,EAAE,CAAC,GAAG,KAAK,SAoBX,aAAa,UAAU,CAAC,4BAStC","sourcesContent":["import type { FormatContext } from '../types.js'\nimport { memo } from '../utils.js'\nimport type { IntlFormat } from './types.js'\nimport { raw } from './raw.js'\n\nexport type IntlFormatConstructor<F, O> = new (\n locale: string,\n options?: O\n) => F\n\nexport type IntlFormatFn<F extends IntlFormat<I>, I, O> = (\n fmt: F,\n value: I,\n options: O | undefined\n) => string\n\nexport function defaultFormat<F extends IntlFormat<I>, I>(\n fmt: F,\n value: I\n) {\n return fmt.format(value)\n}\n\nexport function intlformat<\n F extends IntlFormat<I>,\n I,\n O\n>(\n IntlFormat: IntlFormatConstructor<NoInfer<F>, O>,\n format: IntlFormatFn<F, I, O>,\n optionsOrFallback?: O | false | I,\n maybeOptions?: O | false\n) {\n let fallback: I | undefined\n let options: O | false | undefined\n\n if (maybeOptions !== undefined) {\n fallback = optionsOrFallback as I\n options = maybeOptions\n } else {\n options = optionsOrFallback as O | false | undefined\n }\n\n const rawFormat = raw(fallback)\n\n if (options === false) {\n return rawFormat\n }\n\n const fmt = memo((locale: string) => new IntlFormat(locale, options))\n\n return (ctx: FormatContext, input?: I) => {\n const value = rawFormat(ctx, input)\n\n if (value === undefined) {\n return value\n }\n\n return format(fmt(ctx.$locale()), value, options)\n }\n}\n"]}
@@ -0,0 +1,28 @@
1
+ import type { Format } from './types.js';
2
+ /**
3
+ * Creates a list formatter in raw mode.
4
+ * @param options - Pass `false` to return the input value without formatting.
5
+ * @returns Formatter that returns the input value or `undefined`.
6
+ */
7
+ export declare function list(options: false): Format<Iterable<string> | undefined>;
8
+ /**
9
+ * Creates a locale-aware list formatter.
10
+ * @param options - `Intl.ListFormat` options.
11
+ * @returns Formatter that returns a formatted string or `undefined`.
12
+ */
13
+ export declare function list(options?: Intl.ListFormatOptions): Format<Iterable<string> | undefined, string | undefined>;
14
+ /**
15
+ * Creates a list formatter in raw mode with a fallback value.
16
+ * @param fallback - Value returned when the input is `undefined` or `null`.
17
+ * @param options - Pass `false` to return the input value or fallback without formatting.
18
+ * @returns Formatter that returns the input value or fallback.
19
+ */
20
+ export declare function list(fallback: Iterable<string>, options: false): Format<Iterable<string> | undefined, Iterable<string>>;
21
+ /**
22
+ * Creates a locale-aware list formatter with a fallback value.
23
+ * @param fallback - Value used when the input is `undefined` or `null`.
24
+ * @param options - `Intl.ListFormat` options.
25
+ * @returns Formatter that returns a formatted string or `undefined`.
26
+ */
27
+ export declare function list(fallback: Iterable<string>, options?: Intl.ListFormatOptions): Format<Iterable<string> | undefined, string | undefined>;
28
+ //# sourceMappingURL=list.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"list.d.ts","sourceRoot":"","sources":["../../src/format/list.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAMxC;;;;GAIG;AACH,wBAAgB,IAAI,CAClB,OAAO,EAAE,KAAK,GACb,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC,CAAA;AAEvC;;;;GAIG;AACH,wBAAgB,IAAI,CAClB,OAAO,CAAC,EAAE,IAAI,CAAC,iBAAiB,GAC/B,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AAE3D;;;;;GAKG;AACH,wBAAgB,IAAI,CAClB,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,EAC1B,OAAO,EAAE,KAAK,GACb,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,SAAS,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAA;AAEzD;;;;;GAKG;AACH,wBAAgB,IAAI,CAClB,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,EAC1B,OAAO,CAAC,EAAE,IAAI,CAAC,iBAAiB,GAC/B,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA","sourcesContent":["import type { Format } from './types.js'\nimport {\n defaultFormat,\n intlformat\n} from './intlformat.js'\n\n/**\n * Creates a list 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 list(\n options: false\n): Format<Iterable<string> | undefined>\n\n/**\n * Creates a locale-aware list formatter.\n * @param options - `Intl.ListFormat` options.\n * @returns Formatter that returns a formatted string or `undefined`.\n */\nexport function list(\n options?: Intl.ListFormatOptions\n): Format<Iterable<string> | undefined, string | undefined>\n\n/**\n * Creates a list 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 list(\n fallback: Iterable<string>,\n options: false\n): Format<Iterable<string> | undefined, Iterable<string>>\n\n/**\n * Creates a locale-aware list formatter with a fallback value.\n * @param fallback - Value used when the input is `undefined` or `null`.\n * @param options - `Intl.ListFormat` options.\n * @returns Formatter that returns a formatted string or `undefined`.\n */\nexport function list(\n fallback: Iterable<string>,\n options?: Intl.ListFormatOptions\n): Format<Iterable<string> | undefined, string | undefined>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function list(\n optionsOrFallback?: Intl.ListFormatOptions | false | Iterable<string>,\n maybeOptions?: Intl.ListFormatOptions | false\n) {\n return intlformat<Intl.ListFormat, Iterable<string>, Intl.ListFormatOptions>(\n Intl.ListFormat,\n defaultFormat,\n optionsOrFallback,\n maybeOptions\n )\n}\n"]}
@@ -0,0 +1,17 @@
1
+ import type { Format } from './types.js';
2
+ export type MarkupTag = (chunks: string) => string;
3
+ export type MarkupTags = Record<string, MarkupTag>;
4
+ /**
5
+ * Creates a markup formatter.
6
+ * @param tags - Tag handlers used to map markup tags to strings.
7
+ * @returns Formatter that returns a mapped string or `undefined`.
8
+ */
9
+ export declare function markup(tags: MarkupTags): Format<string | undefined, string | undefined>;
10
+ /**
11
+ * Creates a markup formatter with a fallback message.
12
+ * @param fallback - Message used when the input is `undefined` or `null`.
13
+ * @param tags - Tag handlers used to map markup tags to strings.
14
+ * @returns Formatter that returns a mapped string.
15
+ */
16
+ export declare function markup(fallback: string, tags: MarkupTags): Format<string | undefined, string>;
17
+ //# sourceMappingURL=markup.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"markup.d.ts","sourceRoot":"","sources":["../../src/format/markup.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAMxC,MAAM,MAAM,SAAS,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,CAAA;AAElD,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;AAElD;;;;GAIG;AACH,wBAAgB,MAAM,CACpB,IAAI,EAAE,UAAU,GACf,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AAEjD;;;;;GAKG;AACH,wBAAgB,MAAM,CACpB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,UAAU,GACf,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,MAAM,CAAC,CAAA","sourcesContent":["import type { Format } from './types.js'\nimport {\n type RichTags,\n rich\n} from './rich.js'\n\nexport type MarkupTag = (chunks: string) => string\n\nexport type MarkupTags = Record<string, MarkupTag>\n\n/**\n * Creates a markup formatter.\n * @param tags - Tag handlers used to map markup tags to strings.\n * @returns Formatter that returns a mapped string or `undefined`.\n */\nexport function markup(\n tags: MarkupTags\n): Format<string | undefined, string | undefined>\n\n/**\n * Creates a markup 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 markup tags to strings.\n * @returns Formatter that returns a mapped string.\n */\nexport function markup(\n fallback: string,\n tags: MarkupTags\n): Format<string | undefined, string>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function markup(\n fallbackOrTags: string | MarkupTags,\n maybeTags?: MarkupTags\n): Format<string | undefined, string | undefined> {\n let fallback: string | undefined\n let tags: MarkupTags\n\n if (maybeTags) {\n fallback = fallbackOrTags as string\n tags = maybeTags\n } else {\n tags = fallbackOrTags as MarkupTags\n }\n\n const richTags: RichTags<string> = {}\n\n for (const [tag, format] of Object.entries(tags)) {\n richTags[tag] = chunks => format(chunks.join(''))\n }\n\n const richFormat = rich(fallback, richTags)\n\n return (ctx, input) => richFormat(ctx, input)?.join('')\n}\n"]}
@@ -0,0 +1,26 @@
1
+ import { type ValueOrAccessor } from '@nano_kit/store';
2
+ import type { FormatContext } from '../types.js';
3
+ import type { AnyFormat, FnWithParams, Format, FormatsInput, UnionToIntersection, ParamsOf } from './types.js';
4
+ type CaseParams<F> = F extends AnyFormat<infer O> ? ParamsOf<O> : {};
5
+ export type MatchParams<K extends string, B extends Partial<Record<string, AnyFormat>>> = {
6
+ [P in K]: ValueOrAccessor<number | string>;
7
+ } & UnionToIntersection<CaseParams<B[keyof B]>>;
8
+ export type ArgMatchFn = FnWithParams<ValueOrAccessor<number | string>>;
9
+ export type MatchFn<K extends string, B extends Partial<Record<string, AnyFormat>>> = FnWithParams<MatchParams<K, B> | ValueOrAccessor<number | string>>;
10
+ export type CasesFn<K extends string, B extends Partial<Record<string, AnyFormat>>> = (ctx: FormatContext, input: FormatsInput<B> | undefined, resolvedCases: Record<string, string | undefined | MatchFn<K, B>>) => void;
11
+ /**
12
+ * Creates a reusable cases resolver for `match`.
13
+ * @param cases - Case formatters keyed by possible resolved values.
14
+ * @returns Resolver that formats all cases for the current context and translation input.
15
+ */
16
+ export declare function cases<const K extends string, const B extends Partial<Record<string, AnyFormat>>>(cases: B): CasesFn<K, B>;
17
+ /**
18
+ * Creates a formatter that selects one case by parameter value.
19
+ * @param param - Parameter name used to select a case and replace `{param}` placeholders.
20
+ * @param cases - Case formatters keyed by possible resolved values.
21
+ * @param getKey - Optional resolver used to map the parameter value to a case key.
22
+ * @returns Formatter that resolves cases and returns a parameterized message function.
23
+ */
24
+ export declare function match<const K extends string, const B extends Partial<Record<string, AnyFormat>>>(param: K, cases?: CasesFn<K, B>, getKey?: (param: any, locale: string, cases: Record<string, unknown>) => string): Format<FormatsInput<B>, MatchFn<K, B>>;
25
+ export {};
26
+ //# sourceMappingURL=match.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"match.d.ts","sourceRoot":"","sources":["../../src/format/match.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,eAAe,EAIrB,MAAM,iBAAiB,CAAA;AACxB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAChD,OAAO,KAAK,EACV,SAAS,EACT,YAAY,EAC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SAAS,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,EAElD,KAAK,EAAE,CAAC,GACP,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAQf;AAED;;;;;;GAMG;AAEH,wBAAgB,KAAK,CACnB,KAAK,CAAC,CAAC,SAAS,MAAM,EACtB,KAAK,CAAC,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,EAElD,KAAK,EAAE,CAAC,EACR,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,EACrB,MAAM,GAAE,CACN,KAAK,EAAE,GAAG,EACV,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC3B,MAAiB,GACrB,MAAM,CACP,YAAY,CAAC,CAAC,CAAC,EACf,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CACd,CAwBA","sourcesContent":["import {\n type ValueOrAccessor,\n $get,\n identity,\n isFunction\n} from '@nano_kit/store'\nimport type { FormatContext } from '../types.js'\nimport type {\n AnyFormat,\n FnWithParams,\n Format,\n FormatsInput,\n UnionToIntersection,\n ParamsOf\n} from './types.js'\n\ntype CaseParams<F> = F extends AnyFormat<infer O> ? ParamsOf<O> : {}\n\nexport type MatchParams<\n K extends string,\n B extends Partial<Record<string, AnyFormat>>\n> = {\n [P in K]: ValueOrAccessor<number | string>\n} & UnionToIntersection<CaseParams<B[keyof B]>>\n\nexport type ArgMatchFn = FnWithParams<ValueOrAccessor<number | string>>\n\nexport type MatchFn<\n K extends string,\n B extends Partial<Record<string, AnyFormat>>\n> = FnWithParams<MatchParams<K, B> | ValueOrAccessor<number | string>>\n\nexport type CasesFn<\n K extends string,\n B extends Partial<Record<string, AnyFormat>>\n> = (\n ctx: FormatContext,\n input: FormatsInput<B> | undefined,\n resolvedCases: Record<string, string | undefined | MatchFn<K, B>>\n) => void\n\n/**\n * Creates a reusable cases resolver for `match`.\n * @param cases - Case formatters keyed by possible resolved values.\n * @returns Resolver that formats all cases for the current context and translation input.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function cases<\n const K extends string,\n const B extends Partial<Record<string, AnyFormat>>\n>(\n cases: B\n): CasesFn<K, B> {\n const entries = Object.entries(cases)\n\n return (ctx, input, resolvedCases) => {\n for (const [key, format] of entries) {\n resolvedCases[key] = format!(ctx, input?.[key]) as string | undefined | MatchFn<K, B>\n }\n }\n}\n\n/**\n * Creates a formatter that selects one case by parameter value.\n * @param param - Parameter name used to select a case and replace `{param}` placeholders.\n * @param cases - Case formatters keyed by possible resolved values.\n * @param getKey - Optional resolver used to map the parameter value to a case key.\n * @returns Formatter that resolves cases and returns a parameterized message function.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function match<\n const K extends string,\n const B extends Partial<Record<string, AnyFormat>>\n>(\n param: K,\n cases?: CasesFn<K, B>,\n getKey: (\n param: any,\n locale: string,\n cases: Record<string, unknown>\n ) => string = identity\n): Format<\n FormatsInput<B>,\n MatchFn<K, B>\n> {\n const valuePattern = new RegExp(`{${param}}`, 'g')\n\n return (ctx, input: FormatsInput<B> | undefined) => {\n const locale = ctx.$locale()\n let resolvedCases: Record<string, string | undefined | MatchFn<K, B>> = input ?? {}\n\n if (cases) {\n resolvedCases = {\n ...resolvedCases\n }\n\n cases(ctx, input, resolvedCases)\n }\n\n return (params) => {\n const value = $get(typeof params === 'object' ? params[param] : params)\n const key = getKey(value, locale, resolvedCases)\n const resolvedCase = resolvedCases[key]\n const message = isFunction(resolvedCase) ? resolvedCase(params) : resolvedCase\n\n return message?.replace(valuePattern, value as string)\n }\n }\n}\n"]}
@@ -0,0 +1,37 @@
1
+ import type { Format } from './types.js';
2
+ import { type IntlFormatFn } from './intlformat.js';
3
+ /**
4
+ * Creates a number formatter in raw mode.
5
+ * @param options - Pass `false` to return the input value without formatting.
6
+ * @returns Formatter that returns the input value or `undefined`.
7
+ */
8
+ export declare function number(options: false): Format<number | bigint | string | undefined>;
9
+ /**
10
+ * Creates a locale-aware number formatter.
11
+ * @param options - `Intl.NumberFormat` options.
12
+ * @returns Formatter that returns a formatted string or `undefined`.
13
+ */
14
+ export declare function number(options?: Intl.NumberFormatOptions): Format<number | bigint | string | undefined, string | undefined>;
15
+ /**
16
+ * Creates a number formatter in raw mode with a fallback value.
17
+ * @param fallback - Value returned when the input is `undefined` or `null`.
18
+ * @param options - Pass `false` to return the input value or fallback without formatting.
19
+ * @returns Formatter that returns the input value or fallback.
20
+ */
21
+ export declare function number(fallback: number | bigint | string, options: false): Format<number | bigint | string | undefined, number | bigint | string>;
22
+ /**
23
+ * Creates a locale-aware number formatter with a fallback value.
24
+ * @param fallback - Value used when the input is `undefined` or `null`.
25
+ * @param options - `Intl.NumberFormat` options.
26
+ * @returns Formatter that returns a formatted string or `undefined`.
27
+ */
28
+ export declare function number(fallback: number | bigint | string, options?: Intl.NumberFormatOptions): Format<number | bigint | string | undefined, string | undefined>;
29
+ /**
30
+ * Creates a locale-aware number formatter with a custom formatting function.
31
+ * @param optionsOrFallback - `Intl.NumberFormat` options, `false`, or fallback value.
32
+ * @param maybeOptions - `Intl.NumberFormat` options or `false` when fallback is provided.
33
+ * @param format - Custom formatting function.
34
+ * @returns Formatter that returns a formatted string or `undefined`.
35
+ */
36
+ export declare function number(optionsOrFallback?: Intl.NumberFormatOptions | false | number | bigint | string, maybeOptions?: Intl.NumberFormatOptions | false, format?: IntlFormatFn<Intl.NumberFormat, number | bigint | string, Intl.NumberFormatOptions>): Format<number | bigint | string | undefined, string | undefined>;
37
+ //# sourceMappingURL=number.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../../src/format/number.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACxC,OAAO,EACL,KAAK,YAAY,EAGlB,MAAM,iBAAiB,CAAA;AAExB;;;;GAIG;AACH,wBAAgB,MAAM,CACpB,OAAO,EAAE,KAAK,GACb,MAAM,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC,CAAA;AAE/C;;;;GAIG;AACH,wBAAgB,MAAM,CACpB,OAAO,CAAC,EAAE,IAAI,CAAC,mBAAmB,GACjC,MAAM,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AAEnE;;;;;GAKG;AACH,wBAAgB,MAAM,CACpB,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAClC,OAAO,EAAE,KAAK,GACb,MAAM,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC,CAAA;AAEzE;;;;;GAKG;AACH,wBAAgB,MAAM,CACpB,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAClC,OAAO,CAAC,EAAE,IAAI,CAAC,mBAAmB,GACjC,MAAM,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AAEnE;;;;;;GAMG;AACH,wBAAgB,MAAM,CACpB,iBAAiB,CAAC,EAAE,IAAI,CAAC,mBAAmB,GAAG,KAAK,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,EAC/E,YAAY,CAAC,EAAE,IAAI,CAAC,mBAAmB,GAAG,KAAK,EAC/C,MAAM,CAAC,EAAE,YAAY,CAAC,IAAI,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,IAAI,CAAC,mBAAmB,CAAC,GAC3F,MAAM,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA","sourcesContent":["import type { Format } from './types.js'\nimport {\n type IntlFormatFn,\n defaultFormat,\n intlformat\n} from './intlformat.js'\n\n/**\n * Creates a number 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 number(\n options: false\n): Format<number | bigint | string | undefined>\n\n/**\n * Creates a locale-aware number formatter.\n * @param options - `Intl.NumberFormat` options.\n * @returns Formatter that returns a formatted string or `undefined`.\n */\nexport function number(\n options?: Intl.NumberFormatOptions\n): Format<number | bigint | string | undefined, string | undefined>\n\n/**\n * Creates a number 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 number(\n fallback: number | bigint | string,\n options: false\n): Format<number | bigint | string | undefined, number | bigint | string>\n\n/**\n * Creates a locale-aware number formatter with a fallback value.\n * @param fallback - Value used when the input is `undefined` or `null`.\n * @param options - `Intl.NumberFormat` options.\n * @returns Formatter that returns a formatted string or `undefined`.\n */\nexport function number(\n fallback: number | bigint | string,\n options?: Intl.NumberFormatOptions\n): Format<number | bigint | string | undefined, string | undefined>\n\n/**\n * Creates a locale-aware number formatter with a custom formatting function.\n * @param optionsOrFallback - `Intl.NumberFormat` options, `false`, or fallback value.\n * @param maybeOptions - `Intl.NumberFormat` options or `false` when fallback is provided.\n * @param format - Custom formatting function.\n * @returns Formatter that returns a formatted string or `undefined`.\n */\nexport function number(\n optionsOrFallback?: Intl.NumberFormatOptions | false | number | bigint | string,\n maybeOptions?: Intl.NumberFormatOptions | false,\n format?: IntlFormatFn<Intl.NumberFormat, number | bigint | string, Intl.NumberFormatOptions>\n): Format<number | bigint | string | undefined, string | undefined>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function number(\n optionsOrFallback?: Intl.NumberFormatOptions | false | number | bigint | string,\n maybeOptions?: Intl.NumberFormatOptions | false,\n format: IntlFormatFn<Intl.NumberFormat, number | bigint | string, Intl.NumberFormatOptions> = defaultFormat\n) {\n return intlformat<Intl.NumberFormat, number | bigint | string, Intl.NumberFormatOptions>(\n Intl.NumberFormat,\n format,\n optionsOrFallback,\n maybeOptions\n )\n}\n"]}
@@ -0,0 +1,31 @@
1
+ import { type ValueOrAccessor } from '@nano_kit/store';
2
+ import type { FnWithAnyParams, Format, FormatInput, FormatOutput, FnWithParams, ParamsOf, UnionToIntersection } from './types.js';
3
+ export type ReplacementValue = string | number | boolean | bigint | undefined;
4
+ export type ParamsFormats = Record<string, Format<any, ReplacementValue>>;
5
+ export type RequiredParamKeys<P extends ParamsFormats> = {
6
+ [K in keyof P]: undefined extends FormatOutput<P[K]> ? K : never;
7
+ }[keyof P];
8
+ export type OptionalParamKeys<P extends ParamsFormats> = Exclude<keyof P, RequiredParamKeys<P>>;
9
+ export type ParamsValues<P extends ParamsFormats> = {
10
+ [K in RequiredParamKeys<P>]: ValueOrAccessor<FormatInput<P[K]>>;
11
+ } & {
12
+ [K in OptionalParamKeys<P>]?: ValueOrAccessor<FormatInput<P[K]> | undefined>;
13
+ };
14
+ export type ParamsFn<P extends ParamsFormats> = FnWithParams<ParamsValues<P>>;
15
+ type PreformatOutput = string | undefined | FnWithAnyParams;
16
+ type ComposedParamsFn<P extends ParamsFormats, O> = FnWithParams<ParamsValues<P> & UnionToIntersection<ParamsOf<O>>>;
17
+ /**
18
+ * Creates a parameter interpolation formatter.
19
+ * @param params - Parameter formatters keyed by placeholder name.
20
+ * @returns Formatter that replaces `{key}` placeholders using provided parameter values.
21
+ */
22
+ export declare function params<const P extends ParamsFormats>(params: P): Format<string | undefined, ParamsFn<P>>;
23
+ /**
24
+ * Creates a parameter interpolation formatter that first applies another formatter.
25
+ * @param params - Parameter formatters keyed by placeholder name.
26
+ * @param preformat - Formatter applied before replacing placeholders.
27
+ * @returns Formatter that replaces `{key}` placeholders using provided parameter values.
28
+ */
29
+ export declare function params<const P extends ParamsFormats, F = string | undefined, O extends PreformatOutput = string | undefined | ParamsFn<P>>(params: P, preformat: Format<F, O>): Format<F, ComposedParamsFn<P, O>>;
30
+ export {};
31
+ //# sourceMappingURL=params.d.ts.map
@@ -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K,eAAe,GAAG,MAAM,GAAG,SAAS,GAAG,eAAe,CAAA;AAE3D,KAAK,gBAAgB,CAAC,CAAC,SAAS,aAAa,EAAE,CAAC,IAAI,YAAY,CAAC,YAAY,CAAC,CAAC,CAAC,GAAG,mBAAmB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AAIpH;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,KAAK,CAAC,CAAC,SAAS,aAAa,EAClD,MAAM,EAAE,CAAC,GACR,MAAM,CAAC,MAAM,GAAG,SAAS,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAA;AAE1C;;;;;GAKG;AACH,wBAAgB,MAAM,CACpB,KAAK,CAAC,CAAC,SAAS,aAAa,EAC7B,CAAC,GAAG,MAAM,GAAG,SAAS,EACtB,CAAC,SAAS,eAAe,GAAG,MAAM,GAAG,SAAS,GAAG,QAAQ,CAAC,CAAC,CAAC,EAE5D,MAAM,EAAE,CAAC,EACT,SAAS,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GACtB,MAAM,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA","sourcesContent":["import {\n type ValueOrAccessor,\n $get,\n isFunction\n} from '@nano_kit/store'\nimport type { FormatContext } from '../types.js'\nimport type {\n FnWithAnyParams,\n Format,\n FormatInput,\n FormatOutput,\n FnWithParams,\n ParamsOf,\n UnionToIntersection\n} from './types.js'\n\nexport type ReplacementValue = string | number | boolean | bigint | undefined\n\nexport type ParamsFormats = Record<string, Format<any, ReplacementValue>>\n\nexport type RequiredParamKeys<P extends ParamsFormats> = {\n [K in keyof P]: undefined extends FormatOutput<P[K]> ? K : never\n}[keyof P]\n\nexport type OptionalParamKeys<P extends ParamsFormats> = Exclude<keyof P, RequiredParamKeys<P>>\n\nexport type ParamsValues<P extends ParamsFormats> = {\n [K in RequiredParamKeys<P>]: ValueOrAccessor<FormatInput<P[K]>>\n} & {\n [K in OptionalParamKeys<P>]?: ValueOrAccessor<FormatInput<P[K]> | undefined>\n}\n\nexport type ParamsFn<P extends ParamsFormats> = FnWithParams<ParamsValues<P>>\n\ntype PreformatOutput = string | undefined | FnWithAnyParams\n\ntype ComposedParamsFn<P extends ParamsFormats, O> = FnWithParams<ParamsValues<P> & UnionToIntersection<ParamsOf<O>>>\n\ntype Replace = (ctx: FormatContext, text: string, params: Record<string, ValueOrAccessor<ReplacementValue>>) => string\n\n/**\n * Creates a parameter interpolation formatter.\n * @param params - Parameter formatters keyed by placeholder name.\n * @returns Formatter that replaces `{key}` placeholders using provided parameter values.\n */\nexport function params<const P extends ParamsFormats>(\n params: P\n): Format<string | undefined, ParamsFn<P>>\n\n/**\n * Creates a parameter interpolation formatter that first applies another formatter.\n * @param params - Parameter formatters keyed by placeholder name.\n * @param preformat - Formatter applied before replacing placeholders.\n * @returns Formatter that replaces `{key}` placeholders using provided parameter values.\n */\nexport function params<\n const P extends ParamsFormats,\n F = string | undefined,\n O extends PreformatOutput = string | undefined | ParamsFn<P>\n>(\n params: P,\n preformat: Format<F, O>\n): Format<F, ComposedParamsFn<P, O>>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function params<\n const P extends ParamsFormats,\n F = string | undefined,\n O extends PreformatOutput = string | undefined | ParamsFn<P>\n>(\n params: P,\n preformat?: Format<F, O>\n): Format<F, ComposedParamsFn<P, O>> {\n let replaceAll: Replace | undefined\n\n for (const [key, format] of Object.entries(params)) {\n const re = new RegExp(`{${key}}`, 'g')\n const replace: Replace = (ctx, text, params) => text.replace(re, format(ctx, $get(params[key])) as string)\n\n if (replaceAll) {\n const prevReplace = replaceAll\n\n replaceAll = (ctx, text, params) => replace(ctx, prevReplace(ctx, text, params), params)\n } else {\n replaceAll = replace\n }\n }\n\n return (ctx: FormatContext, input?: F) => {\n const preformatted = (preformat ? preformat(ctx, input) : input) as PreformatOutput\n\n if (isFunction(preformatted)) {\n return (params) => {\n const text = preformatted(params)\n\n if (text === undefined || !replaceAll) {\n return text\n }\n\n return replaceAll(ctx, text, params)\n }\n }\n\n if (preformatted === undefined || !replaceAll) {\n return () => preformatted\n }\n\n return params => replaceAll(ctx, preformatted, params)\n }\n}\n"]}