@saykit/format-json 0.4.0 → 0.5.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/README.md +9 -2
- package/dist/formatter.d.mts +12 -1
- package/dist/formatter.mjs +9 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -70,10 +70,17 @@ Context and source references have no standard slot, so SayKit round-trips them
|
|
|
70
70
|
through `x-saykit-context` / `x-saykit-references` extension fields — other
|
|
71
71
|
tooling reads the `description` and safely ignores the rest.
|
|
72
72
|
|
|
73
|
+
### Options
|
|
74
|
+
|
|
75
|
+
- `dialect` (default none): `'arb'` or `'webextension'`, see above.
|
|
76
|
+
- `includeReferences` (default `true`): include `x-saykit-references` source
|
|
77
|
+
references. No effect on the plain layout, which carries no metadata.
|
|
78
|
+
- `includeLineNumbers` (default `true`): include line numbers in those references.
|
|
79
|
+
|
|
73
80
|
> [!NOTE]
|
|
74
81
|
> JSON catalogues are keyed by message id, so give your messages explicit ids to
|
|
75
|
-
> get stable, human-readable keys.
|
|
76
|
-
>
|
|
82
|
+
> get stable, human-readable keys. The plain layout carries no source
|
|
83
|
+
> references, comments, or contexts — it is a lean runtime format.
|
|
77
84
|
|
|
78
85
|
## Documentation
|
|
79
86
|
|
package/dist/formatter.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Formatter } from "@saykit/config";
|
|
2
|
-
|
|
3
2
|
//#region src/formatter.d.ts
|
|
4
3
|
type Dialect = 'arb' | 'webextension';
|
|
5
4
|
interface FormatterOptions {
|
|
@@ -20,6 +19,18 @@ interface FormatterOptions {
|
|
|
20
19
|
* metadata.
|
|
21
20
|
*/
|
|
22
21
|
dialect?: Dialect;
|
|
22
|
+
/**
|
|
23
|
+
* Include source references in the generated catalogue. Only the metadata
|
|
24
|
+
* carrying dialects have anywhere to put them, so this has no effect on the
|
|
25
|
+
* plain layout.
|
|
26
|
+
* @default true
|
|
27
|
+
*/
|
|
28
|
+
includeReferences?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Include line numbers in source references.
|
|
31
|
+
* @default true
|
|
32
|
+
*/
|
|
33
|
+
includeLineNumbers?: boolean;
|
|
23
34
|
}
|
|
24
35
|
declare function createJsonFormatter(options?: FormatterOptions): Formatter;
|
|
25
36
|
//#endregion
|
package/dist/formatter.mjs
CHANGED
|
@@ -16,11 +16,16 @@ function isRecord(value) {
|
|
|
16
16
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
17
17
|
}
|
|
18
18
|
/** Build the metadata attributes shared by the ARB and WebExtension layouts. */
|
|
19
|
-
function toAttributes(message) {
|
|
19
|
+
function toAttributes(message, options) {
|
|
20
20
|
const attributes = {};
|
|
21
21
|
if (message.comments.length) attributes.description = message.comments.join("\n");
|
|
22
22
|
if (message.context) attributes[CONTEXT_FIELD] = message.context;
|
|
23
|
-
if (
|
|
23
|
+
if (options.includeReferences !== false) {
|
|
24
|
+
let references = message.references ?? [];
|
|
25
|
+
if (options.includeLineNumbers === false) references = references.map((r) => r.replace(/:\d+$/, ""));
|
|
26
|
+
references = Array.from(new Set(references)).sort();
|
|
27
|
+
if (references.length) attributes[REFERENCES_FIELD] = references;
|
|
28
|
+
}
|
|
24
29
|
return attributes;
|
|
25
30
|
}
|
|
26
31
|
/** Reconstruct a {@link Message} from a key, its value, and optional metadata. */
|
|
@@ -56,7 +61,7 @@ function createJsonFormatter(options = {}) {
|
|
|
56
61
|
for (const message of sorted) {
|
|
57
62
|
const key = messageKey(message);
|
|
58
63
|
catalogue[key] = message.translation ?? "";
|
|
59
|
-
const attributes = toAttributes(message);
|
|
64
|
+
const attributes = toAttributes(message, options);
|
|
60
65
|
if (Object.keys(attributes).length) catalogue[`@${key}`] = attributes;
|
|
61
66
|
}
|
|
62
67
|
return `${JSON.stringify(catalogue, null, 2)}\n`;
|
|
@@ -65,7 +70,7 @@ function createJsonFormatter(options = {}) {
|
|
|
65
70
|
const catalogue = {};
|
|
66
71
|
for (const message of sorted) catalogue[messageKey(message)] = {
|
|
67
72
|
message: message.translation ?? "",
|
|
68
|
-
...toAttributes(message)
|
|
73
|
+
...toAttributes(message, options)
|
|
69
74
|
};
|
|
70
75
|
return `${JSON.stringify(catalogue, null, 2)}\n`;
|
|
71
76
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saykit/format-json",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "JSON file formatter for saykit",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"formatter",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"provenance": true
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@saykit/config": "^0.
|
|
37
|
+
"@saykit/config": "^0.5.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
40
|
"@saykit/config": "*"
|