@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/deno.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/vocab-tools",
3
- "version": "2.0.22-dev.1440+7ada752e",
3
+ "version": "2.0.22-dev.1471+cfbcd5d4",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": "./src/mod.ts"
package/dist/mod.cjs CHANGED
@@ -8,7 +8,7 @@ let yaml = require("yaml");
8
8
  let es_toolkit = require("es-toolkit");
9
9
  //#region deno.json
10
10
  var name = "@fedify/vocab-tools";
11
- var version = "2.0.22-dev.1440+7ada752e";
11
+ var version = "2.0.22-dev.1471+cfbcd5d4";
12
12
  //#endregion
13
13
  //#region src/type.ts
14
14
  const HEURISTICS_CONTEXTS = [
@@ -175,7 +175,8 @@ const scalarTypes = {
175
175
  dataCheck(v) {
176
176
  return `typeof ${v} === "object" && "@language" in ${v} && "@value" in ${v}
177
177
  && typeof ${v}["@language"] === "string"
178
- && typeof ${v}["@value"] === "string"`;
178
+ && typeof ${v}["@value"] === "string"
179
+ && isValidLanguageTag(${v}["@language"])`;
179
180
  },
180
181
  decoder(v) {
181
182
  return `new LanguageString(${v}["@value"], ${v}["@language"])`;
@@ -960,6 +961,9 @@ async function* generateDecoder(typeUri, types) {
960
961
  throw new TypeError("Unexpected type: " + instance.constructor.name);
961
962
  }
962
963
  `;
964
+ yield `
965
+ let shouldCacheJsonLd = instance._shouldCacheJsonLd;
966
+ `;
963
967
  for (const property of type.properties) {
964
968
  const variable = await getFieldName(property.uri, "");
965
969
  yield await generateField(property, types, "const ");
@@ -993,26 +997,40 @@ async function* generateDecoder(typeUri, types) {
993
997
  continue;
994
998
  }
995
999
  `;
996
- if (property.range.length == 1) yield `${variable}.push(${getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`)})`;
997
- else {
998
- yield `
1000
+ yield `
999
1001
  const decoded =
1000
- `;
1002
+ `;
1003
+ if (property.range.length == 1) yield getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1004
+ else {
1001
1005
  const decoders = getDecoders(property.range, types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1002
1006
  for (const code of decoders) yield code;
1003
- yield `
1007
+ }
1008
+ yield `
1004
1009
  ;
1005
- if (typeof decoded === "undefined") continue;
1006
- ${variable}.push(decoded);
1010
+ `;
1011
+ if (property.range.length > 1) yield `
1012
+ if (typeof decoded === "undefined") {
1013
+ shouldCacheJsonLd = false;
1014
+ continue;
1015
+ }
1007
1016
  `;
1008
- }
1017
+ yield `
1018
+ if (!this._shouldCacheDecodedJsonLd(decoded)) {
1019
+ shouldCacheJsonLd = false;
1020
+ }
1021
+ ${variable}.push(decoded);
1022
+ `;
1009
1023
  yield `
1010
1024
  }
1011
1025
  instance.${await getFieldName(property.uri)} = ${variable};
1012
1026
  `;
1013
1027
  }
1014
1028
  yield `
1015
- if (!("_fromSubclass" in options) || !options._fromSubclass) {
1029
+ instance._shouldCacheJsonLd = shouldCacheJsonLd;
1030
+ if (
1031
+ shouldCacheJsonLd &&
1032
+ (!("_fromSubclass" in options) || !options._fromSubclass)
1033
+ ) {
1016
1034
  try {
1017
1035
  instance._cachedJsonLd = structuredClone(json);
1018
1036
  } catch {
@@ -1779,6 +1797,7 @@ async function* generateClass(typeUri, types) {
1779
1797
  values?: Record<string, unknown>;
1780
1798
  };
1781
1799
  #cachedJsonLd?: unknown;
1800
+ #shouldCacheJsonLd = true;
1782
1801
  readonly id: URL | null;
1783
1802
 
1784
1803
  protected get _documentLoader(): DocumentLoader | undefined {
@@ -1808,6 +1827,20 @@ async function* generateClass(typeUri, types) {
1808
1827
  protected set _cachedJsonLd(value: unknown | undefined) {
1809
1828
  this.#cachedJsonLd = value;
1810
1829
  }
1830
+
1831
+ protected get _shouldCacheJsonLd(): boolean {
1832
+ return this.#shouldCacheJsonLd;
1833
+ }
1834
+
1835
+ protected set _shouldCacheJsonLd(value: boolean) {
1836
+ this.#shouldCacheJsonLd = value;
1837
+ }
1838
+
1839
+ protected static _shouldCacheDecodedJsonLd(value: unknown): boolean {
1840
+ if (value == null || typeof value !== "object") return true;
1841
+ if (!("_shouldCacheJsonLd" in value)) return true;
1842
+ return (value as { _shouldCacheJsonLd: boolean })._shouldCacheJsonLd;
1843
+ }
1811
1844
  `;
1812
1845
  yield `
1813
1846
  /**
@@ -1854,6 +1887,17 @@ async function* generateClasses(types) {
1854
1887
  isTemporalDuration,
1855
1888
  isTemporalInstant,
1856
1889
  } from "@fedify/vocab-runtime/temporal";\n`;
1890
+ yield `
1891
+ function isValidLanguageTag(language: string): boolean {
1892
+ try {
1893
+ new Intl.Locale(language);
1894
+ return true;
1895
+ } catch (error) {
1896
+ if (error instanceof RangeError) return false;
1897
+ throw error;
1898
+ }
1899
+ }
1900
+ `;
1857
1901
  yield "\n\n";
1858
1902
  const sorted = sortTopologically(types);
1859
1903
  for (const typeUri of sorted) for await (const code of generateClass(typeUri, types)) yield code;
package/dist/mod.js CHANGED
@@ -7,7 +7,7 @@ import { parse } from "yaml";
7
7
  import { pascalCase } from "es-toolkit";
8
8
  //#region deno.json
9
9
  var name = "@fedify/vocab-tools";
10
- var version = "2.0.22-dev.1440+7ada752e";
10
+ var version = "2.0.22-dev.1471+cfbcd5d4";
11
11
  //#endregion
12
12
  //#region src/type.ts
13
13
  const HEURISTICS_CONTEXTS = [
@@ -174,7 +174,8 @@ const scalarTypes = {
174
174
  dataCheck(v) {
175
175
  return `typeof ${v} === "object" && "@language" in ${v} && "@value" in ${v}
176
176
  && typeof ${v}["@language"] === "string"
177
- && typeof ${v}["@value"] === "string"`;
177
+ && typeof ${v}["@value"] === "string"
178
+ && isValidLanguageTag(${v}["@language"])`;
178
179
  },
179
180
  decoder(v) {
180
181
  return `new LanguageString(${v}["@value"], ${v}["@language"])`;
@@ -959,6 +960,9 @@ async function* generateDecoder(typeUri, types) {
959
960
  throw new TypeError("Unexpected type: " + instance.constructor.name);
960
961
  }
961
962
  `;
963
+ yield `
964
+ let shouldCacheJsonLd = instance._shouldCacheJsonLd;
965
+ `;
962
966
  for (const property of type.properties) {
963
967
  const variable = await getFieldName(property.uri, "");
964
968
  yield await generateField(property, types, "const ");
@@ -992,26 +996,40 @@ async function* generateDecoder(typeUri, types) {
992
996
  continue;
993
997
  }
994
998
  `;
995
- if (property.range.length == 1) yield `${variable}.push(${getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`)})`;
996
- else {
997
- yield `
999
+ yield `
998
1000
  const decoded =
999
- `;
1001
+ `;
1002
+ if (property.range.length == 1) yield getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1003
+ else {
1000
1004
  const decoders = getDecoders(property.range, types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1001
1005
  for (const code of decoders) yield code;
1002
- yield `
1006
+ }
1007
+ yield `
1003
1008
  ;
1004
- if (typeof decoded === "undefined") continue;
1005
- ${variable}.push(decoded);
1009
+ `;
1010
+ if (property.range.length > 1) yield `
1011
+ if (typeof decoded === "undefined") {
1012
+ shouldCacheJsonLd = false;
1013
+ continue;
1014
+ }
1006
1015
  `;
1007
- }
1016
+ yield `
1017
+ if (!this._shouldCacheDecodedJsonLd(decoded)) {
1018
+ shouldCacheJsonLd = false;
1019
+ }
1020
+ ${variable}.push(decoded);
1021
+ `;
1008
1022
  yield `
1009
1023
  }
1010
1024
  instance.${await getFieldName(property.uri)} = ${variable};
1011
1025
  `;
1012
1026
  }
1013
1027
  yield `
1014
- if (!("_fromSubclass" in options) || !options._fromSubclass) {
1028
+ instance._shouldCacheJsonLd = shouldCacheJsonLd;
1029
+ if (
1030
+ shouldCacheJsonLd &&
1031
+ (!("_fromSubclass" in options) || !options._fromSubclass)
1032
+ ) {
1015
1033
  try {
1016
1034
  instance._cachedJsonLd = structuredClone(json);
1017
1035
  } catch {
@@ -1778,6 +1796,7 @@ async function* generateClass(typeUri, types) {
1778
1796
  values?: Record<string, unknown>;
1779
1797
  };
1780
1798
  #cachedJsonLd?: unknown;
1799
+ #shouldCacheJsonLd = true;
1781
1800
  readonly id: URL | null;
1782
1801
 
1783
1802
  protected get _documentLoader(): DocumentLoader | undefined {
@@ -1807,6 +1826,20 @@ async function* generateClass(typeUri, types) {
1807
1826
  protected set _cachedJsonLd(value: unknown | undefined) {
1808
1827
  this.#cachedJsonLd = value;
1809
1828
  }
1829
+
1830
+ protected get _shouldCacheJsonLd(): boolean {
1831
+ return this.#shouldCacheJsonLd;
1832
+ }
1833
+
1834
+ protected set _shouldCacheJsonLd(value: boolean) {
1835
+ this.#shouldCacheJsonLd = value;
1836
+ }
1837
+
1838
+ protected static _shouldCacheDecodedJsonLd(value: unknown): boolean {
1839
+ if (value == null || typeof value !== "object") return true;
1840
+ if (!("_shouldCacheJsonLd" in value)) return true;
1841
+ return (value as { _shouldCacheJsonLd: boolean })._shouldCacheJsonLd;
1842
+ }
1810
1843
  `;
1811
1844
  yield `
1812
1845
  /**
@@ -1853,6 +1886,17 @@ async function* generateClasses(types) {
1853
1886
  isTemporalDuration,
1854
1887
  isTemporalInstant,
1855
1888
  } from "@fedify/vocab-runtime/temporal";\n`;
1889
+ yield `
1890
+ function isValidLanguageTag(language: string): boolean {
1891
+ try {
1892
+ new Intl.Locale(language);
1893
+ return true;
1894
+ } catch (error) {
1895
+ if (error instanceof RangeError) return false;
1896
+ throw error;
1897
+ }
1898
+ }
1899
+ `;
1856
1900
  yield "\n\n";
1857
1901
  const sorted = sortTopologically(types);
1858
1902
  for (const typeUri of sorted) for await (const code of generateClass(typeUri, types)) yield code;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/vocab-tools",
3
- "version": "2.0.22-dev.1440+7ada752e",
3
+ "version": "2.0.22-dev.1471+cfbcd5d4",
4
4
  "description": "Code generator for Activity Vocabulary APIs",
5
5
  "homepage": "https://fedify.dev/",
6
6
  "repository": {