@fedify/vocab-tools 2.4.0 → 2.4.2-dev.2347

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
@@ -58,6 +58,7 @@ const RUNTIME_IMPORTS = [
58
58
  "isDecimal",
59
59
  "isGatewayUrl",
60
60
  "LanguageString",
61
+ "normalizeLanguageTag",
61
62
  "parseDecimal",
62
63
  "parseGatewayUrl",
63
64
  "parseIri",
@@ -325,7 +326,7 @@ function inspectIri(iri: URL): string {
325
326
  yield `
326
327
  function isValidLanguageTag(language: string): boolean {
327
328
  try {
328
- new Intl.Locale(language);
329
+ new Intl.Locale(normalizeLanguageTag(language));
329
330
  return true;
330
331
  } catch (error) {
331
332
  if (error instanceof RangeError) return false;
@@ -179,7 +179,7 @@ export async function* generateConstructor(
179
179
  if (Array.isArray(values.${property.pluralName}) &&
180
180
  values.${property.pluralName}.every(v => ${typeGuards})) {
181
181
  // @ts-ignore: type is checked above.
182
- this.${fieldName} = values.${property.pluralName};
182
+ this.${fieldName} = values.${property.pluralName}.slice();
183
183
  `;
184
184
  if (!allScalarTypes) {
185
185
  yield `
@@ -211,6 +211,19 @@ export async function* generateCloner(
211
211
  /**
212
212
  * Clones this instance, optionally updating it with the given values.
213
213
  *
214
+ * This is a shallow copy: property arrays are copied, but nested objects
215
+ * and URLs are shared with this instance. Dereferencing accessors can cache
216
+ * fetched objects in the instance whose accessor is called. Hydrating a
217
+ * shared nested object through the clone can affect the original as well,
218
+ * and can change JSON-LD output when either is embedded in another
219
+ * object. Hydrating a top-level property of the clone does not update this
220
+ * instance's property.
221
+ *
222
+ * To resolve properties without sharing nested objects, serialize and
223
+ * re-parse the instance before hydration rather than using this method.
224
+ * See the [vocabulary manual](https://fedify.dev/manual/vocab#immutability)
225
+ * for details, including how cached JSON-LD affects serialization.
226
+ *
214
227
  * A clone never inherits a signed JSON-LD representation retained by
215
228
  * \`signObject()\`: the clone may differ from the document that the proof
216
229
  * covers, so embedding the original secured JSON in a parent document
@@ -219,7 +232,8 @@ export async function* generateCloner(
219
232
  *
220
233
  * @param values The values to update the clone with.
221
234
  * @param options The options to use for cloning.
222
- * @returns The cloned instance.
235
+ * @returns The cloned instance with its own property arrays. Nested objects
236
+ * and URLs are shared with this instance.
223
237
  */
224
238
  ${emitOverride(typeUri, types)} clone(
225
239
  values:
@@ -258,7 +272,7 @@ export async function* generateCloner(
258
272
  const fieldName = await getFieldName(property.uri);
259
273
  const trustFieldName = await getFieldName(property.uri, "#_trust");
260
274
  const allScalarTypes = areAllScalarTypes(property.range, types);
261
- yield `clone.${fieldName} = this.${fieldName};`;
275
+ yield `clone.${fieldName} = this.${fieldName}.slice();`;
262
276
  if (!allScalarTypes) {
263
277
  yield `clone.${trustFieldName} = new Set(this.${trustFieldName});`;
264
278
  }
@@ -322,7 +336,7 @@ export async function* generateCloner(
322
336
  if (Array.isArray(values.${property.pluralName}) &&
323
337
  values.${property.pluralName}.every(v => ${typeGuards})) {
324
338
  // @ts-ignore: type is checked above.
325
- clone.${fieldName} = values.${property.pluralName};
339
+ clone.${fieldName} = values.${property.pluralName}.slice();
326
340
  `;
327
341
  if (!allScalarTypes) {
328
342
  yield `
package/src/type.ts CHANGED
@@ -310,7 +310,7 @@ const scalarTypes: Record<string, ScalarType> = {
310
310
  && typeof ${v}["@value"] === "string" && !("@language" in ${v})`;
311
311
  },
312
312
  decoder(v) {
313
- return `new Intl.Locale(${v}["@value"])`;
313
+ return `new Intl.Locale(normalizeLanguageTag(${v}["@value"]))`;
314
314
  },
315
315
  },
316
316
  "fedify:url": {