]> Lady’s Gitweb - Etiquette/blob - model.js
9f7962dce8df217f77239f1ecbb730159bc34f7c
[Etiquette] / model.js
1 // 📧🏷️ Étiquette ∷ model.js
2 // ====================================================================
3 //
4 // Copyright © 2023 Lady [@ Lady’s Computer].
5 //
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
9
10 import { Storage } from "./memory.js";
11 import { taggingDiscoveryContext } from "./names.js";
12
13 // TODO: Move these somewhere else and allow for modification before
14 // they are used, freezing them only once tags actually start being
15 // constructed (probably on first call to the `TagSystem` initializer
16 // for convenience).
17 //
18 // Or else make them properties of the tag system itself and ∼fully
19 // modifiable.
20
21 /**
22 * Tag kinds which denote entity tags.
23 *
24 * ※ This object is not exposed.
25 */
26 const ENTITY_TAG_KINDS = new Set([
27 "EntityTag",
28 "CharacterTag",
29 "InanimateEntityTag",
30 ]);
31
32 /**
33 * Tag kinds which denote relationship tags.
34 *
35 * ※ This object is not exposed.
36 */
37 const RELATIONSHIP_TAG_KINDS = new Set([
38 "RelationshipTag",
39 "FamilialRelationship Tag",
40 "FriendshipTag",
41 "RivalryTag",
42 "RomanticRelationshipTag",
43 "SexualRelationshipTag",
44 ]);
45
46 /**
47 * Tag kinds which denote setting tags.
48 *
49 * ※ This object is not exposed.
50 */
51 const SETTING_TAG_KINDS = new Set([
52 "SettingTag",
53 "LocationTag",
54 "TimePeriodTag",
55 "UniverseTag",
56 ]);
57
58 /**
59 * Tag kinds which denote conceptual tags.
60 *
61 * ※ This object is not exposed.
62 */
63 const CONCEPTUAL_TAG_KINDS = new Set(function* () {
64 yield "ConceptualTag";
65 yield* RELATIONSHIP_TAG_KINDS;
66 }());
67
68 /**
69 * All recognized tag kinds.
70 *
71 * ※ This object is not exposed.
72 */
73 const TAG_KINDS = new Set(function* () {
74 yield "Tag";
75 yield "CanonTag";
76 yield* CONCEPTUAL_TAG_KINDS;
77 yield* ENTITY_TAG_KINDS;
78 yield "GenreTag";
79 yield* SETTING_TAG_KINDS;
80 }());
81
82 /**
83 * Tag kinds which can be in canon.
84 *
85 * ※ This object is not exposed.
86 */
87 const HAS_IN_CANON = new Set(function* () {
88 yield* ENTITY_TAG_KINDS;
89 yield* SETTING_TAG_KINDS;
90 }());
91
92 /**
93 * Tag kinds which can be involved in relationship tags.
94 *
95 * ※ This object is not exposed.
96 */
97 const INVOLVABLE_IN_RELATIONSHIP = new Set(function* () {
98 yield "CharacterTag";
99 yield* RELATIONSHIP_TAG_KINDS;
100 }());
101
102 /**
103 * Properties which take literal values instead of identifiers.
104 *
105 * These are the label terms.
106 */
107 const LITERAL_TERMS = new Set([
108 "prefLabel",
109 "altLabel",
110 "hiddenLabel",
111 ]);
112
113 /**
114 * Properties to skip when diffing.
115 *
116 * These are all inverses of properties included in diffs and cannot be
117 * changed manually.
118 */
119 const SKIP_IN_DIFF = new Set([
120 "hasInCanon",
121 "isIncludedIn",
122 "narrower",
123 ]);
124
125 /**
126 * A tag.
127 *
128 * `Tag`s are not assigned identifiers and do not have side·effects on
129 * other tags in the `TagSystem` until they are persisted with
130 * `::persist`, at which point changes to their relationships are
131 * applied.
132 *
133 * `Tag`s are also not kept up‐to‐date, but persisting an outdated
134 * `Tag` will *not* undo subsequent changes.
135 *
136 * ※ This class is not itself directly exposed, although bound
137 * versions of it are via `TagSystem::Tag`.
138 */
139 class Tag {
140 /** The `TagSystem` this `Tag` belongs to. */
141 #system;
142
143 /** The `Storage` managed by this `Tag`’s `TagSystem`. */
144 #storage;
145
146 /**
147 * The 30‐bit W·R·M·G base32 identifier with leading checksum which
148 * has been assigned to this `Tag`.
149 *
150 * Will be `null` if this `Tag` has not been persisted. Otherwise,
151 * the format is `cxx-xxxx` (`c` = checksum; `x` = digit).
152 */
153 #identifier = null;
154
155 /** The kind of this `Tag`. */
156 #kind = "Tag";
157
158 /**
159 * The data which was attached to this `Tag` the last time it was
160 * persisted or retrieved from storage.
161 *
162 * Diffing with this will reveal changes.
163 */
164 #persistedData = null;
165
166 /** The current (modified) data associated with this `Tag`. */
167 #data = tagData();
168
169 /**
170 * Returns whether or not the provided value is a tag which shares a
171 * storage with this tag.
172 *
173 * Sharing a storage also implies sharing a `TagSystem`.
174 */
175 #isTagInStorage($) {
176 try {
177 // Try to compare the provided value’s internal store with
178 // the provided storage.
179 return $.#storage == this.#storage;
180 } catch {
181 // The provided value was not a `Tag`.
182 return false;
183 }
184 }
185
186 /**
187 * Constructs a new `Tag` of the provided kind and with the provided
188 * preferred label.
189 *
190 * ※ The first two arguments of this constructor are bound when
191 * generating the value of `TagSystem::Tag`. It isn’t possible to
192 * access this constructor in its unbound form from outside this
193 * module.
194 *
195 * ☡ This constructor throws if the provided kind is not supported.
196 */
197 constructor(system, storage, kind = "Tag", prefLabel = "") {
198 const kindString = `${kind}`;
199 if (TAG_KINDS.has(kindString)) {
200 // The provided kind is one of the recognized tag kinds.
201 this.#system = system;
202 this.#storage = storage;
203 this.#kind = kindString;
204 this.#data.prefLabel = prefLabel;
205 } else {
206 // The provided kind is not supported.
207 throw new RangeError(
208 `Cannot construct Tag: Unrecognized kind: ${kind}.`,
209 );
210 }
211 }
212
213 /**
214 * Yields the tags in the `TagSystem` associated with this
215 * constructor.
216 *
217 * ※ The first two arguments of this function are bound when
218 * generating the value of `TagSystem::Tag`. It isn’t possible to
219 * access this function in its unbound form from outside this module.
220 */
221 static *all(system, storage) {
222 for (const instance of storage.values()) {
223 // Iterate over the entries and yield the ones which are `Tag`s
224 // in this `TagSystem`.
225 if (Tag.getSystem(instance) == system) {
226 // The current instance is a `Tag` in this `TagSystem`.
227 yield instance;
228 } else {
229 // The current instance is not a `Tag` in this `TagSystem`.
230 /* do nothing */
231 }
232 }
233 }
234
235 /**
236 * Returns a new `Tag` resolved from the provided I·R·I.
237 *
238 * ※ The first two arguments of this function are bound when
239 * generating the value of `TagSystem::Tag`. It isn’t possible to
240 * access this function in its unbound form from outside this module.
241 *
242 * ※ If the I·R·I is not recognized, this function returns `null`.
243 */
244 static fromIRI(system, storage, iri) {
245 const name = `${iri}`;
246 const prefix =
247 `https://${system.authorityName}/tag:${system.taggingEntity}:`;
248 if (!name.startsWith(prefix)) {
249 // The I·R·I does not begin with the expected prefix.
250 return null;
251 } else {
252 // The I·R·I begins with the expected prefix.
253 const identifier = name.substring(prefix.length);
254 try {
255 // Attempt to resolve the identifier.
256 const instance = storage.get(identifier);
257 return Tag.getSystem(instance) == system ? instance : null;
258 } catch {
259 // Do not throw for bad identifiers.
260 return null;
261 }
262 }
263 }
264
265 /**
266 * Returns a new `Tag` resolved from the provided identifier.
267 *
268 * ※ The first two arguments of this function are bound when
269 * generating the value of `TagSystem::Tag`. It isn’t possible to
270 * access this function in its unbound form from outside this module.
271 *
272 * ☡ This function throws if the identifier is invalid.
273 *
274 * ※ If the identifier is valid but not recognized, this function
275 * returns `null`.
276 */
277 static fromIdentifier(system, storage, identifier) {
278 const instance = storage.get(identifier);
279 return Tag.getSystem(instance) == system ? instance : null;
280 }
281
282 /**
283 * Returns a new `Tag` resolved from the provided Tag U·R·I.
284 *
285 * ※ The first two arguments of this function are bound when
286 * generating the value of `TagSystem::Tag`. It isn’t possible to
287 * access this function in its unbound form from outside this module.
288 *
289 * ☡ This function throws if the provided Tag U·R·I does not match
290 * the tagging entity of this constructor’s `TagSystem`.
291 *
292 * ※ If the specific component of the Tag U·R·I is not recognized,
293 * this function returns `null`.
294 */
295 static fromTagURI(system, storage, tagURI) {
296 const tagName = `${tagURI}`;
297 const tagPrefix = `tag:${system.taggingEntity}:`;
298 if (!tagName.startsWith(tagPrefix)) {
299 // The Tag U·R·I does not begin with the expected prefix.
300 throw new RangeError(
301 `Tag U·R·I did not begin with the expected prefix: ${tagName}`,
302 );
303 } else {
304 // The I·R·I begins with the expected prefix.
305 const identifier = tagName.substring(tagPrefix.length);
306 try {
307 // Attempt to resolve the identifier.
308 const instance = storage.get(identifier);
309 return Tag.getSystem(instance) == system ? instance : null;
310 } catch {
311 // Do not throw for bad identifiers.
312 return null;
313 }
314 }
315 }
316
317 /**
318 * Returns the `TagSystem` that the provided value belongs to.
319 *
320 * ※ This function can be used to check if the provided value has
321 * private tag features.
322 *
323 * ※ This function is not exposed.
324 */
325 static getSystem($) {
326 return !(#system in Object($)) ? null : $.#system;
327 }
328
329 /**
330 * Yields the tag identifiers in the `TagSystem` associated with this
331 * constructor.
332 *
333 * ※ The first two arguments of this function are bound when
334 * generating the value of `TagSystem::Tag`. It isn’t possible to
335 * access this function in its unbound form from outside this module.
336 */
337 static *identifiers(system, storage) {
338 for (const [identifier, instance] of storage.entries()) {
339 // Iterate over the entries and yield the ones which are `Tag`s
340 // in this `TagSystem`.
341 if (Tag.getSystem(instance) == system) {
342 // The current instance is a `Tag` in this `TagSystem`.
343 yield identifier;
344 } else {
345 // The current instance is not a `Tag` in this `TagSystem`.
346 /* do nothing */
347 }
348 }
349 }
350
351 /**
352 * Returns a new `Tag` constructed from the provided data and with
353 * the provided identifier.
354 *
355 * ※ This function will not work if called directly from `Tag` (and
356 * nor is it available *to* be called as such from outside this
357 * module). It must be called from a `TagSystem::Tag` bound
358 * constructor.
359 *
360 * ※ This function is not really intended for public usage.
361 */
362 static [Storage.toInstance](_system, _storage, data, identifier) {
363 const tag = new this(data.kind);
364 tag.#identifier = `${identifier}`;
365 tag.#persistedData = tagData(data);
366 tag.#data = tagData(data);
367 return tag;
368 }
369
370 static {
371 // Overwrite the default `::constructor` method to instead give the
372 // actual (bound) constructor which was used to generate a given
373 // `Tag`.
374 Object.defineProperties(this.prototype, {
375 constructor: {
376 configurable: true,
377 enumerable: false,
378 get() {
379 // All `Tag`s are constructed via the `.Tag` constructor
380 // available in their `TagSystem`; return it.
381 return this.#system.Tag;
382 },
383 set: undefined,
384 },
385 });
386 }
387
388 /** Adds the provided label(s) to this `Tag` as alternate labels. */
389 addAltLabel(...labels) {
390 const altLabels = this.#data.altLabel;
391 let objectLabels = null; // initialized on first use
392 for (const $ of labels) {
393 // Iterate over each provided label and attempt to add it.
394 const literal = langString($);
395 if (Object(literal) === literal) {
396 // The current label is a language‐tagged string.
397 objectLabels ??= [...function* () {
398 for (const altLabel of altLabels) {
399 // Iterate over the existing labels and yield the
400 // language‐tagged strings.
401 if (Object(altLabel) === altLabel) {
402 // The current existing label is a language‐tagged
403 // string.
404 yield altLabel;
405 } else {
406 // The current existing label is not a language‐tagged
407 // string.
408 /* do nothing */
409 }
410 }
411 }()];
412 if (
413 objectLabels.some((objectLabel) =>
414 objectLabel["@value"] == literal["@value"] &&
415 objectLabel["@language"] == literal["@language"]
416 )
417 ) {
418 // There is a match with the current label in the existing
419 // labels.
420 /* do nothing */
421 } else {
422 // There is no match and this label must be added.
423 altLabels.add(literal);
424 objectLabels.push(literal);
425 }
426 } else {
427 // The current label is a simple string.
428 altLabels.add(literal);
429 }
430 }
431 }
432
433 /**
434 * Adds the provided tags to the list of tags that this `Tag` is
435 * narrower than.
436 *
437 * Arguments may be string identifiers or objects with an
438 * `.identifier` property.
439 */
440 addBroaderTag(...tags) {
441 const storage = this.#storage;
442 const broader = this.#data.broader;
443 for (const $ of tags) {
444 // Iterate over each tag and attempt to set it as broader than
445 // this `Tag`.
446 const identifier = toIdentifier($);
447 if (identifier == null) {
448 // ☡ The current tag has no identifier.
449 throw new TypeError(
450 "Cannot assign broader to Tag: Identifier must not be nullish.",
451 );
452 } else if (broader.has(identifier)) {
453 // Short‐circuit: The identifier is already something this
454 // `Tag` is narrower than.
455 /* do nothing */
456 } else {
457 // The current tag has an identifier.
458 const tag = storage.get(identifier);
459 if (tag == null) {
460 // ☡ The current tag has not been persisted to this `Tag`’s
461 // storage.
462 throw new RangeError(
463 `Cannot assign broader to Tag: Identifier is not persisted: ${identifier}.`,
464 );
465 } else if (!this.#isTagInStorage(tag)) {
466 // ☡ The current tag is not a tag in the correct tag system.
467 throw new TypeError(
468 `Cannot assign broader to Tag: Tags must be from the same Tag System, but got: ${identifier}.`,
469 );
470 } else {
471 // The current tag is a tag in the correct tag system; add
472 // its identifier.
473 broader.add(identifier);
474 }
475 }
476 }
477 }
478
479 /** Adds the provided label(s) to this `Tag` as hidden labels. */
480 addHiddenLabel(...labels) {
481 const hiddenLabels = this.#data.hiddenLabel;
482 let objectLabels = null; // initialized on first use
483 for (const $ of labels) {
484 // Iterate over each provided label and attempt to add it.
485 const literal = langString($);
486 if (Object(literal) === literal) {
487 // The current label is a language‐tagged string.
488 objectLabels ??= [...function* () {
489 for (const hiddenLabel of hiddenLabels) {
490 // Iterate over the existing labels and yield the
491 // language‐tagged strings.
492 if (Object(hiddenLabel) === hiddenLabel) {
493 // The current existing label is a language‐tagged
494 // string.
495 yield hiddenLabel;
496 } else {
497 // The current existing label is not a language‐tagged
498 // string.
499 /* do nothing */
500 }
501 }
502 }()];
503 if (
504 objectLabels.some((objectLabel) =>
505 objectLabel["@value"] == literal["@value"] &&
506 objectLabel["@language"] == literal["@language"]
507 )
508 ) {
509 // There is a match with the current label in the existing
510 // labels.
511 /* do nothing */
512 } else {
513 // There is no match and this label must be added.
514 hiddenLabels.add(literal);
515 objectLabels.push(literal);
516 }
517 } else {
518 // The current label is a simple string.
519 hiddenLabels.add(literal);
520 }
521 }
522 }
523
524 /**
525 * Adds the provided tags to the list of tags that this `Tag` is in
526 * canon with.
527 *
528 * Arguments may be string identifiers or objects with an
529 * `.identifier` property.
530 *
531 * ☡ This method will throw if a provided argument does not indicate
532 * a canon tag, or if this `Tag` is not of a kind which can be placed
533 * in canon.
534 */
535 addInCanonTag(...tags) {
536 const storage = this.#storage;
537 const kind = this.#kind;
538 const inCanon = this.#data.inCanon;
539 if (!HAS_IN_CANON.has(kind)) {
540 // ☡ This is not an entity tag, setting tag, or recognized
541 // subclass.
542 throw new TypeError(
543 `Cannot put Tag in canon: Incorrect Tag type: ${kind}.`,
544 );
545 } else {
546 // This has a kind which can be placed in canon.
547 for (const $ of tags) {
548 // Iterate over each tag and attempt to set this `Tag` in canon
549 // of it.
550 const identifier = toIdentifier($);
551 if (identifier == null) {
552 // ☡ The current tag has no identifier.
553 throw new TypeError(
554 "Cannot put Tag in canon: Identifier must not be nullish.",
555 );
556 } else if (inCanon.has(identifier)) {
557 // Short‐circuit: The identifier is already something this
558 // `Tag` is in canon of.
559 /* do nothing */
560 } else {
561 // The current tag has an identifier.
562 const tag = storage.get(identifier);
563 if (tag == null) {
564 // ☡ The current tag has not been persisted to this `Tag`’s
565 // storage.
566 throw new RangeError(
567 `Cannot put Tag in canon: Identifier is not persisted: ${identifier}.`,
568 );
569 } else if (
570 // ※ If the first check succeeds, then the current tag
571 // must have `Tag` private class features.
572 !this.#isTagInStorage(tag) || tag.#kind != "CanonTag"
573 ) {
574 // ☡ The current tag is not a canon tag in the correct
575 // tag system.
576 throw new TypeError(
577 `Cannot put Tag in canon: Tags can only be in Canon Tags from the same Tag System, but got: ${identifier}.`,
578 );
579 } else {
580 // The current tag is a canon tag in the correct tag
581 // system; add its identifier.
582 inCanon.add(identifier);
583 }
584 }
585 }
586 }
587 }
588
589 /**
590 * Adds the provided tags to the list of tags that this `Tag`
591 * involves.
592 *
593 * Arguments may be string identifiers or objects with an
594 * `.identifier` property.
595 *
596 * ☡ This method will throw if this `Tag` is not a conceptual tag, or
597 * if this `Tag` is a relationship tag and a provided argument does
598 * not indicate a character or relationship tag.
599 */
600 addInvolvesTag(...tags) {
601 const storage = this.#storage;
602 const kind = this.#kind;
603 const involves = this.#data.involves;
604 if (!CONCEPTUAL_TAG_KINDS.has(kind)) {
605 // ☡ This is not a conceptual tag or recognized subclass.
606 throw new TypeError(
607 `Cannot involve Tag: Incorrect Tag type: ${kind}.`,
608 );
609 } else {
610 // This is a conceptual tag.
611 for (const $ of tags) {
612 // Iterate over each tag and attempt to set this `Tag` as
613 // involving it.
614 const identifier = toIdentifier($);
615 if (identifier == null) {
616 // ☡ The current tag has no identifier.
617 throw new TypeError(
618 "Cannot involve Tag: Identifier must not be nullish.",
619 );
620 } else if (involves.has(identifier)) {
621 // Short‐circuit: The identifier is already something this
622 // `Tag` involves.
623 /* do nothing */
624 } else {
625 // The current tag has an identifier.
626 const tag = storage.get(identifier);
627 if (tag == null) {
628 // ☡ The current tag has not been persisted to this `Tag`’s
629 // storage.
630 throw new RangeError(
631 `Cannot involve Tag: Identifier is not persisted: ${identifier}.`,
632 );
633 } else if (
634 // ※ If the first check succeeds, then the current tag
635 // must have `Tag` private class features.
636 !this.#isTagInStorage(tag) ||
637 RELATIONSHIP_TAG_KINDS.has(kind) &&
638 !INVOLVABLE_IN_RELATIONSHIP.has(tag.#kind)
639 ) {
640 // ☡ The current tag is in the correct tag system and
641 // includable.
642 throw new TypeError(
643 `Cannot involve Tag: Tags must be the same Tag System and involvable, but got: ${identifier}.`,
644 );
645 } else {
646 // The current tag is an involvable tag in the correct tag
647 // system; add its identifier.
648 involves.add(identifier);
649 }
650 }
651 }
652 }
653 }
654
655 /** Yields the alternative labels of this `Tag`. */
656 *altLabels() {
657 yield* this.#data.altLabel;
658 }
659
660 /** Returns the authority (domain) name for this `Tag`. */
661 get authorityName() {
662 return this.#system.authorityName;
663 }
664
665 /** Yields `Tag`s which are broader than this `Tag`. */
666 *broaderTags() {
667 const storage = this.#storage;
668 for (const identifier of this.#data.broader) {
669 // Iterate over the broader tags and yield them if possible.
670 const tag = storage.get(identifier);
671 if (!this.#isTagInStorage(tag)) {
672 // The broader tag no longer appears in storage; perhaps it was
673 // deleted.
674 /* do nothing */
675 } else {
676 // The broader tag exists and is constructable from storage.
677 yield tag;
678 }
679 }
680 }
681
682 /** Yields `Tag`s which are broader than this `Tag`, transitively. */
683 *broaderTransitiveTags() {
684 const storage = this.#storage;
685 const encountered = new Set();
686 let pending = new Set(this.#data.broader);
687 while (pending.size > 0) {
688 // Loop until all broader tags have been encountered.
689 const processing = pending;
690 pending = new Set();
691 for (const identifier of processing) {
692 // Iterate over the broader tags and yield them if possible.
693 if (!encountered.has(identifier)) {
694 // The broader tag has not been encountered before.
695 encountered.add(identifier);
696 const tag = storage.get(identifier);
697 if (!this.#isTagInStorage(tag)) {
698 // The broader tag no longer appears in storage; perhaps it
699 // was deleted.
700 /* do nothing */
701 } else {
702 // The broader tag exists and is constructable from
703 // storage.
704 yield tag;
705 for (const transitive of tag.#data.broader) {
706 // Iterate over the broader tags of the current broader
707 // tag and add them to pending as needed.
708 if (!encountered.has(transitive)) {
709 // The broader broader tag has not been encountered
710 // yet.
711 pending.add(transitive);
712 } else {
713 // The broader broader tag has already been
714 // encountered.
715 /* do nothing */
716 }
717 }
718 }
719 } else {
720 // The broader tag has already been encountered.
721 /* do nothing */
722 }
723 }
724 }
725 }
726
727 /**
728 * Removes the provided string label(s) from this `Tag` as alternate
729 * labels.
730 */
731 deleteAltLabel(...labels) {
732 const altLabels = this.#data.altLabel;
733 let objectLabels = null; // initialized on first use
734 for (const $ of labels) {
735 // Iterate over each provided label and attempt to remove it.
736 const literal = langString($);
737 if (Object(literal) === literal) {
738 // The current label is a language‐tagged string.
739 objectLabels ??= [...function* () {
740 for (const altLabel of altLabels) {
741 // Iterate over the existing labels and yield the
742 // language‐tagged strings.
743 if (Object(altLabel) === altLabel) {
744 // The current existing label is a language‐tagged
745 // string.
746 yield altLabel;
747 } else {
748 // The current existing label is not a language‐tagged
749 // string.
750 /* do nothing */
751 }
752 }
753 }()];
754 const existing = objectLabels.find((objectLabel) =>
755 objectLabel["@value"] == literal["@value"] &&
756 objectLabel["@language"] == literal["@language"]
757 );
758 altLabels.delete(existing);
759 } else {
760 // The current label is a simple string.
761 altLabels.delete(literal);
762 }
763 }
764 }
765
766 /**
767 * Removes the provided tags from the list of tags that this `Tag` is
768 * narrower than.
769 *
770 * Arguments may be string identifiers or objects with an
771 * `.identifier` property.
772 */
773 deleteBroaderTag(...tags) {
774 const broader = this.#data.broader;
775 for (const $ of tags) {
776 // Iterate over the provided tags and delete them.
777 broader.delete(toIdentifier($));
778 }
779 }
780
781 /**
782 * Removes the provided string label(s) from this `Tag` as hidden
783 * labels.
784 */
785 deleteHiddenLabel(...labels) {
786 const hiddenLabels = this.#data.hiddenLabel;
787 let objectLabels = null; // initialized on first use
788 for (const $ of labels) {
789 // Iterate over each provided label and attempt to remove it.
790 const literal = langString($);
791 if (Object(literal) === literal) {
792 // The current label is a language‐tagged string.
793 objectLabels ??= [...function* () {
794 for (const hiddenLabel of hiddenLabels) {
795 // Iterate over the existing labels and yield the
796 // language‐tagged strings.
797 if (Object(hiddenLabel) === hiddenLabel) {
798 // The current existing label is a language‐tagged
799 // string.
800 yield hiddenLabel;
801 } else {
802 // The current existing label is not a language‐tagged
803 // string.
804 /* do nothing */
805 }
806 }
807 }()];
808 const existing = objectLabels.find((objectLabel) =>
809 objectLabel["@value"] == literal["@value"] &&
810 objectLabel["@language"] == literal["@language"]
811 );
812 hiddenLabels.delete(existing);
813 } else {
814 // The current label is a simple string.
815 hiddenLabels.delete(literal);
816 }
817 }
818 }
819
820 /**
821 * Removes the provided tags from the list of tags that this `Tag` is
822 * in canon with.
823 *
824 * Arguments may be string identifiers or objects with an
825 * `.identifier` property.
826 */
827 deleteInCanonTag(...tags) {
828 const inCanon = this.#data.inCanon;
829 for (const $ of tags) {
830 // Iterate over the provided tags and delete them.
831 inCanon.delete(toIdentifier($));
832 }
833 }
834
835 /**
836 * Removes the provided tags from the list of tags that this `Tag`
837 * involves.
838 *
839 * Arguments may be string identifiers or objects with an
840 * `.identifier` property.
841 */
842 deleteInvolvesTag(...tags) {
843 const involves = this.#data.involves;
844 for (const $ of tags) {
845 // Iterate over the provided tags and delete them.
846 involves.delete(toIdentifier($));
847 }
848 }
849
850 /** Yields `Tag`s that are in canon of this `Tag`. */
851 *hasInCanonTags() {
852 const storage = this.#storage;
853 if (this.#kind == "CanonTag") {
854 // This is a canon tag.
855 for (const identifier of this.#data.hasInCanon) {
856 // Iterate over the tags in canon and yield them if possible.
857 const tag = storage.get(identifier);
858 if (
859 !this.#isTagInStorage(tag) || !HAS_IN_CANON.has(tag.#kind)
860 ) {
861 // The tag in canon no longer appears in storage; perhaps it
862 // was deleted.
863 /* do nothing */
864 } else {
865 // The tag in canon exists and is constructable from storage.
866 yield tag;
867 }
868 }
869 } else {
870 /* do nothing */
871 }
872 }
873
874 /** Yields the hidden labels of this `Tag`. */
875 *hiddenLabels() {
876 yield* this.#data.hiddenLabel;
877 }
878
879 /** Returns the identifier of this `Tag`. */
880 get identifier() {
881 return this.#identifier;
882 }
883
884 /** Yields `Tag`s that this `Tag` is in canon of. */
885 *inCanonTags() {
886 const storage = this.#storage;
887 if (HAS_IN_CANON.has(this.#kind)) {
888 // This tag can be placed in canon.
889 for (const identifier of this.#data.inCanon) {
890 // Iterate over the canon tags and yield them if possible.
891 const tag = storage.get(identifier);
892 if (!this.#isTagInStorage(tag) || tag.#kind != "CanonTag") {
893 // The canon tag no longer appears in storage; perhaps it was
894 // deleted.
895 /* do nothing */
896 } else {
897 // The canon tag exists and is constructable from storage.
898 yield tag;
899 }
900 }
901 } else {
902 // This tag cannot be placed in canon.
903 /* do nothing */
904 }
905 }
906
907 /** Yields `Tag`s which involve this `Tag`. */
908 *involvedInTags() {
909 const storage = this.#storage;
910 for (const identifier of this.#data.involvedIn) {
911 // Iterate over the involving tags and yield them if possible.
912 const tag = storage.get(identifier);
913 const tagKind = tag.#kind;
914 if (
915 !this.#isTagInStorage(tag) ||
916 !CONCEPTUAL_TAG_KINDS.has(tagKind) ||
917 RELATIONSHIP_TAG_KINDS.has(tagKind) &&
918 !INVOLVABLE_IN_RELATIONSHIP.has(this.#kind)
919 ) {
920 // The including tag no longer appears in storage; perhaps it
921 // was deleted.
922 /* do nothing */
923 } else {
924 // The including tag exists and is constructable from storage.
925 yield tag;
926 }
927 }
928 }
929
930 /** Yields `Tag`s that this `Tag` involves. */
931 *involvesTags() {
932 const storage = this.#storage;
933 const kind = this.#kind;
934 if (CONCEPTUAL_TAG_KINDS.has(kind)) {
935 // This tag can involve other tags.
936 for (const identifier of this.#data.involves) {
937 // Iterate over the involved and yield them if possible.
938 const tag = storage.get(identifier);
939 if (
940 !this.#isTagInStorage(tag) ||
941 RELATIONSHIP_TAG_KINDS.has(kind) &&
942 !INVOLVABLE_IN_RELATIONSHIP.has(tag.#kind)
943 ) {
944 // The involved tag no longer appears in storage; perhaps it
945 // was deleted.
946 /* do nothing */
947 } else {
948 // The involved tag exists and is constructable from storage.
949 yield tag;
950 }
951 }
952 } else {
953 // This tag cannot involve other tags.
954 /* do nothing */
955 }
956 }
957
958 /** Returns the I·R·I for this `Tag`. */
959 get iri() {
960 const tagURI = this.tagURI;
961 return tagURI == null
962 ? null
963 : `https://${this.authorityName}/${tagURI}`;
964 }
965
966 /** Returns the kind of this `Tag`. */
967 get kind() {
968 return this.#kind;
969 }
970
971 /** Yields `Tag`s which are narrower than this `Tag`. */
972 *narrowerTags() {
973 const storage = this.#storage;
974 for (const identifier of this.#data.narrower) {
975 const tag = storage.get(identifier);
976 if (!this.#isTagInStorage(tag)) {
977 // The narrower tag no longer appears in storage; perhaps it
978 // was deleted.
979 /* do nothing */
980 } else {
981 // The narrower tag exists and is constructable from storage.
982 yield tag;
983 }
984 }
985 }
986
987 /**
988 * Yields `Tag`s which are narrower than this `Tag`, transitively.
989 */
990 *narrowerTransitiveTags() {
991 const storage = this.#storage;
992 const encountered = new Set();
993 let pending = new Set(this.#data.narrower);
994 while (pending.size > 0) {
995 // Loop until all narrower tags have been encountered.
996 const processing = pending;
997 pending = new Set();
998 for (const identifier of processing) {
999 // Iterate over the narrower tags and yield them if possible.
1000 if (!encountered.has(identifier)) {
1001 // The narrower tag has not been encountered before.
1002 encountered.add(identifier);
1003 const tag = storage.get(identifier);
1004 if (!this.#isTagInStorage(tag)) {
1005 // The narrower tag no longer appears in storage; perhaps
1006 // it was deleted.
1007 /* do nothing */
1008 } else {
1009 // The narrower tag exists and is constructable from
1010 // storage.
1011 yield tag;
1012 for (const transitive of tag.#data.narrower) {
1013 // Iterate over the narrower tags of the current narrower
1014 // tag and add them to pending as needed.
1015 if (!encountered.has(transitive)) {
1016 // The narrower narrower tag has not been encountered
1017 // yet.
1018 pending.add(transitive);
1019 } else {
1020 // The narrower narrower tag has already been
1021 // encountered.
1022 /* do nothing */
1023 }
1024 }
1025 }
1026 } else {
1027 // The narrower tag has already been encountered.
1028 /* do nothing */
1029 }
1030 }
1031 }
1032 }
1033
1034 /**
1035 * Persist this `Tag` to storage and return an ActivityStreams
1036 * serialization of a Tag Activity representing any changes, or
1037 * `null` if no changes were made.
1038 *
1039 * ※ Persistence can imply side‐effects on other objects, which are
1040 * not noted explicitly in the activity. For example, marking a tag
1041 * as broader than another causes the other tag to reciprocally be
1042 * marked as narrower.
1043 *
1044 * ※ The inverse terms `hasInCanon`, `isIncludedIn`, and `narrower`
1045 * will never appear in the predicates of generated activities.
1046 */
1047 persist() {
1048 const system = this.#system;
1049 const storage = this.#storage;
1050 const persistedData = this.#persistedData;
1051 const data = this.#data;
1052 const diffs = {};
1053 for (const [key, value] of Object.entries(data)) {
1054 // Iterate over each entry of the tag data and create a diff with
1055 // the last persisted information.
1056 if (SKIP_IN_DIFF.has(key)) {
1057 // The current property is one which is skipped in diffs.
1058 /* do nothing */
1059 } else {
1060 // The current property should be diffed.
1061 const persisted = persistedData?.[key] ?? null;
1062 if (persisted == null) {
1063 // There is no persisted data for the current property yet.
1064 diffs[key] = {
1065 old: new Set(),
1066 new: value instanceof Set
1067 ? new Set(value)
1068 : new Set([value]),
1069 };
1070 } else if (value instanceof Set) {
1071 // The current property is set‐valued.
1072 let values = null; // initialized on first use
1073 const oldValues = new Set(persisted);
1074 const newValues = new Set(value);
1075 for (const existing of persisted) {
1076 // Iterate over each persisted property and either remove
1077 // it from the list of new values or add it to the list of
1078 // removed ones.
1079 //
1080 // ※ Some special handling is required here for
1081 // language‐tagged strings.
1082 if (
1083 value.has(existing) ||
1084 Object(existing) === existing &&
1085 (values ??= [...value]).some(($) =>
1086 `${$}` == `${existing}` &&
1087 $.language == existing.language
1088 )
1089 ) {
1090 // The value is in both the old and new version of the
1091 // data.
1092 oldValues.delete(existing);
1093 newValues.delete(existing);
1094 } else {
1095 // The value is not shared.
1096 /* do nothing */
1097 }
1098 }
1099 diffs[key] = {
1100 old: oldValues,
1101 new: newValues,
1102 };
1103 } else if (
1104 `${value}` != `${persisted}` ||
1105 value.language != persisted.language
1106 ) {
1107 // The current property is (optionally language‐tagged)
1108 // string‐valued and the value changed.
1109 diffs[key] = {
1110 old: new Set([persisted]),
1111 new: new Set([value]),
1112 };
1113 } else {
1114 // The current property did not change.
1115 diffs[key] = {
1116 old: new Set(),
1117 new: new Set(),
1118 };
1119 }
1120 }
1121 }
1122 const identifier = this.#identifier;
1123 if (identifier != null) {
1124 // This `Tag` has already been persisted; use its existing
1125 // identifier and persist.
1126 storage.set(identifier, this);
1127 } else {
1128 // This `Tag` has not been persisted yet; save the new
1129 // identifier after persisting.
1130 this.#identifier = storage.add(this);
1131 }
1132 const persistedIdentifier = this.#identifier;
1133 this.#persistedData = tagData(data); // need to clone here
1134 for (
1135 const [term, inverse] of [
1136 ["broader", "narrower"],
1137 ["inCanon", "hasInCanon"],
1138 ["involves", "involvedIn"],
1139 ]
1140 ) {
1141 // Iterate over each term referencing other tags and update the
1142 // inverse property on those tags if possible.
1143 for (const referencedIdentifier of diffs[term].old) {
1144 // Iterate over the removed tags and remove this `Tag` from
1145 // their inverse property.
1146 const referenced = storage.get(referencedIdentifier);
1147 try {
1148 // Try removing this `Tag`.
1149 referenced.#data[inverse].delete(persistedIdentifier);
1150 storage.set(referencedIdentifier, referenced);
1151 } catch {
1152 // Removal failed, possibly because the other tag was
1153 // deleted.
1154 /* do nothing */
1155 }
1156 }
1157 for (const referencedIdentifier of diffs[term].new) {
1158 const referenced = storage.get(referencedIdentifier);
1159 try {
1160 // Try adding this `Tag`.
1161 referenced.#data[inverse].add(persistedIdentifier);
1162 storage.set(referencedIdentifier, referenced);
1163 } catch {
1164 // Adding failed, possibly because the other tag was deleted.
1165 /* do nothing */
1166 }
1167 }
1168 }
1169 const activity = {
1170 "@context": taggingDiscoveryContext,
1171 "@type": [
1172 "TagActivity",
1173 identifier == null ? "Create" : "Update",
1174 ],
1175 context: `${system.iri}`,
1176 object: `${this.iri}`,
1177 endTime: new Date().toISOString(),
1178 ...(() => {
1179 const statements = {
1180 unstates: [],
1181 states: [],
1182 };
1183 const { unstates, states } = statements;
1184 if (identifier == null) {
1185 // This is a Create activity.
1186 states.push({ predicate: "a", object: `${this.kind}` });
1187 } else {
1188 // This is an Update activity.
1189 /* do nothing */
1190 }
1191 for (
1192 const [term, {
1193 old: oldValues,
1194 new: newValues,
1195 }] of Object.entries(diffs)
1196 ) {
1197 // Iterate over the diffs of each term and state/unstate
1198 // things as needed.
1199 for (const oldValue of oldValues) {
1200 // Iterate over removals and unstate them.
1201 if (LITERAL_TERMS.has(term)) {
1202 // This is a literal term; push the change wrapped in an
1203 // object.
1204 unstates.push({
1205 predicate: term,
1206 object: Object(oldValue) === oldValue
1207 ? { ...langString(oldValue) }
1208 : { "@value": `${oldValue}` },
1209 });
1210 } else {
1211 // This is a named term; attempt to get its I·R·I and
1212 // push it.
1213 try {
1214 // Attempt to resolve the value and push the change.
1215 const tag = storage.get(oldValue);
1216 if (!this.#isTagInStorage(tag)) {
1217 // The value did not resolve to a tag in storage.
1218 /* do nothing */
1219 } else {
1220 // The value resolved; push its I·R·I.
1221 unstates.push({
1222 predicate: term,
1223 object: tag.iri,
1224 });
1225 }
1226 } catch {
1227 // Value resolution failed for some reason; perhaps the
1228 // tag was deleted.
1229 /* do nothing */
1230 }
1231 }
1232 }
1233 for (const newValue of newValues) {
1234 // Iterate over additions and state them.
1235 if (LITERAL_TERMS.has(term)) {
1236 // This is a literal term; push the change wrapped in an
1237 // object.
1238 states.push({
1239 predicate: term,
1240 object: Object(newValue) === newValue
1241 ? { ...langString(newValue) }
1242 : { "@value": `${newValue}` },
1243 });
1244 } else {
1245 // This is a named term; attempt to get its I·R·I and
1246 // push it.
1247 try {
1248 // Attempt to resolve the value and push the change.
1249 const tag = storage.get(newValue);
1250 if (!this.#isTagInStorage(tag)) {
1251 // The value did not resolve to a tag in storage.
1252 /* do nothing */
1253 } else {
1254 // The value resolved; push its I·R·I.
1255 states.push({
1256 predicate: term,
1257 object: tag.iri,
1258 });
1259 }
1260 } catch {
1261 // Value resolution failed for some reason; perhaps the
1262 // tag was deleted.
1263 /* do nothing */
1264 }
1265 }
1266 }
1267 }
1268 if (unstates.length == 0) {
1269 // Nothing was unstated.
1270 delete statements.unstates;
1271 } else {
1272 // Things were stated.
1273 /* do nothing */
1274 }
1275 if (states.length == 0) {
1276 // Nothing was stated.
1277 delete statements.states;
1278 } else {
1279 // Things were stated.
1280 /* do nothing */
1281 }
1282 return statements;
1283 })(),
1284 };
1285 if (
1286 !Object.hasOwn(activity, "states") &&
1287 !Object.hasOwn(activity, "unstates")
1288 ) {
1289 // No meaningful changes were actually persisted.
1290 return null;
1291 } else {
1292 // There were meaningful changes persisted regarding this `Tag`.
1293 return activity;
1294 }
1295 }
1296
1297 /** Returns the preferred label for this `Tag`. */
1298 get prefLabel() {
1299 return this.#data.prefLabel;
1300 }
1301
1302 /** Sets the preferred label of this `Tag` to the provided label. */
1303 set prefLabel($) {
1304 this.#data.prefLabel = langString($);
1305 }
1306
1307 /** Returns the Tag U·R·I for this `Tag`. */
1308 get tagURI() {
1309 const { identifier } = this;
1310 return identifier == null
1311 ? null
1312 : `tag:${this.taggingEntity}:${identifier}`;
1313 }
1314
1315 /** Returns the tagging entity (domain and date) for this `Tag`. */
1316 get taggingEntity() {
1317 return this.#system.taggingEntity;
1318 }
1319
1320 /** Returns the string form of the preferred label of this `Tag`. */
1321 toString() {
1322 return `${this.#data.prefLabel}`;
1323 }
1324
1325 /**
1326 * Returns a new object whose enumerable own properties contain the
1327 * data from this object needed for storage.
1328 *
1329 * ※ This method is not really intended for public usage.
1330 */
1331 [Storage.toObject]() {
1332 const data = this.#data;
1333 return Object.assign(Object.create(null), {
1334 ...data,
1335 kind: this.#kind,
1336 });
1337 }
1338 }
1339
1340 const {
1341 /**
1342 * Returns the provided value converted into either a plain string
1343 * primitive or an object with `.["@value"]` and `.["@language"]`
1344 * properties.
1345 *
1346 * TODO: Ideally this would be extracted more fully into an R·D·F
1347 * library.
1348 *
1349 * ※ This function is not exposed.
1350 */
1351 langString,
1352 } = (() => {
1353 /** Returns the `.["@language"]` of this object. */
1354 const getLanguage = Object.defineProperty(
1355 function () {
1356 return this["@language"];
1357 },
1358 "name",
1359 { value: "get language" },
1360 );
1361
1362 /** Returns the `.["@value"]` of this object. */
1363 const toString = function () {
1364 return this["@value"];
1365 };
1366
1367 /** Returns the `.["@value"]` of this object. */
1368 const valueOf = function () {
1369 return this["@value"];
1370 };
1371
1372 return {
1373 langString: ($) =>
1374 Object($) === $
1375 ? "@value" in $
1376 ? "@language" in $
1377 ? Object.preventExtensions(
1378 Object.create(String.prototype, {
1379 "@value": {
1380 enumerable: true,
1381 value: `${$["@value"]}`,
1382 },
1383 "@language": {
1384 enumerable: true,
1385 value: `${$["@language"]}`,
1386 },
1387 language: { enumerable: false, get: getLanguage },
1388 toString: { enumerable: false, value: toString },
1389 valueOf: { enumerable: false, value: valueOf },
1390 }),
1391 )
1392 : `${$["@value"]}`
1393 : "language" in $
1394 ? Object.preventExtensions(
1395 Object.create(String.prototype, {
1396 "@value": { enumerable: true, value: `${$}` },
1397 "@language": {
1398 enumerable: true,
1399 value: `${$.language}`,
1400 },
1401 language: { enumerable: false, get: getLanguage },
1402 toString: { enumerable: false, value: toString },
1403 valueOf: { enumerable: false, value: valueOf },
1404 }),
1405 )
1406 : `${$}`
1407 : `${$ ?? ""}`,
1408 };
1409 })();
1410
1411 /**
1412 * Returns a normalized tag data object derived from the provided
1413 * object.
1414 *
1415 * ※ The properties of this function need to match the term names used
1416 * in the ActivityStreams serialization.
1417 *
1418 * ※ This function is not exposed.
1419 */
1420 const tagData = ($) => {
1421 const data = Object($);
1422 const {
1423 // prefLabel intentionally not set here
1424 altLabel,
1425 hiddenLabel,
1426 broader,
1427 narrower,
1428 inCanon,
1429 hasInCanon,
1430 involves,
1431 involvedIn,
1432 } = data;
1433 let prefLabel = langString(data.prefLabel);
1434 return Object.preventExtensions(Object.create(null, {
1435 prefLabel: {
1436 enumerable: true,
1437 get: () => prefLabel,
1438 set: ($) => {
1439 prefLabel = langString($);
1440 },
1441 },
1442 altLabel: {
1443 enumerable: true,
1444 value: new Set(
1445 altLabel != null
1446 ? Array.from(altLabel, langString)
1447 : undefined,
1448 ),
1449 },
1450 hiddenLabel: {
1451 enumerable: true,
1452 value: new Set(
1453 hiddenLabel != null
1454 ? Array.from(hiddenLabel, langString)
1455 : undefined,
1456 ),
1457 },
1458 broader: {
1459 enumerable: true,
1460 value: new Set(
1461 broader != null
1462 ? Array.from(broader, toIdentifier)
1463 : undefined,
1464 ),
1465 },
1466 narrower: {
1467 enumerable: true,
1468 value: new Set(
1469 narrower != null
1470 ? Array.from(narrower, toIdentifier)
1471 : undefined,
1472 ),
1473 },
1474 inCanon: {
1475 enumerable: true,
1476 value: new Set(
1477 inCanon != null
1478 ? Array.from(inCanon, toIdentifier)
1479 : undefined,
1480 ),
1481 },
1482 hasInCanon: {
1483 enumerable: true,
1484 value: new Set(
1485 hasInCanon != null
1486 ? Array.from(hasInCanon, toIdentifier)
1487 : undefined,
1488 ),
1489 },
1490 involves: {
1491 enumerable: true,
1492 value: new Set(
1493 involves != null
1494 ? Array.from(involves, toIdentifier)
1495 : undefined,
1496 ),
1497 },
1498 involvedIn: {
1499 enumerable: true,
1500 value: new Set(
1501 involvedIn != null
1502 ? Array.from(involvedIn, toIdentifier)
1503 : undefined,
1504 ),
1505 },
1506 }));
1507 };
1508
1509 /**
1510 * Returns an identifier corresponding to the provided object.
1511 *
1512 * This is either the value of its `.identifier` or its string value.
1513 *
1514 * ※ This function is not exposed.
1515 */
1516 const toIdentifier = ($) =>
1517 $ == null
1518 ? null
1519 : Object($) === $ && "identifier" in $
1520 ? $.identifier
1521 : `${$}`;
1522
1523 /**
1524 * A tag system, with storage.
1525 *
1526 * The `::Tag` constructor available on any `TagSystem` instance can be
1527 * used to create new `Tag`s within the system.
1528 */
1529 export class TagSystem {
1530 /** The cached bound `Tag` constructor for this `TagSystem`. */
1531 #Tag = null;
1532
1533 /** The domain of this `TagSystem`. */
1534 #domain;
1535
1536 /** The date of this `TagSystem`. */
1537 #date;
1538
1539 /** The identifier of this `TagSystem`. */
1540 #identifier;
1541
1542 /** The internal `Storage` of this `TagSystem`. */
1543 #storage = new Storage();
1544
1545 /**
1546 * Constructs a new `TagSystem` with the provided domain and date.
1547 *
1548 * Only actual, lowercased domain names are allowed for the domain,
1549 * and the date must be “full” (include month and day components).
1550 * This is for alignment with general best practices for Tag URI’s.
1551 *
1552 * ☡ This constructor throws if provided with an invalid date.
1553 */
1554 constructor(domain, date, identifier = "") {
1555 const domainString = `${domain}`;
1556 const dateString = `${date}`;
1557 this.#identifier = `${identifier}`;
1558 try {
1559 // If the identifier is a valid storage I·D, reserve it.
1560 this.#storage.delete(this.#identifier);
1561 } catch {
1562 // The identifier is not a valid storage I·D, so no worries.
1563 /* do nothing */
1564 }
1565 if (
1566 !/^[a-z](?:[\da-z-]*[\da-z])?(?:\.[a-z](?:[\da-z-]*[\da-z])?)*$/u
1567 .test(domainString)
1568 ) {
1569 // ☡ The domain is invalid.
1570 throw new RangeError(`Invalid domain: ${domain}.`);
1571 } else if (
1572 !/^\d{4}-\d{2}-\d{2}$/u.test(dateString) ||
1573 dateString != new Date(dateString).toISOString().split("T")[0]
1574 ) {
1575 // ☡ The date is invalid.
1576 throw new RangeError(`Invalid date: ${date}.`);
1577 } else {
1578 // The domain and date are 🆗.
1579 this.#domain = domainString;
1580 this.#date = dateString;
1581 }
1582 }
1583
1584 /**
1585 * Returns a bound constructor for constructing `Tags` in this
1586 * `TagSystem`.
1587 */
1588 get Tag() {
1589 if (this.#Tag != null) {
1590 // A bound constructor has already been generated; return it.
1591 return this.#Tag;
1592 } else {
1593 // No bound constructor has been created yet.
1594 const storage = this.#storage;
1595 const BoundTag = Tag.bind(undefined, this, storage);
1596 return this.#Tag = Object.defineProperties(BoundTag, {
1597 all: {
1598 configurable: true,
1599 enumerable: false,
1600 value: Tag.all.bind(BoundTag, this, storage),
1601 writable: true,
1602 },
1603 fromIRI: {
1604 configurable: true,
1605 enumerable: false,
1606 value: Tag.fromIRI.bind(BoundTag, this, storage),
1607 writable: true,
1608 },
1609 fromIdentifier: {
1610 configurable: true,
1611 enumerable: false,
1612 value: Tag.fromIdentifier.bind(BoundTag, this, storage),
1613 writable: true,
1614 },
1615 fromTagURI: {
1616 configurable: true,
1617 enumerable: false,
1618 value: Tag.fromTagURI.bind(BoundTag, this, storage),
1619 writable: true,
1620 },
1621 identifiers: {
1622 configurable: true,
1623 enumerable: false,
1624 value: Tag.identifiers.bind(BoundTag, this, storage),
1625 writable: true,
1626 },
1627 name: { value: `${this.tagURI}#${Tag.name}` },
1628 prototype: { value: Tag.prototype },
1629 [Storage.toInstance]: {
1630 configurable: true,
1631 enumerable: false,
1632 value: Tag[Storage.toInstance].bind(BoundTag, this, storage),
1633 writable: true,
1634 },
1635 });
1636 }
1637 }
1638
1639 /** Returns the authority name (domain) for this `TagSystem`. */
1640 get authorityName() {
1641 return this.#domain;
1642 }
1643
1644 /** Returns the date of this `TagSystem`, as a string. */
1645 get date() {
1646 return this.#date;
1647 }
1648
1649 /**
1650 * Yields the entities in this `TagSystem`.
1651 *
1652 * ※ Entities can hypothetically be anything. If you specifically
1653 * want the `Tag`s, use `::Tag.all` instead.
1654 */
1655 *entities() {
1656 yield* this.#storage.values();
1657 }
1658
1659 /**
1660 * Returns the identifier of this `TagSystem`.
1661 *
1662 * ※ Often this is just the empty string.
1663 */
1664 get identifier() {
1665 return this.#identifier;
1666 }
1667
1668 /** Yields the identifiers in use in this `TagSystem`. */
1669 *identifiers() {
1670 yield* this.#storage.keys();
1671 }
1672
1673 /** Returns the I·R·I for this `TagSystem`. */
1674 get iri() {
1675 return `https://${this.authorityName}/${this.tagURI}`;
1676 }
1677
1678 /** Returns the Tag U·R·I for this `TagSystem`. */
1679 get tagURI() {
1680 return `tag:${this.taggingEntity}:${this.identifier}`;
1681 }
1682
1683 /**
1684 * Returns the tagging entity (domain and date) for this `TagSystem`.
1685 */
1686 get taggingEntity() {
1687 return `${this.authorityName},${this.date}`;
1688 }
1689 }
This page took 0.207651 seconds and 3 git commands to generate.