@fedify/vocab-tools 2.3.2-dev.1556 → 2.3.3

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/src/class.ts CHANGED
@@ -59,6 +59,7 @@ async function* generateClass(
59
59
  };
60
60
  #cachedJsonLd?: unknown;
61
61
  readonly #_baseUrl?: URL;
62
+ #shouldCacheJsonLd = true;
62
63
  readonly id: URL | null;
63
64
 
64
65
  protected get _documentLoader(): DocumentLoader | undefined {
@@ -92,6 +93,20 @@ async function* generateClass(
92
93
  protected get _baseUrl(): URL | undefined {
93
94
  return this.#_baseUrl;
94
95
  }
96
+
97
+ protected get _shouldCacheJsonLd(): boolean {
98
+ return this.#shouldCacheJsonLd;
99
+ }
100
+
101
+ protected set _shouldCacheJsonLd(value: boolean) {
102
+ this.#shouldCacheJsonLd = value;
103
+ }
104
+
105
+ protected static _shouldCacheDecodedJsonLd(value: unknown): boolean {
106
+ if (value == null || typeof value !== "object") return true;
107
+ if (!("_shouldCacheJsonLd" in value)) return true;
108
+ return (value as { _shouldCacheJsonLd: boolean })._shouldCacheJsonLd;
109
+ }
95
110
  `;
96
111
  }
97
112
  yield `
@@ -206,6 +221,17 @@ export async function* generateClasses(
206
221
  isTemporalDuration,
207
222
  isTemporalInstant,
208
223
  } from "@fedify/vocab-runtime/temporal";\n`;
224
+ yield `
225
+ function isValidLanguageTag(language: string): boolean {
226
+ try {
227
+ new Intl.Locale(language);
228
+ return true;
229
+ } catch (error) {
230
+ if (error instanceof RangeError) return false;
231
+ throw error;
232
+ }
233
+ }
234
+ `;
209
235
  yield "\n\n";
210
236
  const moduleVarNames = new Map<string, string>();
211
237
  const sorted = sortTopologically(types);
package/src/codec.ts CHANGED
@@ -452,6 +452,9 @@ export async function* generateDecoder(
452
452
  }
453
453
  `;
454
454
  }
455
+ yield `
456
+ let shouldCacheJsonLd = instance._shouldCacheJsonLd;
457
+ `;
455
458
  for (const property of type.properties) {
456
459
  const variable = await getFieldName(property.uri, "");
457
460
  yield await generateField(property, types, "const ");
@@ -500,23 +503,18 @@ export async function* generateDecoder(
500
503
  }
501
504
  `;
502
505
  }
506
+ yield `
507
+ const decoded =
508
+ `;
503
509
  if (property.range.length == 1) {
504
- yield `
505
- const decoded = ${
506
- getDecoder(
507
- property.range[0],
508
- types,
509
- "v",
510
- "options",
511
- propertyBaseUrl,
512
- )
513
- };
514
- if (typeof decoded === "undefined") continue;
515
- ${variable}.push(decoded);`;
510
+ yield getDecoder(
511
+ property.range[0],
512
+ types,
513
+ "v",
514
+ "options",
515
+ propertyBaseUrl,
516
+ );
516
517
  } else {
517
- yield `
518
- const decoded =
519
- `;
520
518
  const decoders = getDecoders(
521
519
  property.range,
522
520
  types,
@@ -525,19 +523,33 @@ export async function* generateDecoder(
525
523
  propertyBaseUrl,
526
524
  );
527
525
  for (const code of decoders) yield code;
528
- yield `
526
+ }
527
+ yield `
529
528
  ;
530
- if (typeof decoded === "undefined") continue;
529
+ `;
530
+ yield `
531
+ if (typeof decoded === "undefined") {
532
+ shouldCacheJsonLd = false;
533
+ continue;
534
+ }
535
+ `;
536
+ yield `
537
+ if (!this._shouldCacheDecodedJsonLd(decoded)) {
538
+ shouldCacheJsonLd = false;
539
+ }
531
540
  ${variable}.push(decoded);
532
- `;
533
- }
541
+ `;
534
542
  yield `
535
543
  }
536
544
  instance.${await getFieldName(property.uri)} = ${variable};
537
545
  `;
538
546
  }
539
547
  yield `
540
- if (!("_fromSubclass" in options) || !options._fromSubclass) {
548
+ instance._shouldCacheJsonLd = shouldCacheJsonLd;
549
+ if (
550
+ shouldCacheJsonLd &&
551
+ (!("_fromSubclass" in options) || !options._fromSubclass)
552
+ ) {
541
553
  try {
542
554
  instance._cachedJsonLd = structuredClone(json);
543
555
  } 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"])`;