@json-render/directives 0.19.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.
- package/LICENSE +201 -0
- package/README.md +126 -0
- package/dist/index.d.mts +144 -0
- package/dist/index.d.ts +144 -0
- package/dist/index.js +315 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +280 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2025 Vercel Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# @json-render/directives
|
|
2
|
+
|
|
3
|
+
Pre-built custom directives for `@json-render/core`. Drop them into your catalog and renderer to add formatting, math, string manipulation, and i18n to your AI-generated UIs.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @json-render/directives
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { standardDirectives } from '@json-render/directives';
|
|
15
|
+
|
|
16
|
+
// Wire into prompt generation
|
|
17
|
+
const prompt = catalog.prompt({ directives: standardDirectives });
|
|
18
|
+
|
|
19
|
+
// Wire into the renderer
|
|
20
|
+
<JSONUIProvider spec={spec} directives={directives}>
|
|
21
|
+
...
|
|
22
|
+
</JSONUIProvider>
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Available Directives
|
|
26
|
+
|
|
27
|
+
### `$format` — Locale-aware value formatting
|
|
28
|
+
|
|
29
|
+
```json
|
|
30
|
+
{ "$format": "currency", "value": { "$state": "/cart/total" }, "currency": "USD" }
|
|
31
|
+
{ "$format": "date", "value": { "$state": "/user/createdAt" } }
|
|
32
|
+
{ "$format": "number", "value": 1234567, "notation": "compact" }
|
|
33
|
+
{ "$format": "percent", "value": 0.75 }
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Formats: `date`, `currency`, `number`, `percent`. The `value` field accepts any dynamic expression.
|
|
37
|
+
|
|
38
|
+
### `$math` — Arithmetic operations
|
|
39
|
+
|
|
40
|
+
```json
|
|
41
|
+
{ "$math": "add", "a": { "$state": "/subtotal" }, "b": { "$state": "/tax" } }
|
|
42
|
+
{ "$math": "multiply", "a": { "$state": "/price" }, "b": { "$state": "/qty" } }
|
|
43
|
+
{ "$math": "round", "a": 3.7 }
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Operations: `add`, `subtract`, `multiply`, `divide`, `mod`, `min`, `max`, `round`, `floor`, `ceil`, `abs`. Unary ops only use `a`.
|
|
47
|
+
|
|
48
|
+
### `$concat` — String concatenation
|
|
49
|
+
|
|
50
|
+
```json
|
|
51
|
+
{ "$concat": [{ "$state": "/user/firstName" }, " ", { "$state": "/user/lastName" }] }
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Each element is resolved then joined into a single string.
|
|
55
|
+
|
|
56
|
+
### `$count` — Array/string length
|
|
57
|
+
|
|
58
|
+
```json
|
|
59
|
+
{ "$count": { "$state": "/cart/items" } }
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Returns the length of an array or string. Returns `0` for other types.
|
|
63
|
+
|
|
64
|
+
### `$truncate` — Text truncation
|
|
65
|
+
|
|
66
|
+
```json
|
|
67
|
+
{ "$truncate": { "$state": "/post/body" }, "length": 140, "suffix": "..." }
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Default length is 100, default suffix is `"..."`.
|
|
71
|
+
|
|
72
|
+
### `$pluralize` — Singular/plural forms
|
|
73
|
+
|
|
74
|
+
```json
|
|
75
|
+
{ "$pluralize": { "$state": "/cart/itemCount" }, "one": "item", "other": "items", "zero": "no items" }
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Outputs: `"3 items"`, `"1 item"`, or `"no items"`. The `zero` form is optional.
|
|
79
|
+
|
|
80
|
+
### `$join` — Join array elements
|
|
81
|
+
|
|
82
|
+
```json
|
|
83
|
+
{ "$join": { "$state": "/tags" }, "separator": ", " }
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Default separator is `", "`.
|
|
87
|
+
|
|
88
|
+
### `createI18nDirective` — Internationalization
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { createI18nDirective } from '@json-render/directives';
|
|
92
|
+
|
|
93
|
+
const t = createI18nDirective({
|
|
94
|
+
locale: 'en',
|
|
95
|
+
messages: {
|
|
96
|
+
en: { "greeting": "Hello, {{name}}!", "checkout.submit": "Place Order" },
|
|
97
|
+
es: { "greeting": "Hola, {{name}}!", "checkout.submit": "Realizar Pedido" },
|
|
98
|
+
},
|
|
99
|
+
fallbackLocale: 'en',
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
```json
|
|
104
|
+
{ "$t": "checkout.submit" }
|
|
105
|
+
{ "$t": "greeting", "params": { "name": { "$state": "/user/name" } } }
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
This is a factory function because it requires locale configuration at setup time.
|
|
109
|
+
|
|
110
|
+
## Composition
|
|
111
|
+
|
|
112
|
+
Directives compose naturally — each resolver calls `resolvePropValue` on its inputs:
|
|
113
|
+
|
|
114
|
+
```json
|
|
115
|
+
{
|
|
116
|
+
"$format": "currency",
|
|
117
|
+
"value": { "$math": "multiply", "a": { "$state": "/price" }, "b": { "$state": "/qty" } },
|
|
118
|
+
"currency": "USD"
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
This resolves inside-out: `$math` first, then `$format` on the result.
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
Apache-2.0
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import * as _json_render_core from '@json-render/core';
|
|
2
|
+
import { DirectiveDefinition } from '@json-render/core';
|
|
3
|
+
import * as zod_v4_core from 'zod/v4/core';
|
|
4
|
+
import * as zod from 'zod';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
|
|
7
|
+
declare const formatDirective: _json_render_core.DirectiveDefinition<z.ZodObject<{
|
|
8
|
+
$format: z.ZodEnum<{
|
|
9
|
+
number: "number";
|
|
10
|
+
date: "date";
|
|
11
|
+
currency: "currency";
|
|
12
|
+
percent: "percent";
|
|
13
|
+
}>;
|
|
14
|
+
value: z.ZodUnknown;
|
|
15
|
+
locale: z.ZodOptional<z.ZodString>;
|
|
16
|
+
currency: z.ZodOptional<z.ZodString>;
|
|
17
|
+
notation: z.ZodOptional<z.ZodString>;
|
|
18
|
+
style: z.ZodOptional<z.ZodString>;
|
|
19
|
+
options: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
20
|
+
now: z.ZodOptional<z.ZodNumber>;
|
|
21
|
+
}, z.core.$strip>>;
|
|
22
|
+
|
|
23
|
+
declare const mathDirective: _json_render_core.DirectiveDefinition<z.ZodObject<{
|
|
24
|
+
$math: z.ZodEnum<{
|
|
25
|
+
add: "add";
|
|
26
|
+
subtract: "subtract";
|
|
27
|
+
multiply: "multiply";
|
|
28
|
+
divide: "divide";
|
|
29
|
+
mod: "mod";
|
|
30
|
+
min: "min";
|
|
31
|
+
max: "max";
|
|
32
|
+
round: "round";
|
|
33
|
+
floor: "floor";
|
|
34
|
+
ceil: "ceil";
|
|
35
|
+
abs: "abs";
|
|
36
|
+
}>;
|
|
37
|
+
a: z.ZodOptional<z.ZodUnknown>;
|
|
38
|
+
b: z.ZodOptional<z.ZodUnknown>;
|
|
39
|
+
}, z.core.$strip>>;
|
|
40
|
+
|
|
41
|
+
declare const concatDirective: _json_render_core.DirectiveDefinition<z.ZodObject<{
|
|
42
|
+
$concat: z.ZodArray<z.ZodUnknown>;
|
|
43
|
+
}, z.core.$strip>>;
|
|
44
|
+
|
|
45
|
+
declare const countDirective: _json_render_core.DirectiveDefinition<z.ZodObject<{
|
|
46
|
+
$count: z.ZodUnknown;
|
|
47
|
+
}, z.core.$strip>>;
|
|
48
|
+
|
|
49
|
+
declare const truncateDirective: _json_render_core.DirectiveDefinition<z.ZodObject<{
|
|
50
|
+
$truncate: z.ZodUnknown;
|
|
51
|
+
length: z.ZodOptional<z.ZodNumber>;
|
|
52
|
+
suffix: z.ZodOptional<z.ZodString>;
|
|
53
|
+
}, z.core.$strip>>;
|
|
54
|
+
|
|
55
|
+
declare const pluralizeDirective: _json_render_core.DirectiveDefinition<z.ZodObject<{
|
|
56
|
+
$pluralize: z.ZodUnknown;
|
|
57
|
+
zero: z.ZodOptional<z.ZodString>;
|
|
58
|
+
one: z.ZodString;
|
|
59
|
+
other: z.ZodString;
|
|
60
|
+
}, z.core.$strip>>;
|
|
61
|
+
|
|
62
|
+
declare const joinDirective: _json_render_core.DirectiveDefinition<z.ZodObject<{
|
|
63
|
+
$join: z.ZodUnknown;
|
|
64
|
+
separator: z.ZodOptional<z.ZodString>;
|
|
65
|
+
}, z.core.$strip>>;
|
|
66
|
+
|
|
67
|
+
interface I18nConfig {
|
|
68
|
+
/** Current locale (e.g. "en", "es") */
|
|
69
|
+
locale: string;
|
|
70
|
+
/** Map of locale → key → translated string */
|
|
71
|
+
messages: Record<string, Record<string, string>>;
|
|
72
|
+
/** Fallback locale when a key is missing in the current locale */
|
|
73
|
+
fallbackLocale?: string;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Create a `$t` directive for internationalization.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* const t = createI18nDirective({
|
|
81
|
+
* locale: 'en',
|
|
82
|
+
* messages: {
|
|
83
|
+
* en: { "greeting": "Hello, {{name}}!" },
|
|
84
|
+
* es: { "greeting": "Hola, {{name}}!" },
|
|
85
|
+
* },
|
|
86
|
+
* });
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
declare function createI18nDirective(config: I18nConfig): DirectiveDefinition;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* All non-factory directives in a single array.
|
|
93
|
+
* Spread with factory directives as needed:
|
|
94
|
+
* `[...standardDirectives, createI18nDirective(config)]`
|
|
95
|
+
*/
|
|
96
|
+
declare const standardDirectives: (_json_render_core.DirectiveDefinition<zod.ZodObject<{
|
|
97
|
+
$format: zod.ZodEnum<{
|
|
98
|
+
number: "number";
|
|
99
|
+
date: "date";
|
|
100
|
+
currency: "currency";
|
|
101
|
+
percent: "percent";
|
|
102
|
+
}>;
|
|
103
|
+
value: zod.ZodUnknown;
|
|
104
|
+
locale: zod.ZodOptional<zod.ZodString>;
|
|
105
|
+
currency: zod.ZodOptional<zod.ZodString>;
|
|
106
|
+
notation: zod.ZodOptional<zod.ZodString>;
|
|
107
|
+
style: zod.ZodOptional<zod.ZodString>;
|
|
108
|
+
options: zod.ZodOptional<zod.ZodRecord<zod.ZodString, zod.ZodUnknown>>;
|
|
109
|
+
now: zod.ZodOptional<zod.ZodNumber>;
|
|
110
|
+
}, zod_v4_core.$strip>> | _json_render_core.DirectiveDefinition<zod.ZodObject<{
|
|
111
|
+
$math: zod.ZodEnum<{
|
|
112
|
+
add: "add";
|
|
113
|
+
subtract: "subtract";
|
|
114
|
+
multiply: "multiply";
|
|
115
|
+
divide: "divide";
|
|
116
|
+
mod: "mod";
|
|
117
|
+
min: "min";
|
|
118
|
+
max: "max";
|
|
119
|
+
round: "round";
|
|
120
|
+
floor: "floor";
|
|
121
|
+
ceil: "ceil";
|
|
122
|
+
abs: "abs";
|
|
123
|
+
}>;
|
|
124
|
+
a: zod.ZodOptional<zod.ZodUnknown>;
|
|
125
|
+
b: zod.ZodOptional<zod.ZodUnknown>;
|
|
126
|
+
}, zod_v4_core.$strip>> | _json_render_core.DirectiveDefinition<zod.ZodObject<{
|
|
127
|
+
$concat: zod.ZodArray<zod.ZodUnknown>;
|
|
128
|
+
}, zod_v4_core.$strip>> | _json_render_core.DirectiveDefinition<zod.ZodObject<{
|
|
129
|
+
$count: zod.ZodUnknown;
|
|
130
|
+
}, zod_v4_core.$strip>> | _json_render_core.DirectiveDefinition<zod.ZodObject<{
|
|
131
|
+
$truncate: zod.ZodUnknown;
|
|
132
|
+
length: zod.ZodOptional<zod.ZodNumber>;
|
|
133
|
+
suffix: zod.ZodOptional<zod.ZodString>;
|
|
134
|
+
}, zod_v4_core.$strip>> | _json_render_core.DirectiveDefinition<zod.ZodObject<{
|
|
135
|
+
$pluralize: zod.ZodUnknown;
|
|
136
|
+
zero: zod.ZodOptional<zod.ZodString>;
|
|
137
|
+
one: zod.ZodString;
|
|
138
|
+
other: zod.ZodString;
|
|
139
|
+
}, zod_v4_core.$strip>> | _json_render_core.DirectiveDefinition<zod.ZodObject<{
|
|
140
|
+
$join: zod.ZodUnknown;
|
|
141
|
+
separator: zod.ZodOptional<zod.ZodString>;
|
|
142
|
+
}, zod_v4_core.$strip>>)[];
|
|
143
|
+
|
|
144
|
+
export { type I18nConfig, concatDirective, countDirective, createI18nDirective, formatDirective, joinDirective, mathDirective, pluralizeDirective, standardDirectives, truncateDirective };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import * as _json_render_core from '@json-render/core';
|
|
2
|
+
import { DirectiveDefinition } from '@json-render/core';
|
|
3
|
+
import * as zod_v4_core from 'zod/v4/core';
|
|
4
|
+
import * as zod from 'zod';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
|
|
7
|
+
declare const formatDirective: _json_render_core.DirectiveDefinition<z.ZodObject<{
|
|
8
|
+
$format: z.ZodEnum<{
|
|
9
|
+
number: "number";
|
|
10
|
+
date: "date";
|
|
11
|
+
currency: "currency";
|
|
12
|
+
percent: "percent";
|
|
13
|
+
}>;
|
|
14
|
+
value: z.ZodUnknown;
|
|
15
|
+
locale: z.ZodOptional<z.ZodString>;
|
|
16
|
+
currency: z.ZodOptional<z.ZodString>;
|
|
17
|
+
notation: z.ZodOptional<z.ZodString>;
|
|
18
|
+
style: z.ZodOptional<z.ZodString>;
|
|
19
|
+
options: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
20
|
+
now: z.ZodOptional<z.ZodNumber>;
|
|
21
|
+
}, z.core.$strip>>;
|
|
22
|
+
|
|
23
|
+
declare const mathDirective: _json_render_core.DirectiveDefinition<z.ZodObject<{
|
|
24
|
+
$math: z.ZodEnum<{
|
|
25
|
+
add: "add";
|
|
26
|
+
subtract: "subtract";
|
|
27
|
+
multiply: "multiply";
|
|
28
|
+
divide: "divide";
|
|
29
|
+
mod: "mod";
|
|
30
|
+
min: "min";
|
|
31
|
+
max: "max";
|
|
32
|
+
round: "round";
|
|
33
|
+
floor: "floor";
|
|
34
|
+
ceil: "ceil";
|
|
35
|
+
abs: "abs";
|
|
36
|
+
}>;
|
|
37
|
+
a: z.ZodOptional<z.ZodUnknown>;
|
|
38
|
+
b: z.ZodOptional<z.ZodUnknown>;
|
|
39
|
+
}, z.core.$strip>>;
|
|
40
|
+
|
|
41
|
+
declare const concatDirective: _json_render_core.DirectiveDefinition<z.ZodObject<{
|
|
42
|
+
$concat: z.ZodArray<z.ZodUnknown>;
|
|
43
|
+
}, z.core.$strip>>;
|
|
44
|
+
|
|
45
|
+
declare const countDirective: _json_render_core.DirectiveDefinition<z.ZodObject<{
|
|
46
|
+
$count: z.ZodUnknown;
|
|
47
|
+
}, z.core.$strip>>;
|
|
48
|
+
|
|
49
|
+
declare const truncateDirective: _json_render_core.DirectiveDefinition<z.ZodObject<{
|
|
50
|
+
$truncate: z.ZodUnknown;
|
|
51
|
+
length: z.ZodOptional<z.ZodNumber>;
|
|
52
|
+
suffix: z.ZodOptional<z.ZodString>;
|
|
53
|
+
}, z.core.$strip>>;
|
|
54
|
+
|
|
55
|
+
declare const pluralizeDirective: _json_render_core.DirectiveDefinition<z.ZodObject<{
|
|
56
|
+
$pluralize: z.ZodUnknown;
|
|
57
|
+
zero: z.ZodOptional<z.ZodString>;
|
|
58
|
+
one: z.ZodString;
|
|
59
|
+
other: z.ZodString;
|
|
60
|
+
}, z.core.$strip>>;
|
|
61
|
+
|
|
62
|
+
declare const joinDirective: _json_render_core.DirectiveDefinition<z.ZodObject<{
|
|
63
|
+
$join: z.ZodUnknown;
|
|
64
|
+
separator: z.ZodOptional<z.ZodString>;
|
|
65
|
+
}, z.core.$strip>>;
|
|
66
|
+
|
|
67
|
+
interface I18nConfig {
|
|
68
|
+
/** Current locale (e.g. "en", "es") */
|
|
69
|
+
locale: string;
|
|
70
|
+
/** Map of locale → key → translated string */
|
|
71
|
+
messages: Record<string, Record<string, string>>;
|
|
72
|
+
/** Fallback locale when a key is missing in the current locale */
|
|
73
|
+
fallbackLocale?: string;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Create a `$t` directive for internationalization.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* const t = createI18nDirective({
|
|
81
|
+
* locale: 'en',
|
|
82
|
+
* messages: {
|
|
83
|
+
* en: { "greeting": "Hello, {{name}}!" },
|
|
84
|
+
* es: { "greeting": "Hola, {{name}}!" },
|
|
85
|
+
* },
|
|
86
|
+
* });
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
declare function createI18nDirective(config: I18nConfig): DirectiveDefinition;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* All non-factory directives in a single array.
|
|
93
|
+
* Spread with factory directives as needed:
|
|
94
|
+
* `[...standardDirectives, createI18nDirective(config)]`
|
|
95
|
+
*/
|
|
96
|
+
declare const standardDirectives: (_json_render_core.DirectiveDefinition<zod.ZodObject<{
|
|
97
|
+
$format: zod.ZodEnum<{
|
|
98
|
+
number: "number";
|
|
99
|
+
date: "date";
|
|
100
|
+
currency: "currency";
|
|
101
|
+
percent: "percent";
|
|
102
|
+
}>;
|
|
103
|
+
value: zod.ZodUnknown;
|
|
104
|
+
locale: zod.ZodOptional<zod.ZodString>;
|
|
105
|
+
currency: zod.ZodOptional<zod.ZodString>;
|
|
106
|
+
notation: zod.ZodOptional<zod.ZodString>;
|
|
107
|
+
style: zod.ZodOptional<zod.ZodString>;
|
|
108
|
+
options: zod.ZodOptional<zod.ZodRecord<zod.ZodString, zod.ZodUnknown>>;
|
|
109
|
+
now: zod.ZodOptional<zod.ZodNumber>;
|
|
110
|
+
}, zod_v4_core.$strip>> | _json_render_core.DirectiveDefinition<zod.ZodObject<{
|
|
111
|
+
$math: zod.ZodEnum<{
|
|
112
|
+
add: "add";
|
|
113
|
+
subtract: "subtract";
|
|
114
|
+
multiply: "multiply";
|
|
115
|
+
divide: "divide";
|
|
116
|
+
mod: "mod";
|
|
117
|
+
min: "min";
|
|
118
|
+
max: "max";
|
|
119
|
+
round: "round";
|
|
120
|
+
floor: "floor";
|
|
121
|
+
ceil: "ceil";
|
|
122
|
+
abs: "abs";
|
|
123
|
+
}>;
|
|
124
|
+
a: zod.ZodOptional<zod.ZodUnknown>;
|
|
125
|
+
b: zod.ZodOptional<zod.ZodUnknown>;
|
|
126
|
+
}, zod_v4_core.$strip>> | _json_render_core.DirectiveDefinition<zod.ZodObject<{
|
|
127
|
+
$concat: zod.ZodArray<zod.ZodUnknown>;
|
|
128
|
+
}, zod_v4_core.$strip>> | _json_render_core.DirectiveDefinition<zod.ZodObject<{
|
|
129
|
+
$count: zod.ZodUnknown;
|
|
130
|
+
}, zod_v4_core.$strip>> | _json_render_core.DirectiveDefinition<zod.ZodObject<{
|
|
131
|
+
$truncate: zod.ZodUnknown;
|
|
132
|
+
length: zod.ZodOptional<zod.ZodNumber>;
|
|
133
|
+
suffix: zod.ZodOptional<zod.ZodString>;
|
|
134
|
+
}, zod_v4_core.$strip>> | _json_render_core.DirectiveDefinition<zod.ZodObject<{
|
|
135
|
+
$pluralize: zod.ZodUnknown;
|
|
136
|
+
zero: zod.ZodOptional<zod.ZodString>;
|
|
137
|
+
one: zod.ZodString;
|
|
138
|
+
other: zod.ZodString;
|
|
139
|
+
}, zod_v4_core.$strip>> | _json_render_core.DirectiveDefinition<zod.ZodObject<{
|
|
140
|
+
$join: zod.ZodUnknown;
|
|
141
|
+
separator: zod.ZodOptional<zod.ZodString>;
|
|
142
|
+
}, zod_v4_core.$strip>>)[];
|
|
143
|
+
|
|
144
|
+
export { type I18nConfig, concatDirective, countDirective, createI18nDirective, formatDirective, joinDirective, mathDirective, pluralizeDirective, standardDirectives, truncateDirective };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
concatDirective: () => concatDirective,
|
|
24
|
+
countDirective: () => countDirective,
|
|
25
|
+
createI18nDirective: () => createI18nDirective,
|
|
26
|
+
formatDirective: () => formatDirective,
|
|
27
|
+
joinDirective: () => joinDirective,
|
|
28
|
+
mathDirective: () => mathDirective,
|
|
29
|
+
pluralizeDirective: () => pluralizeDirective,
|
|
30
|
+
standardDirectives: () => standardDirectives,
|
|
31
|
+
truncateDirective: () => truncateDirective
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(index_exports);
|
|
34
|
+
|
|
35
|
+
// src/format.ts
|
|
36
|
+
var import_zod = require("zod");
|
|
37
|
+
var import_core = require("@json-render/core");
|
|
38
|
+
var formatDirective = (0, import_core.defineDirective)({
|
|
39
|
+
name: "$format",
|
|
40
|
+
description: 'Locale-aware value formatting (date, currency, number, percent). Supports style: "relative" for relative dates.',
|
|
41
|
+
schema: import_zod.z.object({
|
|
42
|
+
$format: import_zod.z.enum(["date", "currency", "number", "percent"]),
|
|
43
|
+
value: import_zod.z.unknown(),
|
|
44
|
+
locale: import_zod.z.string().optional(),
|
|
45
|
+
currency: import_zod.z.string().optional(),
|
|
46
|
+
notation: import_zod.z.string().optional(),
|
|
47
|
+
style: import_zod.z.string().optional(),
|
|
48
|
+
options: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional(),
|
|
49
|
+
now: import_zod.z.number().optional()
|
|
50
|
+
}),
|
|
51
|
+
resolve(raw, ctx) {
|
|
52
|
+
const value = (0, import_core.resolvePropValue)(raw.value, ctx);
|
|
53
|
+
const locale = raw.locale ?? void 0;
|
|
54
|
+
const extra = raw.options ?? {};
|
|
55
|
+
switch (raw.$format) {
|
|
56
|
+
case "date": {
|
|
57
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
58
|
+
if (raw.style === "relative") {
|
|
59
|
+
const now = raw.now ?? Date.now();
|
|
60
|
+
const diff = now - date.getTime();
|
|
61
|
+
if (diff === 0) return "just now";
|
|
62
|
+
const absDiff = Math.abs(diff);
|
|
63
|
+
const suffix = diff > 0 ? "ago" : "from now";
|
|
64
|
+
const seconds = Math.floor(absDiff / 1e3);
|
|
65
|
+
const minutes = Math.floor(seconds / 60);
|
|
66
|
+
const hours = Math.floor(minutes / 60);
|
|
67
|
+
const days = Math.floor(hours / 24);
|
|
68
|
+
if (days > 0) return `${days}d ${suffix}`;
|
|
69
|
+
if (hours > 0) return `${hours}h ${suffix}`;
|
|
70
|
+
if (minutes > 0) return `${minutes}m ${suffix}`;
|
|
71
|
+
return `${seconds}s ${suffix}`;
|
|
72
|
+
}
|
|
73
|
+
return new Intl.DateTimeFormat(
|
|
74
|
+
locale,
|
|
75
|
+
extra
|
|
76
|
+
).format(date);
|
|
77
|
+
}
|
|
78
|
+
case "currency":
|
|
79
|
+
return new Intl.NumberFormat(locale, {
|
|
80
|
+
style: "currency",
|
|
81
|
+
currency: raw.currency ?? "USD",
|
|
82
|
+
...extra
|
|
83
|
+
}).format(value);
|
|
84
|
+
case "number":
|
|
85
|
+
return new Intl.NumberFormat(locale, {
|
|
86
|
+
...raw.notation ? {
|
|
87
|
+
notation: raw.notation
|
|
88
|
+
} : {},
|
|
89
|
+
...extra
|
|
90
|
+
}).format(value);
|
|
91
|
+
case "percent":
|
|
92
|
+
return new Intl.NumberFormat(locale, {
|
|
93
|
+
style: "percent",
|
|
94
|
+
...extra
|
|
95
|
+
}).format(value);
|
|
96
|
+
default:
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// src/math.ts
|
|
103
|
+
var import_zod2 = require("zod");
|
|
104
|
+
var import_core2 = require("@json-render/core");
|
|
105
|
+
function toNum(v) {
|
|
106
|
+
if (v == null) return 0;
|
|
107
|
+
const n = Number(v);
|
|
108
|
+
if (Number.isNaN(n)) {
|
|
109
|
+
console.warn(`$math: non-numeric value coerced to 0:`, v);
|
|
110
|
+
return 0;
|
|
111
|
+
}
|
|
112
|
+
return n;
|
|
113
|
+
}
|
|
114
|
+
var mathDirective = (0, import_core2.defineDirective)({
|
|
115
|
+
name: "$math",
|
|
116
|
+
description: 'Arithmetic operations. Unary ops (round, floor, ceil, abs) only use "a". Division by zero returns 0.',
|
|
117
|
+
schema: import_zod2.z.object({
|
|
118
|
+
$math: import_zod2.z.enum([
|
|
119
|
+
"add",
|
|
120
|
+
"subtract",
|
|
121
|
+
"multiply",
|
|
122
|
+
"divide",
|
|
123
|
+
"mod",
|
|
124
|
+
"min",
|
|
125
|
+
"max",
|
|
126
|
+
"round",
|
|
127
|
+
"floor",
|
|
128
|
+
"ceil",
|
|
129
|
+
"abs"
|
|
130
|
+
]),
|
|
131
|
+
a: import_zod2.z.unknown().optional(),
|
|
132
|
+
b: import_zod2.z.unknown().optional()
|
|
133
|
+
}),
|
|
134
|
+
resolve(raw, ctx) {
|
|
135
|
+
const a = toNum((0, import_core2.resolvePropValue)(raw.a, ctx));
|
|
136
|
+
const b = toNum((0, import_core2.resolvePropValue)(raw.b, ctx));
|
|
137
|
+
switch (raw.$math) {
|
|
138
|
+
case "add":
|
|
139
|
+
return a + b;
|
|
140
|
+
case "subtract":
|
|
141
|
+
return a - b;
|
|
142
|
+
case "multiply":
|
|
143
|
+
return a * b;
|
|
144
|
+
case "divide":
|
|
145
|
+
return b !== 0 ? a / b : 0;
|
|
146
|
+
case "mod":
|
|
147
|
+
return b !== 0 ? a % b : 0;
|
|
148
|
+
case "min":
|
|
149
|
+
return Math.min(a, b);
|
|
150
|
+
case "max":
|
|
151
|
+
return Math.max(a, b);
|
|
152
|
+
case "round":
|
|
153
|
+
return Math.round(a);
|
|
154
|
+
case "floor":
|
|
155
|
+
return Math.floor(a);
|
|
156
|
+
case "ceil":
|
|
157
|
+
return Math.ceil(a);
|
|
158
|
+
case "abs":
|
|
159
|
+
return Math.abs(a);
|
|
160
|
+
default:
|
|
161
|
+
return a;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// src/concat.ts
|
|
167
|
+
var import_zod3 = require("zod");
|
|
168
|
+
var import_core3 = require("@json-render/core");
|
|
169
|
+
var concatDirective = (0, import_core3.defineDirective)({
|
|
170
|
+
name: "$concat",
|
|
171
|
+
description: "Concatenate multiple dynamic values into a string.",
|
|
172
|
+
schema: import_zod3.z.object({
|
|
173
|
+
$concat: import_zod3.z.array(import_zod3.z.unknown())
|
|
174
|
+
}),
|
|
175
|
+
resolve(raw, ctx) {
|
|
176
|
+
return raw.$concat.map((part) => {
|
|
177
|
+
const resolved = (0, import_core3.resolvePropValue)(part, ctx);
|
|
178
|
+
return resolved != null ? String(resolved) : "";
|
|
179
|
+
}).join("");
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// src/count.ts
|
|
184
|
+
var import_zod4 = require("zod");
|
|
185
|
+
var import_core4 = require("@json-render/core");
|
|
186
|
+
var countDirective = (0, import_core4.defineDirective)({
|
|
187
|
+
name: "$count",
|
|
188
|
+
description: "Get the length of an array or string.",
|
|
189
|
+
schema: import_zod4.z.object({
|
|
190
|
+
$count: import_zod4.z.unknown()
|
|
191
|
+
}),
|
|
192
|
+
resolve(raw, ctx) {
|
|
193
|
+
const resolved = (0, import_core4.resolvePropValue)(raw.$count, ctx);
|
|
194
|
+
if (Array.isArray(resolved)) return resolved.length;
|
|
195
|
+
if (typeof resolved === "string") return resolved.length;
|
|
196
|
+
return 0;
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// src/truncate.ts
|
|
201
|
+
var import_zod5 = require("zod");
|
|
202
|
+
var import_core5 = require("@json-render/core");
|
|
203
|
+
var truncateDirective = (0, import_core5.defineDirective)({
|
|
204
|
+
name: "$truncate",
|
|
205
|
+
description: "Truncate text to a max length with a suffix.",
|
|
206
|
+
schema: import_zod5.z.object({
|
|
207
|
+
$truncate: import_zod5.z.unknown(),
|
|
208
|
+
length: import_zod5.z.number().optional(),
|
|
209
|
+
suffix: import_zod5.z.string().optional()
|
|
210
|
+
}),
|
|
211
|
+
resolve(raw, ctx) {
|
|
212
|
+
const resolved = (0, import_core5.resolvePropValue)(raw.$truncate, ctx);
|
|
213
|
+
const text = resolved != null ? String(resolved) : "";
|
|
214
|
+
const maxLength = raw.length ?? 100;
|
|
215
|
+
const suffix = raw.suffix ?? "...";
|
|
216
|
+
if (text.length <= maxLength) return text;
|
|
217
|
+
return text.slice(0, maxLength) + suffix;
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// src/pluralize.ts
|
|
222
|
+
var import_zod6 = require("zod");
|
|
223
|
+
var import_core6 = require("@json-render/core");
|
|
224
|
+
var pluralizeDirective = (0, import_core6.defineDirective)({
|
|
225
|
+
name: "$pluralize",
|
|
226
|
+
description: 'Select singular/plural/zero form based on count. Output: "3 items", "1 item", or "no items".',
|
|
227
|
+
schema: import_zod6.z.object({
|
|
228
|
+
$pluralize: import_zod6.z.unknown(),
|
|
229
|
+
zero: import_zod6.z.string().optional(),
|
|
230
|
+
one: import_zod6.z.string(),
|
|
231
|
+
other: import_zod6.z.string()
|
|
232
|
+
}),
|
|
233
|
+
resolve(raw, ctx) {
|
|
234
|
+
const resolved = (0, import_core6.resolvePropValue)(raw.$pluralize, ctx);
|
|
235
|
+
const n = Number(resolved);
|
|
236
|
+
const count = Number.isNaN(n) ? 0 : n;
|
|
237
|
+
if (count === 0 && raw.zero != null) return raw.zero;
|
|
238
|
+
if (count === 1) return `${count} ${raw.one}`;
|
|
239
|
+
return `${count} ${raw.other}`;
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
// src/join.ts
|
|
244
|
+
var import_zod7 = require("zod");
|
|
245
|
+
var import_core7 = require("@json-render/core");
|
|
246
|
+
var joinDirective = (0, import_core7.defineDirective)({
|
|
247
|
+
name: "$join",
|
|
248
|
+
description: "Join array elements with a separator.",
|
|
249
|
+
schema: import_zod7.z.object({
|
|
250
|
+
$join: import_zod7.z.unknown(),
|
|
251
|
+
separator: import_zod7.z.string().optional()
|
|
252
|
+
}),
|
|
253
|
+
resolve(raw, ctx) {
|
|
254
|
+
const resolved = (0, import_core7.resolvePropValue)(raw.$join, ctx);
|
|
255
|
+
const separator = raw.separator ?? ", ";
|
|
256
|
+
if (Array.isArray(resolved)) {
|
|
257
|
+
return resolved.map((item) => item != null ? String(item) : "").join(separator);
|
|
258
|
+
}
|
|
259
|
+
return resolved != null ? String(resolved) : "";
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
// src/i18n.ts
|
|
264
|
+
var import_zod8 = require("zod");
|
|
265
|
+
var import_core8 = require("@json-render/core");
|
|
266
|
+
function createI18nDirective(config) {
|
|
267
|
+
return (0, import_core8.defineDirective)({
|
|
268
|
+
name: "$t",
|
|
269
|
+
description: "Translated text with {{param}} interpolation.",
|
|
270
|
+
schema: import_zod8.z.object({
|
|
271
|
+
$t: import_zod8.z.string(),
|
|
272
|
+
params: import_zod8.z.record(import_zod8.z.string(), import_zod8.z.unknown()).optional()
|
|
273
|
+
}),
|
|
274
|
+
resolve(raw, ctx) {
|
|
275
|
+
const key = raw.$t;
|
|
276
|
+
const localeMessages = config.messages[config.locale];
|
|
277
|
+
const fallbackMessages = config.fallbackLocale ? config.messages[config.fallbackLocale] : void 0;
|
|
278
|
+
let template = localeMessages?.[key] ?? fallbackMessages?.[key] ?? key;
|
|
279
|
+
if (raw.params) {
|
|
280
|
+
for (const [paramKey, paramValue] of Object.entries(raw.params)) {
|
|
281
|
+
const resolved = (0, import_core8.resolvePropValue)(paramValue, ctx);
|
|
282
|
+
template = template.replace(
|
|
283
|
+
new RegExp(`\\{\\{${paramKey}\\}\\}`, "g"),
|
|
284
|
+
resolved != null ? String(resolved) : ""
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return template;
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// src/index.ts
|
|
294
|
+
var standardDirectives = [
|
|
295
|
+
formatDirective,
|
|
296
|
+
mathDirective,
|
|
297
|
+
concatDirective,
|
|
298
|
+
countDirective,
|
|
299
|
+
truncateDirective,
|
|
300
|
+
pluralizeDirective,
|
|
301
|
+
joinDirective
|
|
302
|
+
];
|
|
303
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
304
|
+
0 && (module.exports = {
|
|
305
|
+
concatDirective,
|
|
306
|
+
countDirective,
|
|
307
|
+
createI18nDirective,
|
|
308
|
+
formatDirective,
|
|
309
|
+
joinDirective,
|
|
310
|
+
mathDirective,
|
|
311
|
+
pluralizeDirective,
|
|
312
|
+
standardDirectives,
|
|
313
|
+
truncateDirective
|
|
314
|
+
});
|
|
315
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/format.ts","../src/math.ts","../src/concat.ts","../src/count.ts","../src/truncate.ts","../src/pluralize.ts","../src/join.ts","../src/i18n.ts"],"sourcesContent":["export { formatDirective } from \"./format\";\nexport { mathDirective } from \"./math\";\nexport { concatDirective } from \"./concat\";\nexport { countDirective } from \"./count\";\nexport { truncateDirective } from \"./truncate\";\nexport { pluralizeDirective } from \"./pluralize\";\nexport { joinDirective } from \"./join\";\nexport { createI18nDirective, type I18nConfig } from \"./i18n\";\n\nimport { formatDirective } from \"./format\";\nimport { mathDirective } from \"./math\";\nimport { concatDirective } from \"./concat\";\nimport { countDirective } from \"./count\";\nimport { truncateDirective } from \"./truncate\";\nimport { pluralizeDirective } from \"./pluralize\";\nimport { joinDirective } from \"./join\";\n\n/**\n * All non-factory directives in a single array.\n * Spread with factory directives as needed:\n * `[...standardDirectives, createI18nDirective(config)]`\n */\nexport const standardDirectives = [\n formatDirective,\n mathDirective,\n concatDirective,\n countDirective,\n truncateDirective,\n pluralizeDirective,\n joinDirective,\n];\n","import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\n\nexport const formatDirective = defineDirective({\n name: \"$format\",\n description:\n 'Locale-aware value formatting (date, currency, number, percent). Supports style: \"relative\" for relative dates.',\n schema: z.object({\n $format: z.enum([\"date\", \"currency\", \"number\", \"percent\"]),\n value: z.unknown(),\n locale: z.string().optional(),\n currency: z.string().optional(),\n notation: z.string().optional(),\n style: z.string().optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n now: z.number().optional(),\n }),\n resolve(raw, ctx) {\n const value = resolvePropValue(raw.value, ctx);\n const locale = raw.locale ?? undefined;\n const extra = raw.options ?? {};\n\n switch (raw.$format) {\n case \"date\": {\n const date =\n value instanceof Date ? value : new Date(value as string | number);\n if (raw.style === \"relative\") {\n const now = raw.now ?? Date.now();\n const diff = now - date.getTime();\n if (diff === 0) return \"just now\";\n const absDiff = Math.abs(diff);\n const suffix = diff > 0 ? \"ago\" : \"from now\";\n const seconds = Math.floor(absDiff / 1000);\n const minutes = Math.floor(seconds / 60);\n const hours = Math.floor(minutes / 60);\n const days = Math.floor(hours / 24);\n if (days > 0) return `${days}d ${suffix}`;\n if (hours > 0) return `${hours}h ${suffix}`;\n if (minutes > 0) return `${minutes}m ${suffix}`;\n return `${seconds}s ${suffix}`;\n }\n return new Intl.DateTimeFormat(\n locale,\n extra as Intl.DateTimeFormatOptions,\n ).format(date);\n }\n case \"currency\":\n return new Intl.NumberFormat(locale, {\n style: \"currency\",\n currency: raw.currency ?? \"USD\",\n ...(extra as Intl.NumberFormatOptions),\n }).format(value as number);\n case \"number\":\n return new Intl.NumberFormat(locale, {\n ...(raw.notation\n ? {\n notation: raw.notation as Intl.NumberFormatOptions[\"notation\"],\n }\n : {}),\n ...(extra as Intl.NumberFormatOptions),\n }).format(value as number);\n case \"percent\":\n return new Intl.NumberFormat(locale, {\n style: \"percent\",\n ...(extra as Intl.NumberFormatOptions),\n }).format(value as number);\n default:\n return value;\n }\n },\n});\n","import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\n\nfunction toNum(v: unknown): number {\n if (v == null) return 0;\n const n = Number(v);\n if (Number.isNaN(n)) {\n console.warn(`$math: non-numeric value coerced to 0:`, v);\n return 0;\n }\n return n;\n}\n\nexport const mathDirective = defineDirective({\n name: \"$math\",\n description:\n 'Arithmetic operations. Unary ops (round, floor, ceil, abs) only use \"a\". Division by zero returns 0.',\n schema: z.object({\n $math: z.enum([\n \"add\",\n \"subtract\",\n \"multiply\",\n \"divide\",\n \"mod\",\n \"min\",\n \"max\",\n \"round\",\n \"floor\",\n \"ceil\",\n \"abs\",\n ]),\n a: z.unknown().optional(),\n b: z.unknown().optional(),\n }),\n resolve(raw, ctx) {\n const a = toNum(resolvePropValue(raw.a, ctx));\n const b = toNum(resolvePropValue(raw.b, ctx));\n\n switch (raw.$math) {\n case \"add\":\n return a + b;\n case \"subtract\":\n return a - b;\n case \"multiply\":\n return a * b;\n case \"divide\":\n return b !== 0 ? a / b : 0;\n case \"mod\":\n return b !== 0 ? a % b : 0;\n case \"min\":\n return Math.min(a, b);\n case \"max\":\n return Math.max(a, b);\n case \"round\":\n return Math.round(a);\n case \"floor\":\n return Math.floor(a);\n case \"ceil\":\n return Math.ceil(a);\n case \"abs\":\n return Math.abs(a);\n default:\n return a;\n }\n },\n});\n","import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\n\nexport const concatDirective = defineDirective({\n name: \"$concat\",\n description: \"Concatenate multiple dynamic values into a string.\",\n schema: z.object({\n $concat: z.array(z.unknown()),\n }),\n resolve(raw, ctx) {\n return raw.$concat\n .map((part) => {\n const resolved = resolvePropValue(part, ctx);\n return resolved != null ? String(resolved) : \"\";\n })\n .join(\"\");\n },\n});\n","import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\n\nexport const countDirective = defineDirective({\n name: \"$count\",\n description: \"Get the length of an array or string.\",\n schema: z.object({\n $count: z.unknown(),\n }),\n resolve(raw, ctx) {\n const resolved = resolvePropValue(raw.$count, ctx);\n if (Array.isArray(resolved)) return resolved.length;\n if (typeof resolved === \"string\") return resolved.length;\n return 0;\n },\n});\n","import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\n\nexport const truncateDirective = defineDirective({\n name: \"$truncate\",\n description: \"Truncate text to a max length with a suffix.\",\n schema: z.object({\n $truncate: z.unknown(),\n length: z.number().optional(),\n suffix: z.string().optional(),\n }),\n resolve(raw, ctx) {\n const resolved = resolvePropValue(raw.$truncate, ctx);\n const text = resolved != null ? String(resolved) : \"\";\n const maxLength = raw.length ?? 100;\n const suffix = raw.suffix ?? \"...\";\n\n if (text.length <= maxLength) return text;\n return text.slice(0, maxLength) + suffix;\n },\n});\n","import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\n\nexport const pluralizeDirective = defineDirective({\n name: \"$pluralize\",\n description:\n 'Select singular/plural/zero form based on count. Output: \"3 items\", \"1 item\", or \"no items\".',\n schema: z.object({\n $pluralize: z.unknown(),\n zero: z.string().optional(),\n one: z.string(),\n other: z.string(),\n }),\n resolve(raw, ctx) {\n const resolved = resolvePropValue(raw.$pluralize, ctx);\n const n = Number(resolved);\n const count = Number.isNaN(n) ? 0 : n;\n\n if (count === 0 && raw.zero != null) return raw.zero;\n if (count === 1) return `${count} ${raw.one}`;\n return `${count} ${raw.other}`;\n },\n});\n","import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\n\nexport const joinDirective = defineDirective({\n name: \"$join\",\n description: \"Join array elements with a separator.\",\n schema: z.object({\n $join: z.unknown(),\n separator: z.string().optional(),\n }),\n resolve(raw, ctx) {\n const resolved = resolvePropValue(raw.$join, ctx);\n const separator = raw.separator ?? \", \";\n\n if (Array.isArray(resolved)) {\n return resolved\n .map((item) => (item != null ? String(item) : \"\"))\n .join(separator);\n }\n return resolved != null ? String(resolved) : \"\";\n },\n});\n","import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\nimport type { DirectiveDefinition } from \"@json-render/core\";\n\nexport interface I18nConfig {\n /** Current locale (e.g. \"en\", \"es\") */\n locale: string;\n /** Map of locale → key → translated string */\n messages: Record<string, Record<string, string>>;\n /** Fallback locale when a key is missing in the current locale */\n fallbackLocale?: string;\n}\n\n/**\n * Create a `$t` directive for internationalization.\n *\n * @example\n * ```ts\n * const t = createI18nDirective({\n * locale: 'en',\n * messages: {\n * en: { \"greeting\": \"Hello, {{name}}!\" },\n * es: { \"greeting\": \"Hola, {{name}}!\" },\n * },\n * });\n * ```\n */\nexport function createI18nDirective(config: I18nConfig): DirectiveDefinition {\n return defineDirective({\n name: \"$t\",\n description: \"Translated text with {{param}} interpolation.\",\n schema: z.object({\n $t: z.string(),\n params: z.record(z.string(), z.unknown()).optional(),\n }),\n resolve(raw, ctx) {\n const key = raw.$t;\n\n const localeMessages = config.messages[config.locale];\n const fallbackMessages = config.fallbackLocale\n ? config.messages[config.fallbackLocale]\n : undefined;\n let template = localeMessages?.[key] ?? fallbackMessages?.[key] ?? key;\n\n if (raw.params) {\n for (const [paramKey, paramValue] of Object.entries(raw.params)) {\n const resolved = resolvePropValue(paramValue, ctx);\n template = template.replace(\n new RegExp(`\\\\{\\\\{${paramKey}\\\\}\\\\}`, \"g\"),\n resolved != null ? String(resolved) : \"\",\n );\n }\n }\n\n return template;\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,iBAAkB;AAClB,kBAAkD;AAE3C,IAAM,sBAAkB,6BAAgB;AAAA,EAC7C,MAAM;AAAA,EACN,aACE;AAAA,EACF,QAAQ,aAAE,OAAO;AAAA,IACf,SAAS,aAAE,KAAK,CAAC,QAAQ,YAAY,UAAU,SAAS,CAAC;AAAA,IACzD,OAAO,aAAE,QAAQ;AAAA,IACjB,QAAQ,aAAE,OAAO,EAAE,SAAS;AAAA,IAC5B,UAAU,aAAE,OAAO,EAAE,SAAS;AAAA,IAC9B,UAAU,aAAE,OAAO,EAAE,SAAS;AAAA,IAC9B,OAAO,aAA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jD,qBAAW,SAAS;AAAA,YAClB,IAAI,OAAO,SAAS,QAAQ,UAAU,GAAG;AAAA,YACzC,YAAY,OAAO,OAAO,QAAQ,IAAI;AAAA,UACxC;AAAA,QACF;AAAA,MACF;AAEA,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AACH;;;ARnCO,IAAM,qBAAqB;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;","names":["import_zod","import_core","import_zod","import_core","import_zod","import_core","import_zod","import_core","import_zod","import_core","import_zod","import_core","import_zod","import_core"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
// src/format.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { defineDirective, resolvePropValue } from "@json-render/core";
|
|
4
|
+
var formatDirective = defineDirective({
|
|
5
|
+
name: "$format",
|
|
6
|
+
description: 'Locale-aware value formatting (date, currency, number, percent). Supports style: "relative" for relative dates.',
|
|
7
|
+
schema: z.object({
|
|
8
|
+
$format: z.enum(["date", "currency", "number", "percent"]),
|
|
9
|
+
value: z.unknown(),
|
|
10
|
+
locale: z.string().optional(),
|
|
11
|
+
currency: z.string().optional(),
|
|
12
|
+
notation: z.string().optional(),
|
|
13
|
+
style: z.string().optional(),
|
|
14
|
+
options: z.record(z.string(), z.unknown()).optional(),
|
|
15
|
+
now: z.number().optional()
|
|
16
|
+
}),
|
|
17
|
+
resolve(raw, ctx) {
|
|
18
|
+
const value = resolvePropValue(raw.value, ctx);
|
|
19
|
+
const locale = raw.locale ?? void 0;
|
|
20
|
+
const extra = raw.options ?? {};
|
|
21
|
+
switch (raw.$format) {
|
|
22
|
+
case "date": {
|
|
23
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
24
|
+
if (raw.style === "relative") {
|
|
25
|
+
const now = raw.now ?? Date.now();
|
|
26
|
+
const diff = now - date.getTime();
|
|
27
|
+
if (diff === 0) return "just now";
|
|
28
|
+
const absDiff = Math.abs(diff);
|
|
29
|
+
const suffix = diff > 0 ? "ago" : "from now";
|
|
30
|
+
const seconds = Math.floor(absDiff / 1e3);
|
|
31
|
+
const minutes = Math.floor(seconds / 60);
|
|
32
|
+
const hours = Math.floor(minutes / 60);
|
|
33
|
+
const days = Math.floor(hours / 24);
|
|
34
|
+
if (days > 0) return `${days}d ${suffix}`;
|
|
35
|
+
if (hours > 0) return `${hours}h ${suffix}`;
|
|
36
|
+
if (minutes > 0) return `${minutes}m ${suffix}`;
|
|
37
|
+
return `${seconds}s ${suffix}`;
|
|
38
|
+
}
|
|
39
|
+
return new Intl.DateTimeFormat(
|
|
40
|
+
locale,
|
|
41
|
+
extra
|
|
42
|
+
).format(date);
|
|
43
|
+
}
|
|
44
|
+
case "currency":
|
|
45
|
+
return new Intl.NumberFormat(locale, {
|
|
46
|
+
style: "currency",
|
|
47
|
+
currency: raw.currency ?? "USD",
|
|
48
|
+
...extra
|
|
49
|
+
}).format(value);
|
|
50
|
+
case "number":
|
|
51
|
+
return new Intl.NumberFormat(locale, {
|
|
52
|
+
...raw.notation ? {
|
|
53
|
+
notation: raw.notation
|
|
54
|
+
} : {},
|
|
55
|
+
...extra
|
|
56
|
+
}).format(value);
|
|
57
|
+
case "percent":
|
|
58
|
+
return new Intl.NumberFormat(locale, {
|
|
59
|
+
style: "percent",
|
|
60
|
+
...extra
|
|
61
|
+
}).format(value);
|
|
62
|
+
default:
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// src/math.ts
|
|
69
|
+
import { z as z2 } from "zod";
|
|
70
|
+
import { defineDirective as defineDirective2, resolvePropValue as resolvePropValue2 } from "@json-render/core";
|
|
71
|
+
function toNum(v) {
|
|
72
|
+
if (v == null) return 0;
|
|
73
|
+
const n = Number(v);
|
|
74
|
+
if (Number.isNaN(n)) {
|
|
75
|
+
console.warn(`$math: non-numeric value coerced to 0:`, v);
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
return n;
|
|
79
|
+
}
|
|
80
|
+
var mathDirective = defineDirective2({
|
|
81
|
+
name: "$math",
|
|
82
|
+
description: 'Arithmetic operations. Unary ops (round, floor, ceil, abs) only use "a". Division by zero returns 0.',
|
|
83
|
+
schema: z2.object({
|
|
84
|
+
$math: z2.enum([
|
|
85
|
+
"add",
|
|
86
|
+
"subtract",
|
|
87
|
+
"multiply",
|
|
88
|
+
"divide",
|
|
89
|
+
"mod",
|
|
90
|
+
"min",
|
|
91
|
+
"max",
|
|
92
|
+
"round",
|
|
93
|
+
"floor",
|
|
94
|
+
"ceil",
|
|
95
|
+
"abs"
|
|
96
|
+
]),
|
|
97
|
+
a: z2.unknown().optional(),
|
|
98
|
+
b: z2.unknown().optional()
|
|
99
|
+
}),
|
|
100
|
+
resolve(raw, ctx) {
|
|
101
|
+
const a = toNum(resolvePropValue2(raw.a, ctx));
|
|
102
|
+
const b = toNum(resolvePropValue2(raw.b, ctx));
|
|
103
|
+
switch (raw.$math) {
|
|
104
|
+
case "add":
|
|
105
|
+
return a + b;
|
|
106
|
+
case "subtract":
|
|
107
|
+
return a - b;
|
|
108
|
+
case "multiply":
|
|
109
|
+
return a * b;
|
|
110
|
+
case "divide":
|
|
111
|
+
return b !== 0 ? a / b : 0;
|
|
112
|
+
case "mod":
|
|
113
|
+
return b !== 0 ? a % b : 0;
|
|
114
|
+
case "min":
|
|
115
|
+
return Math.min(a, b);
|
|
116
|
+
case "max":
|
|
117
|
+
return Math.max(a, b);
|
|
118
|
+
case "round":
|
|
119
|
+
return Math.round(a);
|
|
120
|
+
case "floor":
|
|
121
|
+
return Math.floor(a);
|
|
122
|
+
case "ceil":
|
|
123
|
+
return Math.ceil(a);
|
|
124
|
+
case "abs":
|
|
125
|
+
return Math.abs(a);
|
|
126
|
+
default:
|
|
127
|
+
return a;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// src/concat.ts
|
|
133
|
+
import { z as z3 } from "zod";
|
|
134
|
+
import { defineDirective as defineDirective3, resolvePropValue as resolvePropValue3 } from "@json-render/core";
|
|
135
|
+
var concatDirective = defineDirective3({
|
|
136
|
+
name: "$concat",
|
|
137
|
+
description: "Concatenate multiple dynamic values into a string.",
|
|
138
|
+
schema: z3.object({
|
|
139
|
+
$concat: z3.array(z3.unknown())
|
|
140
|
+
}),
|
|
141
|
+
resolve(raw, ctx) {
|
|
142
|
+
return raw.$concat.map((part) => {
|
|
143
|
+
const resolved = resolvePropValue3(part, ctx);
|
|
144
|
+
return resolved != null ? String(resolved) : "";
|
|
145
|
+
}).join("");
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// src/count.ts
|
|
150
|
+
import { z as z4 } from "zod";
|
|
151
|
+
import { defineDirective as defineDirective4, resolvePropValue as resolvePropValue4 } from "@json-render/core";
|
|
152
|
+
var countDirective = defineDirective4({
|
|
153
|
+
name: "$count",
|
|
154
|
+
description: "Get the length of an array or string.",
|
|
155
|
+
schema: z4.object({
|
|
156
|
+
$count: z4.unknown()
|
|
157
|
+
}),
|
|
158
|
+
resolve(raw, ctx) {
|
|
159
|
+
const resolved = resolvePropValue4(raw.$count, ctx);
|
|
160
|
+
if (Array.isArray(resolved)) return resolved.length;
|
|
161
|
+
if (typeof resolved === "string") return resolved.length;
|
|
162
|
+
return 0;
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// src/truncate.ts
|
|
167
|
+
import { z as z5 } from "zod";
|
|
168
|
+
import { defineDirective as defineDirective5, resolvePropValue as resolvePropValue5 } from "@json-render/core";
|
|
169
|
+
var truncateDirective = defineDirective5({
|
|
170
|
+
name: "$truncate",
|
|
171
|
+
description: "Truncate text to a max length with a suffix.",
|
|
172
|
+
schema: z5.object({
|
|
173
|
+
$truncate: z5.unknown(),
|
|
174
|
+
length: z5.number().optional(),
|
|
175
|
+
suffix: z5.string().optional()
|
|
176
|
+
}),
|
|
177
|
+
resolve(raw, ctx) {
|
|
178
|
+
const resolved = resolvePropValue5(raw.$truncate, ctx);
|
|
179
|
+
const text = resolved != null ? String(resolved) : "";
|
|
180
|
+
const maxLength = raw.length ?? 100;
|
|
181
|
+
const suffix = raw.suffix ?? "...";
|
|
182
|
+
if (text.length <= maxLength) return text;
|
|
183
|
+
return text.slice(0, maxLength) + suffix;
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
// src/pluralize.ts
|
|
188
|
+
import { z as z6 } from "zod";
|
|
189
|
+
import { defineDirective as defineDirective6, resolvePropValue as resolvePropValue6 } from "@json-render/core";
|
|
190
|
+
var pluralizeDirective = defineDirective6({
|
|
191
|
+
name: "$pluralize",
|
|
192
|
+
description: 'Select singular/plural/zero form based on count. Output: "3 items", "1 item", or "no items".',
|
|
193
|
+
schema: z6.object({
|
|
194
|
+
$pluralize: z6.unknown(),
|
|
195
|
+
zero: z6.string().optional(),
|
|
196
|
+
one: z6.string(),
|
|
197
|
+
other: z6.string()
|
|
198
|
+
}),
|
|
199
|
+
resolve(raw, ctx) {
|
|
200
|
+
const resolved = resolvePropValue6(raw.$pluralize, ctx);
|
|
201
|
+
const n = Number(resolved);
|
|
202
|
+
const count = Number.isNaN(n) ? 0 : n;
|
|
203
|
+
if (count === 0 && raw.zero != null) return raw.zero;
|
|
204
|
+
if (count === 1) return `${count} ${raw.one}`;
|
|
205
|
+
return `${count} ${raw.other}`;
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
// src/join.ts
|
|
210
|
+
import { z as z7 } from "zod";
|
|
211
|
+
import { defineDirective as defineDirective7, resolvePropValue as resolvePropValue7 } from "@json-render/core";
|
|
212
|
+
var joinDirective = defineDirective7({
|
|
213
|
+
name: "$join",
|
|
214
|
+
description: "Join array elements with a separator.",
|
|
215
|
+
schema: z7.object({
|
|
216
|
+
$join: z7.unknown(),
|
|
217
|
+
separator: z7.string().optional()
|
|
218
|
+
}),
|
|
219
|
+
resolve(raw, ctx) {
|
|
220
|
+
const resolved = resolvePropValue7(raw.$join, ctx);
|
|
221
|
+
const separator = raw.separator ?? ", ";
|
|
222
|
+
if (Array.isArray(resolved)) {
|
|
223
|
+
return resolved.map((item) => item != null ? String(item) : "").join(separator);
|
|
224
|
+
}
|
|
225
|
+
return resolved != null ? String(resolved) : "";
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// src/i18n.ts
|
|
230
|
+
import { z as z8 } from "zod";
|
|
231
|
+
import { defineDirective as defineDirective8, resolvePropValue as resolvePropValue8 } from "@json-render/core";
|
|
232
|
+
function createI18nDirective(config) {
|
|
233
|
+
return defineDirective8({
|
|
234
|
+
name: "$t",
|
|
235
|
+
description: "Translated text with {{param}} interpolation.",
|
|
236
|
+
schema: z8.object({
|
|
237
|
+
$t: z8.string(),
|
|
238
|
+
params: z8.record(z8.string(), z8.unknown()).optional()
|
|
239
|
+
}),
|
|
240
|
+
resolve(raw, ctx) {
|
|
241
|
+
const key = raw.$t;
|
|
242
|
+
const localeMessages = config.messages[config.locale];
|
|
243
|
+
const fallbackMessages = config.fallbackLocale ? config.messages[config.fallbackLocale] : void 0;
|
|
244
|
+
let template = localeMessages?.[key] ?? fallbackMessages?.[key] ?? key;
|
|
245
|
+
if (raw.params) {
|
|
246
|
+
for (const [paramKey, paramValue] of Object.entries(raw.params)) {
|
|
247
|
+
const resolved = resolvePropValue8(paramValue, ctx);
|
|
248
|
+
template = template.replace(
|
|
249
|
+
new RegExp(`\\{\\{${paramKey}\\}\\}`, "g"),
|
|
250
|
+
resolved != null ? String(resolved) : ""
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return template;
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// src/index.ts
|
|
260
|
+
var standardDirectives = [
|
|
261
|
+
formatDirective,
|
|
262
|
+
mathDirective,
|
|
263
|
+
concatDirective,
|
|
264
|
+
countDirective,
|
|
265
|
+
truncateDirective,
|
|
266
|
+
pluralizeDirective,
|
|
267
|
+
joinDirective
|
|
268
|
+
];
|
|
269
|
+
export {
|
|
270
|
+
concatDirective,
|
|
271
|
+
countDirective,
|
|
272
|
+
createI18nDirective,
|
|
273
|
+
formatDirective,
|
|
274
|
+
joinDirective,
|
|
275
|
+
mathDirective,
|
|
276
|
+
pluralizeDirective,
|
|
277
|
+
standardDirectives,
|
|
278
|
+
truncateDirective
|
|
279
|
+
};
|
|
280
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/format.ts","../src/math.ts","../src/concat.ts","../src/count.ts","../src/truncate.ts","../src/pluralize.ts","../src/join.ts","../src/i18n.ts","../src/index.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\n\nexport const formatDirective = defineDirective({\n name: \"$format\",\n description:\n 'Locale-aware value formatting (date, currency, number, percent). Supports style: \"relative\" for relative dates.',\n schema: z.object({\n $format: z.enum([\"date\", \"currency\", \"number\", \"percent\"]),\n value: z.unknown(),\n locale: z.string().optional(),\n currency: z.string().optional(),\n notation: z.string().optional(),\n style: z.string().optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n now: z.number().optional(),\n }),\n resolve(raw, ctx) {\n const value = resolvePropValue(raw.value, ctx);\n const locale = raw.locale ?? undefined;\n const extra = raw.options ?? {};\n\n switch (raw.$format) {\n case \"date\": {\n const date =\n value instanceof Date ? value : new Date(value as string | number);\n if (raw.style === \"relative\") {\n const now = raw.now ?? Date.now();\n const diff = now - date.getTime();\n if (diff === 0) return \"just now\";\n const absDiff = Math.abs(diff);\n const suffix = diff > 0 ? \"ago\" : \"from now\";\n const seconds = Math.floor(absDiff / 1000);\n const minutes = Math.floor(seconds / 60);\n const hours = Math.floor(minutes / 60);\n const days = Math.floor(hours / 24);\n if (days > 0) return `${days}d ${suffix}`;\n if (hours > 0) return `${hours}h ${suffix}`;\n if (minutes > 0) return `${minutes}m ${suffix}`;\n return `${seconds}s ${suffix}`;\n }\n return new Intl.DateTimeFormat(\n locale,\n extra as Intl.DateTimeFormatOptions,\n ).format(date);\n }\n case \"currency\":\n return new Intl.NumberFormat(locale, {\n style: \"currency\",\n currency: raw.currency ?? \"USD\",\n ...(extra as Intl.NumberFormatOptions),\n }).format(value as number);\n case \"number\":\n return new Intl.NumberFormat(locale, {\n ...(raw.notation\n ? {\n notation: raw.notation as Intl.NumberFormatOptions[\"notation\"],\n }\n : {}),\n ...(extra as Intl.NumberFormatOptions),\n }).format(value as number);\n case \"percent\":\n return new Intl.NumberFormat(locale, {\n style: \"percent\",\n ...(extra as Intl.NumberFormatOptions),\n }).format(value as number);\n default:\n return value;\n }\n },\n});\n","import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\n\nfunction toNum(v: unknown): number {\n if (v == null) return 0;\n const n = Number(v);\n if (Number.isNaN(n)) {\n console.warn(`$math: non-numeric value coerced to 0:`, v);\n return 0;\n }\n return n;\n}\n\nexport const mathDirective = defineDirective({\n name: \"$math\",\n description:\n 'Arithmetic operations. Unary ops (round, floor, ceil, abs) only use \"a\". Division by zero returns 0.',\n schema: z.object({\n $math: z.enum([\n \"add\",\n \"subtract\",\n \"multiply\",\n \"divide\",\n \"mod\",\n \"min\",\n \"max\",\n \"round\",\n \"floor\",\n \"ceil\",\n \"abs\",\n ]),\n a: z.unknown().optional(),\n b: z.unknown().optional(),\n }),\n resolve(raw, ctx) {\n const a = toNum(resolvePropValue(raw.a, ctx));\n const b = toNum(resolvePropValue(raw.b, ctx));\n\n switch (raw.$math) {\n case \"add\":\n return a + b;\n case \"subtract\":\n return a - b;\n case \"multiply\":\n return a * b;\n case \"divide\":\n return b !== 0 ? a / b : 0;\n case \"mod\":\n return b !== 0 ? a % b : 0;\n case \"min\":\n return Math.min(a, b);\n case \"max\":\n return Math.max(a, b);\n case \"round\":\n return Math.round(a);\n case \"floor\":\n return Math.floor(a);\n case \"ceil\":\n return Math.ceil(a);\n case \"abs\":\n return Math.abs(a);\n default:\n return a;\n }\n },\n});\n","import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\n\nexport const concatDirective = defineDirective({\n name: \"$concat\",\n description: \"Concatenate multiple dynamic values into a string.\",\n schema: z.object({\n $concat: z.array(z.unknown()),\n }),\n resolve(raw, ctx) {\n return raw.$concat\n .map((part) => {\n const resolved = resolvePropValue(part, ctx);\n return resolved != null ? String(resolved) : \"\";\n })\n .join(\"\");\n },\n});\n","import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\n\nexport const countDirective = defineDirective({\n name: \"$count\",\n description: \"Get the length of an array or string.\",\n schema: z.object({\n $count: z.unknown(),\n }),\n resolve(raw, ctx) {\n const resolved = resolvePropValue(raw.$count, ctx);\n if (Array.isArray(resolved)) return resolved.length;\n if (typeof resolved === \"string\") return resolved.length;\n return 0;\n },\n});\n","import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\n\nexport const truncateDirective = defineDirective({\n name: \"$truncate\",\n description: \"Truncate text to a max length with a suffix.\",\n schema: z.object({\n $truncate: z.unknown(),\n length: z.number().optional(),\n suffix: z.string().optional(),\n }),\n resolve(raw, ctx) {\n const resolved = resolvePropValue(raw.$truncate, ctx);\n const text = resolved != null ? String(resolved) : \"\";\n const maxLength = raw.length ?? 100;\n const suffix = raw.suffix ?? \"...\";\n\n if (text.length <= maxLength) return text;\n return text.slice(0, maxLength) + suffix;\n },\n});\n","import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\n\nexport const pluralizeDirective = defineDirective({\n name: \"$pluralize\",\n description:\n 'Select singular/plural/zero form based on count. Output: \"3 items\", \"1 item\", or \"no items\".',\n schema: z.object({\n $pluralize: z.unknown(),\n zero: z.string().optional(),\n one: z.string(),\n other: z.string(),\n }),\n resolve(raw, ctx) {\n const resolved = resolvePropValue(raw.$pluralize, ctx);\n const n = Number(resolved);\n const count = Number.isNaN(n) ? 0 : n;\n\n if (count === 0 && raw.zero != null) return raw.zero;\n if (count === 1) return `${count} ${raw.one}`;\n return `${count} ${raw.other}`;\n },\n});\n","import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\n\nexport const joinDirective = defineDirective({\n name: \"$join\",\n description: \"Join array elements with a separator.\",\n schema: z.object({\n $join: z.unknown(),\n separator: z.string().optional(),\n }),\n resolve(raw, ctx) {\n const resolved = resolvePropValue(raw.$join, ctx);\n const separator = raw.separator ?? \", \";\n\n if (Array.isArray(resolved)) {\n return resolved\n .map((item) => (item != null ? String(item) : \"\"))\n .join(separator);\n }\n return resolved != null ? String(resolved) : \"\";\n },\n});\n","import { z } from \"zod\";\nimport { defineDirective, resolvePropValue } from \"@json-render/core\";\nimport type { DirectiveDefinition } from \"@json-render/core\";\n\nexport interface I18nConfig {\n /** Current locale (e.g. \"en\", \"es\") */\n locale: string;\n /** Map of locale → key → translated string */\n messages: Record<string, Record<string, string>>;\n /** Fallback locale when a key is missing in the current locale */\n fallbackLocale?: string;\n}\n\n/**\n * Create a `$t` directive for internationalization.\n *\n * @example\n * ```ts\n * const t = createI18nDirective({\n * locale: 'en',\n * messages: {\n * en: { \"greeting\": \"Hello, {{name}}!\" },\n * es: { \"greeting\": \"Hola, {{name}}!\" },\n * },\n * });\n * ```\n */\nexport function createI18nDirective(config: I18nConfig): DirectiveDefinition {\n return defineDirective({\n name: \"$t\",\n description: \"Translated text with {{param}} interpolation.\",\n schema: z.object({\n $t: z.string(),\n params: z.record(z.string(), z.unknown()).optional(),\n }),\n resolve(raw, ctx) {\n const key = raw.$t;\n\n const localeMessages = config.messages[config.locale];\n const fallbackMessages = config.fallbackLocale\n ? config.messages[config.fallbackLocale]\n : undefined;\n let template = localeMessages?.[key] ?? fallbackMessages?.[key] ?? key;\n\n if (raw.params) {\n for (const [paramKey, paramValue] of Object.entries(raw.params)) {\n const resolved = resolvePropValue(paramValue, ctx);\n template = template.replace(\n new RegExp(`\\\\{\\\\{${paramKey}\\\\}\\\\}`, \"g\"),\n resolved != null ? String(resolved) : \"\",\n );\n }\n }\n\n return template;\n },\n });\n}\n","export { formatDirective } from \"./format\";\nexport { mathDirective } from \"./math\";\nexport { concatDirective } from \"./concat\";\nexport { countDirective } from \"./count\";\nexport { truncateDirective } from \"./truncate\";\nexport { pluralizeDirective } from \"./pluralize\";\nexport { joinDirective } from \"./join\";\nexport { createI18nDirective, type I18nConfig } from \"./i18n\";\n\nimport { formatDirective } from \"./format\";\nimport { mathDirective } from \"./math\";\nimport { concatDirective } from \"./concat\";\nimport { countDirective } from \"./count\";\nimport { truncateDirective } from \"./truncate\";\nimport { pluralizeDirective } from \"./pluralize\";\nimport { joinDirective } from \"./join\";\n\n/**\n * All non-factory directives in a single array.\n * Spread with factory directives as needed:\n * `[...standardDirectives, createI18nDirective(config)]`\n */\nexport const standardDirectives = [\n formatDirective,\n mathDirective,\n concatDirective,\n countDirective,\n truncateDirective,\n pluralizeDirective,\n joinDirective,\n];\n"],"mappings":";AAAA,SAAS,SAAS;AAClB,SAAS,iBAAiB,wBAAwB;AAE3C,IAAM,kBAAkB,gBAAgB;AAAA,EAC7C,MAAM;AAAA,EACN,aACE;AAAA,EACF,QAAQ,EAAE,OAAO;AAAA,IACf,SAAS,EAAE,KAAK,CAAC,QAAQ,YAAY,UAAU,SAAS,CAAC;AAAA,IACzD,OAAO,EAAE,QAAQ;AAAA,IACjB,QAAQ,EAAE,OAAO,EAAE,SAAS;AAAA,IAC5B,UAAU,EAAE,OAAO,EAAE,SAAS;AAAA,IAC9B,UAAU,EAAE,OAAO,EAAE,SAAS;AAAA,IAC9B,OAAO,EAAE,OAAO,EAAE,SAAS;AAAA,IAC3B,SAAS,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,QAAQ,CAAC,EAAE,SAAS;AAAA,IACpD,KAAK,EAAE,OAAO,EAAE,SAAS;AAAA,EAC3B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,UAAU,KAAK,OAAO,QAAQ,IAAI,MAAM,GAAG;AAC/D,gBAAM,WAAWE,kBAAiB,YAAY,GAAG;AACjD,qBAAW,SAAS;AAAA,YAClB,IAAI,OAAO,SAAS,QAAQ,UAAU,GAAG;AAAA,YACzC,YAAY,OAAO,OAAO,QAAQ,IAAI;AAAA,UACxC;AAAA,QACF;AAAA,MACF;AAEA,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AACH;;;ACnCO,IAAM,qBAAqB;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;","names":["z","defineDirective","resolvePropValue","z","defineDirective","resolvePropValue","z","defineDirective","resolvePropValue","z","defineDirective","resolvePropValue","z","defineDirective","resolvePropValue","z","defineDirective","resolvePropValue","z","defineDirective","resolvePropValue"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@json-render/directives",
|
|
3
|
+
"version": "0.19.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"description": "Pre-built directives for @json-render/core — $format, $math, $concat, $count, $truncate, $pluralize, $join, and $t (i18n).",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"json-render",
|
|
8
|
+
"directives",
|
|
9
|
+
"format",
|
|
10
|
+
"i18n",
|
|
11
|
+
"math",
|
|
12
|
+
"generative-ui"
|
|
13
|
+
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/vercel-labs/json-render.git",
|
|
17
|
+
"directory": "packages/directives"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://json-render.dev",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/vercel-labs/json-render/issues"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"module": "./dist/index.mjs",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"import": "./dist/index.mjs",
|
|
33
|
+
"require": "./dist/index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist"
|
|
38
|
+
],
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@json-render/core": "0.19.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"tsup": "^8.0.2",
|
|
44
|
+
"typescript": "^5.4.5",
|
|
45
|
+
"vitest": "^4.0.17",
|
|
46
|
+
"zod": "^4.3.6",
|
|
47
|
+
"@internal/typescript-config": "0.0.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"zod": "^4.0.0"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "tsup",
|
|
54
|
+
"dev": "tsup --watch",
|
|
55
|
+
"typecheck": "tsc --noEmit",
|
|
56
|
+
"test": "vitest run"
|
|
57
|
+
}
|
|
58
|
+
}
|