@swagger-api/apidom-core 0.78.0 → 0.80.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.
@@ -1,3 +1,10 @@
1
1
  import { ApiDOMStructuredError } from '@swagger-api/apidom-error';
2
- class CloneError extends ApiDOMStructuredError {}
2
+ class CloneError extends ApiDOMStructuredError {
3
+ constructor(message, structuredOptions) {
4
+ super(message, structuredOptions);
5
+ if (typeof structuredOptions !== 'undefined') {
6
+ this.value = structuredOptions.source;
7
+ }
8
+ }
9
+ }
3
10
  export default CloneError;
@@ -2,23 +2,63 @@ import { ArraySlice, ObjectSlice, KeyValuePair } from 'minim';
2
2
  import { isElement } from "../predicates/index.mjs";
3
3
  import DeepCloneError from "./errors/DeepCloneError.mjs";
4
4
  import ShallowCloneError from "./errors/ShallowCloneError.mjs";
5
- const invokeClone = value => {
6
- if (typeof (value === null || value === void 0 ? void 0 : value.clone) === 'function') {
7
- return value.clone();
5
+ export const cloneDeep = (value, options = {}) => {
6
+ const {
7
+ visited = new WeakMap()
8
+ } = options;
9
+ const passThroughOptions = {
10
+ ...options,
11
+ visited
12
+ };
13
+
14
+ // detect cycle and return memoized value
15
+ if (visited.has(value)) {
16
+ return visited.get(value);
17
+ }
18
+ if (value instanceof KeyValuePair) {
19
+ const {
20
+ key,
21
+ value: val
22
+ } = value;
23
+ const keyCopy = isElement(key) ? cloneDeep(key, passThroughOptions) : key;
24
+ const valueCopy = isElement(val) ? cloneDeep(val, passThroughOptions) : val;
25
+ const copy = new KeyValuePair(keyCopy, valueCopy);
26
+ visited.set(value, copy);
27
+ return copy;
8
28
  }
9
- return value;
10
- };
11
- export const cloneDeep = value => {
12
29
  if (value instanceof ObjectSlice) {
13
- const items = [...value].map(invokeClone);
14
- return new ObjectSlice(items);
30
+ const mapper = element => cloneDeep(element, passThroughOptions);
31
+ const items = [...value].map(mapper);
32
+ const copy = new ObjectSlice(items);
33
+ visited.set(value, copy);
34
+ return copy;
15
35
  }
16
36
  if (value instanceof ArraySlice) {
17
- const items = [...value].map(invokeClone);
18
- return new ArraySlice(items);
37
+ const mapper = element => cloneDeep(element, passThroughOptions);
38
+ const items = [...value].map(mapper);
39
+ const copy = new ArraySlice(items);
40
+ visited.set(value, copy);
41
+ return copy;
19
42
  }
20
- if (typeof (value === null || value === void 0 ? void 0 : value.clone) === 'function') {
21
- return value.clone();
43
+ if (isElement(value)) {
44
+ const copy = cloneShallow(value); // eslint-disable-line @typescript-eslint/no-use-before-define
45
+
46
+ visited.set(value, copy);
47
+ if (value.content) {
48
+ if (isElement(value.content)) {
49
+ copy.content = cloneDeep(value.content, passThroughOptions);
50
+ } else if (value.content instanceof KeyValuePair) {
51
+ copy.content = cloneDeep(value.content, passThroughOptions);
52
+ } else if (Array.isArray(value.content)) {
53
+ const mapper = element => cloneDeep(element, passThroughOptions);
54
+ copy.content = value.content.map(mapper);
55
+ } else {
56
+ copy.content = value.content;
57
+ }
58
+ } else {
59
+ copy.content = value.content;
60
+ }
61
+ return copy;
22
62
  }
23
63
  throw new DeepCloneError("Value provided to cloneDeep function couldn't be cloned", {
24
64
  value
package/es/deepmerge.mjs CHANGED
@@ -73,7 +73,7 @@ export default function deepmerge(targetElement, sourceElement, options) {
73
73
  }
74
74
  deepmerge.all = (list, options) => {
75
75
  if (!Array.isArray(list)) {
76
- throw new TypeError('First argument should be an array.');
76
+ throw new TypeError('First argument of deepmerge should be an array.');
77
77
  }
78
78
  if (list.length === 0) {
79
79
  return new ObjectElement();
@@ -1,16 +1,21 @@
1
- const hasMethod = (name, obj) => typeof (obj === null || obj === void 0 ? void 0 : obj[name]) === 'function';
2
- const hasBasicElementProps = element => element != null && Object.prototype.hasOwnProperty.call(element, '_storedElement') && Object.prototype.hasOwnProperty.call(element, '_content');
3
- const primitiveEq = (val, obj) => {
4
- var _obj$primitive;
5
- return (obj === null || obj === void 0 || (_obj$primitive = obj.primitive) === null || _obj$primitive === void 0 ? void 0 : _obj$primitive.call(obj)) === val;
1
+ import { ArrayElement } from 'minim';
2
+ const hasMethod = (name, element) => {
3
+ return typeof element === 'object' && element !== null && name in element && typeof element[name] === 'function';
6
4
  };
7
- const hasClass = (cls, obj) => {
8
- var _obj$classes, _obj$classes$includes;
9
- return (obj === null || obj === void 0 || (_obj$classes = obj.classes) === null || _obj$classes === void 0 || (_obj$classes$includes = _obj$classes.includes) === null || _obj$classes$includes === void 0 ? void 0 : _obj$classes$includes.call(_obj$classes, cls)) || false;
5
+ const hasBasicElementProps = element => typeof element === 'object' && element != null && '_storedElement' in element && typeof element._storedElement === 'string' &&
6
+ // eslint-disable-line no-underscore-dangle
7
+ '_content' in element;
8
+ const primitiveEq = (val, element) => {
9
+ if (typeof element === 'object' && element !== null && 'primitive' in element) {
10
+ return typeof element.primitive === 'function' && element.primitive() === val;
11
+ }
12
+ return false;
10
13
  };
11
- export const isElementType = (name, element) => (element === null || element === void 0 ? void 0 : element.element) === name;
14
+ const hasClass = (cls, element) => {
15
+ return typeof element === 'object' && element !== null && 'classes' in element && (Array.isArray(element.classes) || element.classes instanceof ArrayElement) && element.classes.includes(cls);
16
+ };
17
+ export const isElementType = (name, element) => typeof element === 'object' && element !== null && 'element' in element && element.element === name;
12
18
  const createPredicate = predicateCreator => {
13
- // @ts-ignore
14
19
  return predicateCreator({
15
20
  hasMethod,
16
21
  hasBasicElementProps,
@@ -103,8 +103,7 @@ export const isPrimitiveElement = element => {
103
103
  return isElementTypeHelper('object', element) || isElementTypeHelper('array', element) || isElementTypeHelper('boolean', element) || isElementTypeHelper('number', element) || isElementTypeHelper('string', element) || isElementTypeHelper('null', element) || isElementTypeHelper('member', element);
104
104
  };
105
105
  export const hasElementSourceMap = element => {
106
- var _element$meta, _element$meta$get;
107
- return isSourceMapElement(element === null || element === void 0 || (_element$meta = element.meta) === null || _element$meta === void 0 || (_element$meta$get = _element$meta.get) === null || _element$meta$get === void 0 ? void 0 : _element$meta$get.call(_element$meta, 'sourceMap'));
106
+ return isSourceMapElement(element.meta.get('sourceMap'));
108
107
  };
109
108
  export const includesSymbols = (symbols, element) => {
110
109
  if (symbols.length === 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swagger-api/apidom-core",
3
- "version": "0.78.0",
3
+ "version": "0.80.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.78.0",
46
- "@swagger-api/apidom-error": "^0.78.0",
45
+ "@swagger-api/apidom-ast": "^0.80.0",
46
+ "@swagger-api/apidom-error": "^0.80.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": "d6587217f8a7bec5bbc49ca4dabff8d3d66e0913"
65
+ "gitHead": "c959dedb5900d9d6ec2ba64aebc30a999d61b696"
66
66
  }
package/types/dist.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  /// <reference path="./minim.d.ts" />
2
- import { Element, ArrayElement, Meta, Attributes, Namespace as Namespace$1, NamespacePlugin, StringElement, ArraySlice, KeyValuePair, ObjectSlice, ObjectElement } from 'minim';
2
+ import { Element, ArrayElement, Meta, Attributes, Namespace as Namespace$1, NamespacePlugin, StringElement, ArraySlice, NumberElement, NullElement, BooleanElement, ObjectElement, MemberElement, LinkElement, RefElement, 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
- import { ApiDOMStructuredError } from '@swagger-api/apidom-error';
5
+ import { ApiDOMStructuredError, ApiDOMErrorOptions } from '@swagger-api/apidom-error';
6
6
  export { BREAK, mergeAllVisitors } from '@swagger-api/apidom-ast';
7
7
 
8
8
  declare const dispatchPlugins: <T extends Element>(element: T, plugins: any[], options?: {}) => T;
@@ -89,29 +89,6 @@ declare class ParseResult extends ArrayElement {
89
89
  replaceResult(replacement: Element): boolean;
90
90
  }
91
91
 
92
- declare const isElement: (element: any) => boolean;
93
- declare const isStringElement: (element: any) => boolean;
94
- declare const isNumberElement: (element: any) => boolean;
95
- declare const isNullElement: (element: any) => boolean;
96
- declare const isBooleanElement: (element: any) => boolean;
97
- declare const isObjectElement: (element: any) => boolean;
98
- declare const isArrayElement: (element: any) => boolean;
99
- declare const isMemberElement: (element: any) => boolean;
100
- declare const isLinkElement: (element: any) => boolean;
101
- declare const isRefElement: (element: any) => boolean;
102
- declare const isAnnotationElement: (element: any) => boolean;
103
- declare const isParseResultElement: (element: any) => boolean;
104
- declare const isSourceMapElement: (element: any) => boolean;
105
- declare const isPrimitiveElement: (element: any) => boolean;
106
- declare const hasElementSourceMap: (element: any) => boolean;
107
- declare const includesSymbols: <T extends Element>(symbols: string[], element: T) => boolean;
108
- declare const includesClasses: <T extends Element>(classes: string[], element: T) => boolean;
109
-
110
- declare const hasMethod: (name: string, obj: Record<string, unknown>) => boolean;
111
- declare const hasBasicElementProps: (element: any) => boolean;
112
- declare const primitiveEq: (val: unknown, obj: any) => boolean;
113
- declare const hasClass: (cls: string, obj: any) => boolean;
114
- declare const isElementType: (name: string, element: any) => boolean;
115
92
  interface PredicateHelpers {
116
93
  hasMethod: typeof hasMethod;
117
94
  hasBasicElementProps: typeof hasBasicElementProps;
@@ -119,8 +96,46 @@ interface PredicateHelpers {
119
96
  isElementType: typeof isElementType;
120
97
  hasClass: typeof hasClass;
121
98
  }
122
- type PredicateCreator = (helpers: PredicateHelpers) => (element: any) => boolean;
123
- declare const createPredicate: (predicateCreator: PredicateCreator) => (element: any) => boolean;
99
+ interface ElementBasicsTrait {
100
+ _storedElement: string;
101
+ _content: unknown;
102
+ }
103
+ interface ElementPrimitiveBehavior {
104
+ primitive: () => unknown;
105
+ }
106
+ interface ElementTypeTrait<T = string> {
107
+ element: T;
108
+ }
109
+ interface ElementClassesTrait {
110
+ classes: ArrayElement | Array<string>;
111
+ }
112
+ type PredicateCreator<T extends Element> = (helpers: PredicateHelpers) => ElementPredicate<T>;
113
+ type ElementPredicate<T extends Element> = (element: unknown) => element is T;
114
+ declare const hasMethod: <T extends string>(name: T, element: unknown) => element is { [key in T]: (...args: unknown[]) => unknown; };
115
+ declare const hasBasicElementProps: (element: unknown) => element is ElementBasicsTrait;
116
+ declare const primitiveEq: (val: unknown, element: unknown) => element is ElementPrimitiveBehavior;
117
+ declare const hasClass: (cls: string, element: unknown) => element is ElementClassesTrait;
118
+ declare const isElementType: (name: string, element: unknown) => element is ElementTypeTrait<string>;
119
+ declare const createPredicate: <T extends Element>(predicateCreator: PredicateCreator<T>) => ElementPredicate<T>;
120
+
121
+ declare const isElement: ElementPredicate<Element>;
122
+ declare const isStringElement: ElementPredicate<StringElement>;
123
+ declare const isNumberElement: ElementPredicate<NumberElement>;
124
+ declare const isNullElement: ElementPredicate<NullElement>;
125
+ declare const isBooleanElement: ElementPredicate<BooleanElement>;
126
+ declare const isObjectElement: ElementPredicate<ObjectElement>;
127
+ declare const isArrayElement: ElementPredicate<ArrayElement>;
128
+ declare const isMemberElement: ElementPredicate<MemberElement>;
129
+ declare const isLinkElement: ElementPredicate<LinkElement>;
130
+ declare const isRefElement: ElementPredicate<RefElement>;
131
+ declare const isAnnotationElement: ElementPredicate<Annotation>;
132
+ declare const isParseResultElement: ElementPredicate<ParseResult>;
133
+ declare const isSourceMapElement: ElementPredicate<SourceMap>;
134
+ type PrimitiveElement = ObjectElement | ArrayElement | BooleanElement | NumberElement | StringElement | NullElement | MemberElement;
135
+ declare const isPrimitiveElement: ElementPredicate<PrimitiveElement>;
136
+ declare const hasElementSourceMap: <T extends Element>(element: T) => boolean;
137
+ declare const includesSymbols: <T extends Element>(symbols: string[], element: T) => boolean;
138
+ declare const includesClasses: <T extends Element>(classes: string[], element: T) => boolean;
124
139
 
125
140
  declare const filter: <T extends Element>(predicate: (element: any) => boolean, element: T) => ArraySlice;
126
141
 
@@ -202,8 +217,11 @@ declare const transclude: (search: Element, replace: Element, element: Element)
202
217
  declare const dereference: (object: Record<string, any>, root?: Record<string, any>) => Record<string, any>;
203
218
 
204
219
  type FinalCloneTypes = KeyValuePair | ArraySlice | ObjectSlice;
220
+ type DeepCloneOptions<T extends Element | FinalCloneTypes> = {
221
+ visited?: WeakMap<T, T>;
222
+ };
205
223
  declare const cloneDeep: {
206
- <T extends Element | FinalCloneTypes>(value: T): T;
224
+ <T extends Element | FinalCloneTypes>(value: T, options?: DeepCloneOptions<T>): T;
207
225
  safe<T_1>(value: T_1): T_1;
208
226
  };
209
227
  declare const cloneShallow: {
@@ -211,7 +229,12 @@ declare const cloneShallow: {
211
229
  safe<T_1>(value: T_1): T_1;
212
230
  };
213
231
 
232
+ interface CloneErrorOptions extends ApiDOMErrorOptions {
233
+ readonly value: unknown;
234
+ }
214
235
  declare class CloneError extends ApiDOMStructuredError {
236
+ readonly value: unknown;
237
+ constructor(message?: string, structuredOptions?: CloneErrorOptions);
215
238
  }
216
239
 
217
240
  declare class DeepCloneError extends CloneError {
@@ -271,4 +294,4 @@ declare namespace deepmerge {
271
294
  var all: (list: ObjectOrArrayElement[], options?: DeepMergeOptions | undefined) => any;
272
295
  }
273
296
 
274
- export { Annotation as AnnotationElement, CloneError, Comment as CommentElement, DeepCloneError, 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 };
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 };