@fedify/vocab-tools 2.1.16 → 2.1.18

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.1.16",
3
+ "version": "2.1.18",
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.1.16";
11
+ var version = "2.1.18";
12
12
  //#endregion
13
13
  //#region src/type.ts
14
14
  const HEURISTICS_CONTEXTS = [
@@ -199,7 +199,8 @@ const scalarTypes = {
199
199
  dataCheck(v) {
200
200
  return `typeof ${v} === "object" && "@language" in ${v} && "@value" in ${v}
201
201
  && typeof ${v}["@language"] === "string"
202
- && typeof ${v}["@value"] === "string"`;
202
+ && typeof ${v}["@value"] === "string"
203
+ && isValidLanguageTag(${v}["@language"])`;
203
204
  },
204
205
  decoder(v) {
205
206
  return `new LanguageString(${v}["@value"], ${v}["@language"])`;
@@ -1004,6 +1005,9 @@ async function* generateDecoder(typeUri, types) {
1004
1005
  throw new TypeError("Unexpected type: " + instance.constructor.name);
1005
1006
  }
1006
1007
  `;
1008
+ yield `
1009
+ let shouldCacheJsonLd = instance._shouldCacheJsonLd;
1010
+ `;
1007
1011
  for (const property of type.properties) {
1008
1012
  const variable = await getFieldName(property.uri, "");
1009
1013
  yield await generateField(property, types, "const ");
@@ -1037,26 +1041,40 @@ async function* generateDecoder(typeUri, types) {
1037
1041
  continue;
1038
1042
  }
1039
1043
  `;
1040
- if (property.range.length == 1) yield `${variable}.push(${getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`)})`;
1041
- else {
1042
- yield `
1044
+ yield `
1043
1045
  const decoded =
1044
- `;
1046
+ `;
1047
+ if (property.range.length == 1) yield getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1048
+ else {
1045
1049
  const decoders = getDecoders(property.range, types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1046
1050
  for (const code of decoders) yield code;
1047
- yield `
1051
+ }
1052
+ yield `
1048
1053
  ;
1049
- if (typeof decoded === "undefined") continue;
1050
- ${variable}.push(decoded);
1054
+ `;
1055
+ if (property.range.length > 1) yield `
1056
+ if (typeof decoded === "undefined") {
1057
+ shouldCacheJsonLd = false;
1058
+ continue;
1059
+ }
1051
1060
  `;
1052
- }
1061
+ yield `
1062
+ if (!this._shouldCacheDecodedJsonLd(decoded)) {
1063
+ shouldCacheJsonLd = false;
1064
+ }
1065
+ ${variable}.push(decoded);
1066
+ `;
1053
1067
  yield `
1054
1068
  }
1055
1069
  instance.${await getFieldName(property.uri)} = ${variable};
1056
1070
  `;
1057
1071
  }
1058
1072
  yield `
1059
- if (!("_fromSubclass" in options) || !options._fromSubclass) {
1073
+ instance._shouldCacheJsonLd = shouldCacheJsonLd;
1074
+ if (
1075
+ shouldCacheJsonLd &&
1076
+ (!("_fromSubclass" in options) || !options._fromSubclass)
1077
+ ) {
1060
1078
  try {
1061
1079
  instance._cachedJsonLd = structuredClone(json);
1062
1080
  } catch {
@@ -1823,6 +1841,7 @@ async function* generateClass(typeUri, types) {
1823
1841
  values?: Record<string, unknown>;
1824
1842
  };
1825
1843
  #cachedJsonLd?: unknown;
1844
+ #shouldCacheJsonLd = true;
1826
1845
  readonly id: URL | null;
1827
1846
 
1828
1847
  protected get _documentLoader(): DocumentLoader | undefined {
@@ -1852,6 +1871,20 @@ async function* generateClass(typeUri, types) {
1852
1871
  protected set _cachedJsonLd(value: unknown | undefined) {
1853
1872
  this.#cachedJsonLd = value;
1854
1873
  }
1874
+
1875
+ protected get _shouldCacheJsonLd(): boolean {
1876
+ return this.#shouldCacheJsonLd;
1877
+ }
1878
+
1879
+ protected set _shouldCacheJsonLd(value: boolean) {
1880
+ this.#shouldCacheJsonLd = value;
1881
+ }
1882
+
1883
+ protected static _shouldCacheDecodedJsonLd(value: unknown): boolean {
1884
+ if (value == null || typeof value !== "object") return true;
1885
+ if (!("_shouldCacheJsonLd" in value)) return true;
1886
+ return (value as { _shouldCacheJsonLd: boolean })._shouldCacheJsonLd;
1887
+ }
1855
1888
  `;
1856
1889
  yield `
1857
1890
  /**
@@ -1904,6 +1937,17 @@ async function* generateClasses(types) {
1904
1937
  isTemporalDuration,
1905
1938
  isTemporalInstant,
1906
1939
  } from "@fedify/vocab-runtime/temporal";\n`;
1940
+ yield `
1941
+ function isValidLanguageTag(language: string): boolean {
1942
+ try {
1943
+ new Intl.Locale(language);
1944
+ return true;
1945
+ } catch (error) {
1946
+ if (error instanceof RangeError) return false;
1947
+ throw error;
1948
+ }
1949
+ }
1950
+ `;
1907
1951
  yield "\n\n";
1908
1952
  const sorted = sortTopologically(types);
1909
1953
  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.1.16";
10
+ var version = "2.1.18";
11
11
  //#endregion
12
12
  //#region src/type.ts
13
13
  const HEURISTICS_CONTEXTS = [
@@ -198,7 +198,8 @@ const scalarTypes = {
198
198
  dataCheck(v) {
199
199
  return `typeof ${v} === "object" && "@language" in ${v} && "@value" in ${v}
200
200
  && typeof ${v}["@language"] === "string"
201
- && typeof ${v}["@value"] === "string"`;
201
+ && typeof ${v}["@value"] === "string"
202
+ && isValidLanguageTag(${v}["@language"])`;
202
203
  },
203
204
  decoder(v) {
204
205
  return `new LanguageString(${v}["@value"], ${v}["@language"])`;
@@ -1003,6 +1004,9 @@ async function* generateDecoder(typeUri, types) {
1003
1004
  throw new TypeError("Unexpected type: " + instance.constructor.name);
1004
1005
  }
1005
1006
  `;
1007
+ yield `
1008
+ let shouldCacheJsonLd = instance._shouldCacheJsonLd;
1009
+ `;
1006
1010
  for (const property of type.properties) {
1007
1011
  const variable = await getFieldName(property.uri, "");
1008
1012
  yield await generateField(property, types, "const ");
@@ -1036,26 +1040,40 @@ async function* generateDecoder(typeUri, types) {
1036
1040
  continue;
1037
1041
  }
1038
1042
  `;
1039
- if (property.range.length == 1) yield `${variable}.push(${getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`)})`;
1040
- else {
1041
- yield `
1043
+ yield `
1042
1044
  const decoded =
1043
- `;
1045
+ `;
1046
+ if (property.range.length == 1) yield getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1047
+ else {
1044
1048
  const decoders = getDecoders(property.range, types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1045
1049
  for (const code of decoders) yield code;
1046
- yield `
1050
+ }
1051
+ yield `
1047
1052
  ;
1048
- if (typeof decoded === "undefined") continue;
1049
- ${variable}.push(decoded);
1053
+ `;
1054
+ if (property.range.length > 1) yield `
1055
+ if (typeof decoded === "undefined") {
1056
+ shouldCacheJsonLd = false;
1057
+ continue;
1058
+ }
1050
1059
  `;
1051
- }
1060
+ yield `
1061
+ if (!this._shouldCacheDecodedJsonLd(decoded)) {
1062
+ shouldCacheJsonLd = false;
1063
+ }
1064
+ ${variable}.push(decoded);
1065
+ `;
1052
1066
  yield `
1053
1067
  }
1054
1068
  instance.${await getFieldName(property.uri)} = ${variable};
1055
1069
  `;
1056
1070
  }
1057
1071
  yield `
1058
- if (!("_fromSubclass" in options) || !options._fromSubclass) {
1072
+ instance._shouldCacheJsonLd = shouldCacheJsonLd;
1073
+ if (
1074
+ shouldCacheJsonLd &&
1075
+ (!("_fromSubclass" in options) || !options._fromSubclass)
1076
+ ) {
1059
1077
  try {
1060
1078
  instance._cachedJsonLd = structuredClone(json);
1061
1079
  } catch {
@@ -1822,6 +1840,7 @@ async function* generateClass(typeUri, types) {
1822
1840
  values?: Record<string, unknown>;
1823
1841
  };
1824
1842
  #cachedJsonLd?: unknown;
1843
+ #shouldCacheJsonLd = true;
1825
1844
  readonly id: URL | null;
1826
1845
 
1827
1846
  protected get _documentLoader(): DocumentLoader | undefined {
@@ -1851,6 +1870,20 @@ async function* generateClass(typeUri, types) {
1851
1870
  protected set _cachedJsonLd(value: unknown | undefined) {
1852
1871
  this.#cachedJsonLd = value;
1853
1872
  }
1873
+
1874
+ protected get _shouldCacheJsonLd(): boolean {
1875
+ return this.#shouldCacheJsonLd;
1876
+ }
1877
+
1878
+ protected set _shouldCacheJsonLd(value: boolean) {
1879
+ this.#shouldCacheJsonLd = value;
1880
+ }
1881
+
1882
+ protected static _shouldCacheDecodedJsonLd(value: unknown): boolean {
1883
+ if (value == null || typeof value !== "object") return true;
1884
+ if (!("_shouldCacheJsonLd" in value)) return true;
1885
+ return (value as { _shouldCacheJsonLd: boolean })._shouldCacheJsonLd;
1886
+ }
1854
1887
  `;
1855
1888
  yield `
1856
1889
  /**
@@ -1903,6 +1936,17 @@ async function* generateClasses(types) {
1903
1936
  isTemporalDuration,
1904
1937
  isTemporalInstant,
1905
1938
  } from "@fedify/vocab-runtime/temporal";\n`;
1939
+ yield `
1940
+ function isValidLanguageTag(language: string): boolean {
1941
+ try {
1942
+ new Intl.Locale(language);
1943
+ return true;
1944
+ } catch (error) {
1945
+ if (error instanceof RangeError) return false;
1946
+ throw error;
1947
+ }
1948
+ }
1949
+ `;
1906
1950
  yield "\n\n";
1907
1951
  const sorted = sortTopologically(types);
1908
1952
  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.1.16",
3
+ "version": "2.1.18",
4
4
  "description": "Code generator for Activity Vocabulary APIs",
5
5
  "homepage": "https://fedify.dev/",
6
6
  "repository": {