@fedify/vocab-tools 2.2.5 → 2.2.7
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/deno.json +1 -1
- package/dist/mod.cjs +55 -14
- package/dist/mod.js +55 -14
- package/package.json +1 -1
- package/src/__snapshots__/class.test.ts.deno.snap +4154 -808
- package/src/__snapshots__/class.test.ts.node.snap +4154 -808
- package/src/__snapshots__/class.test.ts.snap +4154 -808
- package/src/class.ts +26 -0
- package/src/codec.ts +32 -20
- package/src/type.ts +2 -1
package/src/class.ts
CHANGED
|
@@ -57,6 +57,7 @@ async function* generateClass(
|
|
|
57
57
|
values?: Record<string, unknown>;
|
|
58
58
|
};
|
|
59
59
|
#cachedJsonLd?: unknown;
|
|
60
|
+
#shouldCacheJsonLd = true;
|
|
60
61
|
readonly id: URL | null;
|
|
61
62
|
|
|
62
63
|
protected get _documentLoader(): DocumentLoader | undefined {
|
|
@@ -86,6 +87,20 @@ async function* generateClass(
|
|
|
86
87
|
protected set _cachedJsonLd(value: unknown | undefined) {
|
|
87
88
|
this.#cachedJsonLd = value;
|
|
88
89
|
}
|
|
90
|
+
|
|
91
|
+
protected get _shouldCacheJsonLd(): boolean {
|
|
92
|
+
return this.#shouldCacheJsonLd;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
protected set _shouldCacheJsonLd(value: boolean) {
|
|
96
|
+
this.#shouldCacheJsonLd = value;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
protected static _shouldCacheDecodedJsonLd(value: unknown): boolean {
|
|
100
|
+
if (value == null || typeof value !== "object") return true;
|
|
101
|
+
if (!("_shouldCacheJsonLd" in value)) return true;
|
|
102
|
+
return (value as { _shouldCacheJsonLd: boolean })._shouldCacheJsonLd;
|
|
103
|
+
}
|
|
89
104
|
`;
|
|
90
105
|
}
|
|
91
106
|
yield `
|
|
@@ -196,6 +211,17 @@ export async function* generateClasses(
|
|
|
196
211
|
isTemporalDuration,
|
|
197
212
|
isTemporalInstant,
|
|
198
213
|
} from "@fedify/vocab-runtime/temporal";\n`;
|
|
214
|
+
yield `
|
|
215
|
+
function isValidLanguageTag(language: string): boolean {
|
|
216
|
+
try {
|
|
217
|
+
new Intl.Locale(language);
|
|
218
|
+
return true;
|
|
219
|
+
} catch (error) {
|
|
220
|
+
if (error instanceof RangeError) return false;
|
|
221
|
+
throw error;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
`;
|
|
199
225
|
yield "\n\n";
|
|
200
226
|
const sorted = sortTopologically(types);
|
|
201
227
|
for (const typeUri of sorted) {
|
package/src/codec.ts
CHANGED
|
@@ -400,6 +400,9 @@ export async function* generateDecoder(
|
|
|
400
400
|
}
|
|
401
401
|
`;
|
|
402
402
|
}
|
|
403
|
+
yield `
|
|
404
|
+
let shouldCacheJsonLd = instance._shouldCacheJsonLd;
|
|
405
|
+
`;
|
|
403
406
|
for (const property of type.properties) {
|
|
404
407
|
const variable = await getFieldName(property.uri, "");
|
|
405
408
|
yield await generateField(property, types, "const ");
|
|
@@ -439,23 +442,18 @@ export async function* generateDecoder(
|
|
|
439
442
|
}
|
|
440
443
|
`;
|
|
441
444
|
}
|
|
445
|
+
yield `
|
|
446
|
+
const decoded =
|
|
447
|
+
`;
|
|
442
448
|
if (property.range.length == 1) {
|
|
443
|
-
yield
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
`(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`,
|
|
451
|
-
)
|
|
452
|
-
};
|
|
453
|
-
if (typeof decoded === "undefined") continue;
|
|
454
|
-
${variable}.push(decoded);`;
|
|
449
|
+
yield getDecoder(
|
|
450
|
+
property.range[0],
|
|
451
|
+
types,
|
|
452
|
+
"v",
|
|
453
|
+
"options",
|
|
454
|
+
`(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`,
|
|
455
|
+
);
|
|
455
456
|
} else {
|
|
456
|
-
yield `
|
|
457
|
-
const decoded =
|
|
458
|
-
`;
|
|
459
457
|
const decoders = getDecoders(
|
|
460
458
|
property.range,
|
|
461
459
|
types,
|
|
@@ -464,19 +462,33 @@ export async function* generateDecoder(
|
|
|
464
462
|
`(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`,
|
|
465
463
|
);
|
|
466
464
|
for (const code of decoders) yield code;
|
|
467
|
-
|
|
465
|
+
}
|
|
466
|
+
yield `
|
|
468
467
|
;
|
|
469
|
-
|
|
468
|
+
`;
|
|
469
|
+
yield `
|
|
470
|
+
if (typeof decoded === "undefined") {
|
|
471
|
+
shouldCacheJsonLd = false;
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
`;
|
|
475
|
+
yield `
|
|
476
|
+
if (!this._shouldCacheDecodedJsonLd(decoded)) {
|
|
477
|
+
shouldCacheJsonLd = false;
|
|
478
|
+
}
|
|
470
479
|
${variable}.push(decoded);
|
|
471
|
-
|
|
472
|
-
}
|
|
480
|
+
`;
|
|
473
481
|
yield `
|
|
474
482
|
}
|
|
475
483
|
instance.${await getFieldName(property.uri)} = ${variable};
|
|
476
484
|
`;
|
|
477
485
|
}
|
|
478
486
|
yield `
|
|
479
|
-
|
|
487
|
+
instance._shouldCacheJsonLd = shouldCacheJsonLd;
|
|
488
|
+
if (
|
|
489
|
+
shouldCacheJsonLd &&
|
|
490
|
+
(!("_fromSubclass" in options) || !options._fromSubclass)
|
|
491
|
+
) {
|
|
480
492
|
try {
|
|
481
493
|
instance._cachedJsonLd = structuredClone(json);
|
|
482
494
|
} catch {
|
package/src/type.ts
CHANGED
|
@@ -200,7 +200,8 @@ const scalarTypes: Record<string, ScalarType> = {
|
|
|
200
200
|
dataCheck(v) {
|
|
201
201
|
return `typeof ${v} === "object" && "@language" in ${v} && "@value" in ${v}
|
|
202
202
|
&& typeof ${v}["@language"] === "string"
|
|
203
|
-
&& typeof ${v}["@value"] === "string"
|
|
203
|
+
&& typeof ${v}["@value"] === "string"
|
|
204
|
+
&& isValidLanguageTag(${v}["@language"])`;
|
|
204
205
|
},
|
|
205
206
|
decoder(v) {
|
|
206
207
|
return `new LanguageString(${v}["@value"], ${v}["@language"])`;
|