@fedify/vocab-tools 2.0.22-dev.1440 → 2.0.22-dev.1471

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
@@ -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 `
@@ -138,6 +153,17 @@ export async function* generateClasses(
138
153
  isTemporalDuration,
139
154
  isTemporalInstant,
140
155
  } from "@fedify/vocab-runtime/temporal";\n`;
156
+ yield `
157
+ function isValidLanguageTag(language: string): boolean {
158
+ try {
159
+ new Intl.Locale(language);
160
+ return true;
161
+ } catch (error) {
162
+ if (error instanceof RangeError) return false;
163
+ throw error;
164
+ }
165
+ }
166
+ `;
141
167
  yield "\n\n";
142
168
  const sorted = sortTopologically(types);
143
169
  for (const typeUri of sorted) {
package/src/codec.ts CHANGED
@@ -396,6 +396,9 @@ export async function* generateDecoder(
396
396
  }
397
397
  `;
398
398
  }
399
+ yield `
400
+ let shouldCacheJsonLd = instance._shouldCacheJsonLd;
401
+ `;
399
402
  for (const property of type.properties) {
400
403
  const variable = await getFieldName(property.uri, "");
401
404
  yield await generateField(property, types, "const ");
@@ -435,20 +438,18 @@ export async function* generateDecoder(
435
438
  }
436
439
  `;
437
440
  }
441
+ yield `
442
+ const decoded =
443
+ `;
438
444
  if (property.range.length == 1) {
439
- yield `${variable}.push(${
440
- getDecoder(
441
- property.range[0],
442
- types,
443
- "v",
444
- "options",
445
- `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`,
446
- )
447
- })`;
445
+ yield getDecoder(
446
+ property.range[0],
447
+ types,
448
+ "v",
449
+ "options",
450
+ `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`,
451
+ );
448
452
  } else {
449
- yield `
450
- const decoded =
451
- `;
452
453
  const decoders = getDecoders(
453
454
  property.range,
454
455
  types,
@@ -457,19 +458,35 @@ export async function* generateDecoder(
457
458
  `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`,
458
459
  );
459
460
  for (const code of decoders) yield code;
460
- yield `
461
+ }
462
+ yield `
461
463
  ;
462
- if (typeof decoded === "undefined") continue;
463
- ${variable}.push(decoded);
464
+ `;
465
+ if (property.range.length > 1) {
466
+ yield `
467
+ if (typeof decoded === "undefined") {
468
+ shouldCacheJsonLd = false;
469
+ continue;
470
+ }
464
471
  `;
465
472
  }
473
+ yield `
474
+ if (!this._shouldCacheDecodedJsonLd(decoded)) {
475
+ shouldCacheJsonLd = false;
476
+ }
477
+ ${variable}.push(decoded);
478
+ `;
466
479
  yield `
467
480
  }
468
481
  instance.${await getFieldName(property.uri)} = ${variable};
469
482
  `;
470
483
  }
471
484
  yield `
472
- if (!("_fromSubclass" in options) || !options._fromSubclass) {
485
+ instance._shouldCacheJsonLd = shouldCacheJsonLd;
486
+ if (
487
+ shouldCacheJsonLd &&
488
+ (!("_fromSubclass" in options) || !options._fromSubclass)
489
+ ) {
473
490
  try {
474
491
  instance._cachedJsonLd = structuredClone(json);
475
492
  } catch {
package/src/type.ts CHANGED
@@ -176,7 +176,8 @@ const scalarTypes: Record<string, ScalarType> = {
176
176
  dataCheck(v) {
177
177
  return `typeof ${v} === "object" && "@language" in ${v} && "@value" in ${v}
178
178
  && typeof ${v}["@language"] === "string"
179
- && typeof ${v}["@value"] === "string"`;
179
+ && typeof ${v}["@value"] === "string"
180
+ && isValidLanguageTag(${v}["@language"])`;
180
181
  },
181
182
  decoder(v) {
182
183
  return `new LanguageString(${v}["@value"], ${v}["@language"])`;