@swagger-api/apidom-core 0.82.2 → 0.83.0

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.
@@ -3,7 +3,7 @@ class CloneError extends ApiDOMStructuredError {
3
3
  constructor(message, structuredOptions) {
4
4
  super(message, structuredOptions);
5
5
  if (typeof structuredOptions !== 'undefined') {
6
- this.value = structuredOptions.source;
6
+ this.value = structuredOptions.value;
7
7
  }
8
8
  }
9
9
  }
@@ -0,0 +1,10 @@
1
+ import { ApiDOMStructuredError } from '@swagger-api/apidom-error';
2
+ class ElementIdentityError extends ApiDOMStructuredError {
3
+ constructor(message, structuredOptions) {
4
+ super(message, structuredOptions);
5
+ if (typeof structuredOptions !== 'undefined') {
6
+ this.value = structuredOptions.value;
7
+ }
8
+ }
9
+ }
10
+ export default ElementIdentityError;
@@ -0,0 +1,58 @@
1
+ import { StringElement } from 'minim';
2
+ import stampit from 'stampit';
3
+ import ShortUniqueId from 'short-unique-id';
4
+ import ElementIdentityError from "./errors/ElementIdentityError.mjs";
5
+ import { isElement, isStringElement } from "../predicates/index.mjs";
6
+ export const IdentityManager = stampit({
7
+ props: {
8
+ uuid: null,
9
+ length: null,
10
+ identityMap: null
11
+ },
12
+ init({
13
+ length = 6
14
+ } = {}) {
15
+ this.length = 6;
16
+ this.uuid = new ShortUniqueId({
17
+ length
18
+ });
19
+ this.identityMap = new WeakMap();
20
+ },
21
+ methods: {
22
+ identify(element) {
23
+ if (!isElement(element)) {
24
+ throw new ElementIdentityError('Cannot not identify the element. `element` is neither structurally compatible nor a subclass of an Element class.', {
25
+ value: element
26
+ });
27
+ }
28
+
29
+ // use already assigned identity
30
+ if (element.meta.hasKey('id') && isStringElement(element.meta.id) && !element.meta.id.equals('')) {
31
+ return element.id;
32
+ }
33
+
34
+ // assign identity in immutable way
35
+ if (this.identityMap.has(element)) {
36
+ return this.identityMap.get(element);
37
+ }
38
+
39
+ // return element identity
40
+ const id = new StringElement(this.generateId());
41
+ this.identityMap.set(element, id);
42
+ return id;
43
+ },
44
+ forget(element) {
45
+ if (this.identityMap.has(element)) {
46
+ this.identityMap.delete(element);
47
+ return true;
48
+ }
49
+ return false;
50
+ },
51
+ generateId() {
52
+ return this.uuid.randomUUID();
53
+ }
54
+ }
55
+ });
56
+ export const defaultIdentityManager = IdentityManager({
57
+ length: 6
58
+ });
package/es/index.mjs CHANGED
@@ -15,6 +15,8 @@ export { cloneShallow, cloneDeep } from "./clone/index.mjs";
15
15
  export { default as CloneError } from "./clone/errors/CloneError.mjs";
16
16
  export { default as DeepCloneError } from "./clone/errors/DeepCloneError.mjs";
17
17
  export { default as ShallowCloneError } from "./clone/errors/ShallowCloneError.mjs";
18
+ export { defaultIdentityManager, IdentityManager } from "./identity/index.mjs";
19
+ export { default as ElementIdentityError } from "./identity/errors/ElementIdentityError.mjs";
18
20
  /**
19
21
  * Transforms data to an Element from a particular namespace.
20
22
  */
@@ -1,28 +1,26 @@
1
- import ShortUniqueId from 'short-unique-id';
2
1
  import { StringElement } from 'minim';
3
-
2
+ import { IdentityManager } from "../../identity/index.mjs";
4
3
  /**
5
4
  * Plugin for decorating every element in ApiDOM tree with UUID.
6
5
  */
7
-
8
6
  const plugin = ({
9
7
  length = 6
10
8
  } = {}) => () => {
11
- let uuid;
9
+ let identityManager;
12
10
  return {
13
11
  pre() {
14
- uuid = new ShortUniqueId({
12
+ identityManager = IdentityManager({
15
13
  length
16
14
  });
17
15
  },
18
16
  visitor: {
19
17
  enter(element) {
20
- // eslint-disable-next-line no-param-reassign
21
- element.id = new StringElement(uuid.randomUUID());
18
+ element.id = new StringElement(identityManager.generateId()); // eslint-disable-line no-param-reassign
22
19
  }
23
20
  },
21
+
24
22
  post() {
25
- uuid = null;
23
+ identityManager = null;
26
24
  }
27
25
  };
28
26
  };
@@ -1,32 +1,30 @@
1
- import ShortUniqueId from 'short-unique-id';
2
1
  import { StringElement } from 'minim';
3
-
2
+ import { IdentityManager } from "../../identity/index.mjs";
4
3
  /**
5
4
  * Plugin for decorating every semantic element in ApiDOM tree with UUID.
6
5
  */
7
-
8
6
  const plugin = ({
9
7
  length = 6
10
8
  } = {}) => ({
11
9
  predicates
12
10
  }) => {
13
- let uuid;
11
+ let identityManager;
14
12
  return {
15
13
  pre() {
16
- uuid = new ShortUniqueId({
14
+ identityManager = IdentityManager({
17
15
  length
18
16
  });
19
17
  },
20
18
  visitor: {
21
19
  enter(element) {
22
20
  if (!predicates.isPrimitiveElement(element)) {
23
- // eslint-disable-next-line no-param-reassign
24
- element.id = new StringElement(uuid.randomUUID());
21
+ element.id = new StringElement(identityManager.generateId()); // eslint-disable-line no-param-reassign
25
22
  }
26
23
  }
27
24
  },
25
+
28
26
  post() {
29
- uuid = null;
27
+ identityManager = null;
30
28
  }
31
29
  };
32
30
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swagger-api/apidom-core",
3
- "version": "0.82.2",
3
+ "version": "0.83.0",
4
4
  "description": "Tools for manipulating ApiDOM structures.",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -42,8 +42,8 @@
42
42
  "license": "Apache-2.0",
43
43
  "dependencies": {
44
44
  "@babel/runtime-corejs3": "^7.20.7",
45
- "@swagger-api/apidom-ast": "^0.82.2",
46
- "@swagger-api/apidom-error": "^0.82.1",
45
+ "@swagger-api/apidom-ast": "^0.83.0",
46
+ "@swagger-api/apidom-error": "^0.83.0",
47
47
  "@types/ramda": "~0.29.6",
48
48
  "minim": "~0.23.8",
49
49
  "ramda": "~0.29.0",
@@ -62,5 +62,5 @@
62
62
  "README.md",
63
63
  "CHANGELOG.md"
64
64
  ],
65
- "gitHead": "4d2a5697cba7abcab32abfdbea6ba946b4fd262f"
65
+ "gitHead": "4f5563541b7973162e0d5096966d56dbad4c706c"
66
66
  }
package/types/dist.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  /// <reference path="./minim.d.ts" />
2
- import { Element, ArrayElement, Meta, Attributes, Namespace as Namespace$1, NamespacePlugin, StringElement, ArraySlice, NumberElement, NullElement, BooleanElement, ObjectElement, MemberElement, LinkElement, RefElement, KeyValuePair, ObjectSlice } from 'minim';
2
+ import { Element, StringElement, Meta, Attributes, ArrayElement, ArraySlice, NumberElement, NullElement, BooleanElement, ObjectElement, MemberElement, LinkElement, RefElement, Namespace as Namespace$1, NamespacePlugin, KeyValuePair, ObjectSlice } from 'minim';
3
3
  export { ArrayElement, ArraySlice, Attributes, BooleanElement, Element, KeyValuePair, LinkElement, MemberElement, Meta, NamespacePluginOptions, NullElement, NumberElement, ObjectElement, ObjectSlice, RefElement, StringElement, refract } from 'minim';
4
4
  import stampit from 'stampit';
5
5
  import { ApiDOMStructuredError, ApiDOMErrorOptions } from '@swagger-api/apidom-error';
6
+ import ShortUniqueId from 'short-unique-id';
6
7
  export { BREAK, mergeAllVisitors } from '@swagger-api/apidom-ast';
7
8
 
8
9
  declare const dispatchPlugins: <T extends Element>(element: T, plugins: any[], options?: {}) => T;
@@ -20,53 +21,6 @@ declare const plugin$1: ({ length }?: {
20
21
  post(): void;
21
22
  };
22
23
 
23
- /**
24
- * Plugin for decorating every semantic element in ApiDOM tree with UUID.
25
- */
26
- type Predicates = {
27
- isPrimitiveElement: (element: Element) => boolean;
28
- };
29
- declare const plugin: ({ length }?: {
30
- length?: number | undefined;
31
- }) => ({ predicates }: {
32
- predicates: Predicates;
33
- }) => {
34
- pre(): void;
35
- visitor: {
36
- enter<T extends Element>(element: T): void;
37
- };
38
- post(): void;
39
- };
40
-
41
- declare class MediaTypes<T> extends Array<T> {
42
- unknownMediaType: string;
43
- filterByFormat(): void;
44
- findBy(): void;
45
- latest(): void;
46
- }
47
-
48
- interface Position {
49
- row: number;
50
- column: number;
51
- char: number;
52
- }
53
- interface PositionRange {
54
- start: Position;
55
- end: Position;
56
- }
57
- declare class SourceMap extends ArrayElement {
58
- constructor(content?: Array<any>, meta?: Meta, attributes?: Attributes);
59
- get positionStart(): Element | undefined;
60
- get positionEnd(): Element | undefined;
61
- set position(position: PositionRange | null);
62
- }
63
-
64
- declare class Namespace extends Namespace$1 {
65
- constructor();
66
- }
67
- declare const namespace: Namespace;
68
- declare const createNamespace: (namespacePlugin?: NamespacePlugin) => Namespace;
69
-
70
24
  declare class Annotation extends StringElement {
71
25
  constructor(content?: string, meta?: Meta, attributes?: Attributes);
72
26
  get code(): any;
@@ -89,6 +43,22 @@ declare class ParseResult extends ArrayElement {
89
43
  replaceResult(replacement: Element): boolean;
90
44
  }
91
45
 
46
+ interface Position {
47
+ row: number;
48
+ column: number;
49
+ char: number;
50
+ }
51
+ interface PositionRange {
52
+ start: Position;
53
+ end: Position;
54
+ }
55
+ declare class SourceMap extends ArrayElement {
56
+ constructor(content?: Array<any>, meta?: Meta, attributes?: Attributes);
57
+ get positionStart(): Element | undefined;
58
+ get positionEnd(): Element | undefined;
59
+ set position(position: PositionRange | null);
60
+ }
61
+
92
62
  interface PredicateHelpers {
93
63
  hasMethod: typeof hasMethod;
94
64
  hasBasicElementProps: typeof hasBasicElementProps;
@@ -137,6 +107,37 @@ declare const hasElementSourceMap: <T extends Element>(element: T) => boolean;
137
107
  declare const includesSymbols: <T extends Element>(symbols: string[], element: T) => boolean;
138
108
  declare const includesClasses: <T extends Element>(classes: string[], element: T) => boolean;
139
109
 
110
+ /**
111
+ * Plugin for decorating every semantic element in ApiDOM tree with UUID.
112
+ */
113
+ type Predicates = {
114
+ isPrimitiveElement: typeof isPrimitiveElement;
115
+ };
116
+ declare const plugin: ({ length }?: {
117
+ length?: number | undefined;
118
+ }) => ({ predicates }: {
119
+ predicates: Predicates;
120
+ }) => {
121
+ pre(): void;
122
+ visitor: {
123
+ enter<T extends Element>(element: T): void;
124
+ };
125
+ post(): void;
126
+ };
127
+
128
+ declare class MediaTypes<T> extends Array<T> {
129
+ unknownMediaType: string;
130
+ filterByFormat(): void;
131
+ findBy(): void;
132
+ latest(): void;
133
+ }
134
+
135
+ declare class Namespace extends Namespace$1 {
136
+ constructor();
137
+ }
138
+ declare const namespace: Namespace;
139
+ declare const createNamespace: (namespacePlugin?: NamespacePlugin) => Namespace;
140
+
140
141
  declare const filter: <T extends Element>(predicate: (element: any) => boolean, element: T) => ArraySlice;
141
142
 
142
143
  declare const find: <T extends Element>(predicate: (element: any) => boolean, element: T) => T | undefined;
@@ -243,6 +244,25 @@ declare class DeepCloneError extends CloneError {
243
244
  declare class ShallowCloneError extends CloneError {
244
245
  }
245
246
 
247
+ interface IdentityManager<T extends Element = Element> {
248
+ length: number;
249
+ uuid: ShortUniqueId;
250
+ identityMap: WeakMap<T, StringElement>;
251
+ identify(this: IdentityManager<T>, element: T): StringElement;
252
+ forget(this: IdentityManager<T>, element: T): boolean;
253
+ generateId(this: IdentityManager<T>): string;
254
+ }
255
+ declare const IdentityManager: stampit.Stamp<IdentityManager>;
256
+ declare const defaultIdentityManager: IdentityManager<Element>;
257
+
258
+ interface ElementIdentityErrorOptions extends ApiDOMErrorOptions {
259
+ readonly value: unknown;
260
+ }
261
+ declare class ElementIdentityError extends ApiDOMStructuredError {
262
+ readonly value: unknown;
263
+ constructor(message?: string, structuredOptions?: ElementIdentityErrorOptions);
264
+ }
265
+
246
266
  /**
247
267
  * Transforms data to an Element from a particular namespace.
248
268
  */
@@ -294,4 +314,4 @@ declare namespace deepmerge {
294
314
  var all: (list: ObjectOrArrayElement[], options?: DeepMergeOptions | undefined) => any;
295
315
  }
296
316
 
297
- export { Annotation as AnnotationElement, CloneError, Comment as CommentElement, DeepCloneError, type ElementPredicate, MediaTypes, Namespace, ParseResult as ParseResultElement, type Position, type PositionRange, ShallowCloneError, SourceMap as SourceMapElement, Transcluder, cloneDeep, cloneNode, cloneShallow, createNamespace, createPredicate, deepmerge, dehydrate, dereference, dispatchPlugins as dispatchRefractorPlugins, filter, find, findAtOffset, from, getNodeType, hasElementSourceMap, includesClasses, includesSymbols, isAnnotationElement, isArrayElement, isBooleanElement, isElement, isLinkElement, isMemberElement, isNullElement, isNumberElement, isObjectElement, isParseResultElement, isPrimitiveElement, isRefElement, isSourceMapElement, isStringElement, keyMapDefault as keyMap, namespace, parents, plugin$1 as refractorPluginElementIdentity, plugin as refractorPluginSemanticElementIdentity, reject, sexprs, some, serializer$1 as toJSON, toString, serializer$2 as toValue, serializer as toYAML, transclude, traverse, visit };
317
+ export { Annotation as AnnotationElement, CloneError, Comment as CommentElement, DeepCloneError, ElementIdentityError, type ElementPredicate, IdentityManager, MediaTypes, Namespace, ParseResult as ParseResultElement, type Position, type PositionRange, ShallowCloneError, SourceMap as SourceMapElement, Transcluder, cloneDeep, cloneNode, cloneShallow, createNamespace, createPredicate, deepmerge, defaultIdentityManager, dehydrate, dereference, dispatchPlugins as dispatchRefractorPlugins, filter, find, findAtOffset, from, getNodeType, hasElementSourceMap, includesClasses, includesSymbols, isAnnotationElement, isArrayElement, isBooleanElement, isElement, isLinkElement, isMemberElement, isNullElement, isNumberElement, isObjectElement, isParseResultElement, isPrimitiveElement, isRefElement, isSourceMapElement, isStringElement, keyMapDefault as keyMap, namespace, parents, plugin$1 as refractorPluginElementIdentity, plugin as refractorPluginSemanticElementIdentity, reject, sexprs, some, serializer$1 as toJSON, toString, serializer$2 as toValue, serializer as toYAML, transclude, traverse, visit };