@speclynx/apidom-datamodel 4.0.3 → 4.0.4
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/CHANGELOG.md +6 -0
- package/package.json +5 -4
- package/src/KeyValuePair.ts +0 -31
- package/src/Metadata.ts +0 -100
- package/src/Namespace.ts +0 -260
- package/src/ObjectSlice.ts +0 -228
- package/src/clone/errors/CloneError.ts +0 -26
- package/src/clone/errors/DeepCloneError.ts +0 -8
- package/src/clone/errors/ShallowCloneError.ts +0 -8
- package/src/clone/index.ts +0 -195
- package/src/elements/Annotation.ts +0 -35
- package/src/elements/Comment.ts +0 -16
- package/src/elements/LinkElement.ts +0 -49
- package/src/elements/ParseResult.ts +0 -94
- package/src/elements/RefElement.ts +0 -33
- package/src/elements/SourceMap.ts +0 -170
- package/src/elements/Style.ts +0 -56
- package/src/index.ts +0 -79
- package/src/predicates/elements.ts +0 -42
- package/src/predicates/index.ts +0 -89
- package/src/predicates/primitives.ts +0 -79
- package/src/primitives/ArrayElement.ts +0 -161
- package/src/primitives/BooleanElement.ts +0 -18
- package/src/primitives/CollectionElement.ts +0 -191
- package/src/primitives/Element.ts +0 -556
- package/src/primitives/MemberElement.ts +0 -61
- package/src/primitives/NullElement.ts +0 -26
- package/src/primitives/NumberElement.ts +0 -18
- package/src/primitives/ObjectElement.ts +0 -263
- package/src/primitives/StringElement.ts +0 -25
- package/src/registration.ts +0 -111
- package/src/serialisers/JSONSerialiser.ts +0 -295
- package/src/types.ts +0 -72
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [4.0.4](https://github.com/speclynx/apidom/compare/v4.0.3...v4.0.4) (2026-03-12)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **release:** override minimatch 10.2.3 to fix glob pattern regression in lerna publish ([#157](https://github.com/speclynx/apidom/issues/157)) ([c2d65a0](https://github.com/speclynx/apidom/commit/c2d65a06a2187e8563a9dc9db74ba27255450e0b)), closes [lerna/lerna#4305](https://github.com/lerna/lerna/issues/4305) [isaacs/minimatch#284](https://github.com/isaacs/minimatch/issues/284)
|
|
11
|
+
|
|
6
12
|
## [4.0.3](https://github.com/speclynx/apidom/compare/v4.0.2...v4.0.3) (2026-03-11)
|
|
7
13
|
|
|
8
14
|
### Bug Fixes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@speclynx/apidom-datamodel",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.4",
|
|
4
4
|
"description": "Data model primitives for ApiDOM.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"apidom",
|
|
@@ -55,11 +55,12 @@
|
|
|
55
55
|
"license": "Apache-2.0",
|
|
56
56
|
"dependencies": {
|
|
57
57
|
"@babel/runtime-corejs3": "^7.28.4",
|
|
58
|
-
"@speclynx/apidom-error": "4.0.
|
|
58
|
+
"@speclynx/apidom-error": "4.0.4",
|
|
59
59
|
"ramda": "~0.32.0"
|
|
60
60
|
},
|
|
61
61
|
"files": [
|
|
62
|
-
"src
|
|
62
|
+
"src/**/*.mjs",
|
|
63
|
+
"src/**/*.cjs",
|
|
63
64
|
"dist/",
|
|
64
65
|
"types/apidom-datamodel.d.ts",
|
|
65
66
|
"LICENSES",
|
|
@@ -67,5 +68,5 @@
|
|
|
67
68
|
"README.md",
|
|
68
69
|
"CHANGELOG.md"
|
|
69
70
|
],
|
|
70
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "a06f6ef3d37ad5bf860cbbf4b442bfbe3a522cc2"
|
|
71
72
|
}
|
package/src/KeyValuePair.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type Element from './primitives/Element.ts';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Represents a key-value pair used in MemberElement content.
|
|
5
|
-
* This is used internally to store object member data.
|
|
6
|
-
*
|
|
7
|
-
* @typeParam K - Key element type
|
|
8
|
-
* @typeParam V - Value element type
|
|
9
|
-
* @public
|
|
10
|
-
*/
|
|
11
|
-
class KeyValuePair<K extends Element = Element, V extends Element = Element> {
|
|
12
|
-
public key: K | undefined;
|
|
13
|
-
public value: V | undefined;
|
|
14
|
-
|
|
15
|
-
constructor(key?: K, value?: V) {
|
|
16
|
-
this.key = key;
|
|
17
|
-
this.value = value;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Converts to a plain JavaScript object representation.
|
|
22
|
-
*/
|
|
23
|
-
toValue(): { key: unknown; value: unknown } {
|
|
24
|
-
return {
|
|
25
|
-
key: this.key?.toValue(),
|
|
26
|
-
value: this.value?.toValue(),
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export default KeyValuePair;
|
package/src/Metadata.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { clone } from 'ramda';
|
|
2
|
-
|
|
3
|
-
import type Element from './primitives/Element.ts';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Lightweight meta container for Element metadata.
|
|
7
|
-
*
|
|
8
|
-
* Data is stored as own properties on the instance; methods live on the prototype.
|
|
9
|
-
* `Object.keys()`, `Object.entries()`, etc. only see data properties.
|
|
10
|
-
*
|
|
11
|
-
* @public
|
|
12
|
-
*/
|
|
13
|
-
class Metadata {
|
|
14
|
-
// Set via prototype assignment in registration.ts to avoid circular dependency
|
|
15
|
-
declare Element: typeof Element;
|
|
16
|
-
declare cloneDeepElement: (element: Element) => Element;
|
|
17
|
-
|
|
18
|
-
get(name: string): unknown {
|
|
19
|
-
return (this as Record<string, unknown>)[name];
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
set(name: string, value: unknown): void {
|
|
23
|
-
(this as Record<string, unknown>)[name] = value;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
hasKey(name: string): boolean {
|
|
27
|
-
return Object.hasOwn(this, name);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
keys(): string[] {
|
|
31
|
-
return Object.keys(this);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
remove(name: string): void {
|
|
35
|
-
delete (this as Record<string, unknown>)[name];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
get isEmpty(): boolean {
|
|
39
|
-
return Object.keys(this).length === 0;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
get isFrozen(): boolean {
|
|
43
|
-
return Object.isFrozen(this);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
freeze(): void {
|
|
47
|
-
for (const value of Object.values(this)) {
|
|
48
|
-
if (value instanceof this.Element) {
|
|
49
|
-
value.freeze();
|
|
50
|
-
} else if (Array.isArray(value) || (value !== null && typeof value === 'object')) {
|
|
51
|
-
Object.freeze(value);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
Object.freeze(this);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Creates a shallow clone. Same references, new container.
|
|
59
|
-
*/
|
|
60
|
-
cloneShallow(): Metadata {
|
|
61
|
-
const clone = new Metadata();
|
|
62
|
-
Object.assign(clone, this);
|
|
63
|
-
return clone;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Merges another Metadata into a new instance.
|
|
68
|
-
* Arrays are concatenated, all other values are overwritten by source.
|
|
69
|
-
*/
|
|
70
|
-
merge(source: Metadata): Metadata {
|
|
71
|
-
const result = this.cloneShallow();
|
|
72
|
-
for (const [key, value] of Object.entries(source)) {
|
|
73
|
-
const existing = result.get(key);
|
|
74
|
-
if (Array.isArray(existing) && Array.isArray(value)) {
|
|
75
|
-
result.set(key, [...existing, ...value]);
|
|
76
|
-
} else {
|
|
77
|
-
result.set(key, value);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return result;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Creates a deep clone. Elements are deep cloned,
|
|
85
|
-
* all other values are deep cloned via ramda clone.
|
|
86
|
-
*/
|
|
87
|
-
cloneDeep(): Metadata {
|
|
88
|
-
const copy = new Metadata();
|
|
89
|
-
for (const [key, value] of Object.entries(this)) {
|
|
90
|
-
if (value instanceof this.Element) {
|
|
91
|
-
copy.set(key, this.cloneDeepElement(value));
|
|
92
|
-
} else {
|
|
93
|
-
copy.set(key, clone(value));
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return copy;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export default Metadata;
|
package/src/Namespace.ts
DELETED
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
import JSONSerialiser from './serialisers/JSONSerialiser.ts';
|
|
2
|
-
import * as elements from './registration.ts';
|
|
3
|
-
import type Element from './primitives/Element.ts';
|
|
4
|
-
import type ElementConstructor from './primitives/Element.ts';
|
|
5
|
-
import type KeyValuePairConstructor from './KeyValuePair.ts';
|
|
6
|
-
|
|
7
|
-
const isNull = (value: unknown): value is null => value === null;
|
|
8
|
-
const isString = (value: unknown): value is string => typeof value === 'string';
|
|
9
|
-
const isNumber = (value: unknown): value is number => typeof value === 'number';
|
|
10
|
-
const isBoolean = (value: unknown): value is boolean => typeof value === 'boolean';
|
|
11
|
-
const isObject = (value: unknown): value is Record<string, unknown> =>
|
|
12
|
-
value !== null && typeof value === 'object';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Constructor type for Element classes.
|
|
16
|
-
* @public
|
|
17
|
-
*/
|
|
18
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
-
type ElementClass = new (...args: any[]) => Element;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Function to test if a value should be converted to a specific element type.
|
|
23
|
-
* @public
|
|
24
|
-
*/
|
|
25
|
-
type DetectionTest = (value: unknown) => boolean;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Tuple of detection test and element class.
|
|
29
|
-
* @public
|
|
30
|
-
*/
|
|
31
|
-
type DetectionEntry = [DetectionTest, ElementClass];
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Options for Namespace constructor.
|
|
35
|
-
* @public
|
|
36
|
-
*/
|
|
37
|
-
interface NamespaceOptions {
|
|
38
|
-
noDefault?: boolean;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Plugin interface for extending Namespace.
|
|
43
|
-
* @public
|
|
44
|
-
*/
|
|
45
|
-
interface NamespacePlugin {
|
|
46
|
-
namespace?: (options: { base: Namespace }) => void;
|
|
47
|
-
load?: (options: { base: Namespace }) => void;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Map of registered element classes.
|
|
52
|
-
* @public
|
|
53
|
-
*/
|
|
54
|
-
interface ElementsMap {
|
|
55
|
-
Element: ElementClass;
|
|
56
|
-
[key: string]: ElementClass;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* A refract element implementation with an extensible namespace, able to
|
|
61
|
-
* load other namespaces into it.
|
|
62
|
-
*
|
|
63
|
-
* The namespace allows you to register your own classes to be instantiated
|
|
64
|
-
* when a particular refract element is encountered, and allows you to specify
|
|
65
|
-
* which elements get instantiated for existing JavaScript objects.
|
|
66
|
-
*
|
|
67
|
-
* @public
|
|
68
|
-
*/
|
|
69
|
-
class Namespace {
|
|
70
|
-
public elementMap: Record<string, ElementClass> = {};
|
|
71
|
-
public elementDetection: DetectionEntry[] = [];
|
|
72
|
-
public Element: typeof ElementConstructor;
|
|
73
|
-
public KeyValuePair: typeof KeyValuePairConstructor;
|
|
74
|
-
|
|
75
|
-
protected _elements?: ElementsMap;
|
|
76
|
-
protected _attributeElementKeys: string[] = [];
|
|
77
|
-
protected _attributeElementArrayKeys: string[] = [];
|
|
78
|
-
|
|
79
|
-
constructor(options?: NamespaceOptions) {
|
|
80
|
-
this.Element = elements.Element;
|
|
81
|
-
this.KeyValuePair = elements.KeyValuePair;
|
|
82
|
-
|
|
83
|
-
if (!options || !options.noDefault) {
|
|
84
|
-
this.useDefault();
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Use a namespace plugin or load a generic plugin.
|
|
90
|
-
*/
|
|
91
|
-
use(plugin: NamespacePlugin): this {
|
|
92
|
-
if (plugin.namespace) {
|
|
93
|
-
plugin.namespace({ base: this });
|
|
94
|
-
}
|
|
95
|
-
if (plugin.load) {
|
|
96
|
-
plugin.load({ base: this });
|
|
97
|
-
}
|
|
98
|
-
return this;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Use the default namespace. This preloads all the default elements
|
|
103
|
-
* into this registry instance.
|
|
104
|
-
*/
|
|
105
|
-
useDefault(): this {
|
|
106
|
-
// Set up classes for default elements
|
|
107
|
-
this.register('null', elements.NullElement)
|
|
108
|
-
.register('string', elements.StringElement)
|
|
109
|
-
.register('number', elements.NumberElement)
|
|
110
|
-
.register('boolean', elements.BooleanElement)
|
|
111
|
-
.register('array', elements.ArrayElement)
|
|
112
|
-
.register('object', elements.ObjectElement)
|
|
113
|
-
.register('member', elements.MemberElement)
|
|
114
|
-
.register('ref', elements.RefElement)
|
|
115
|
-
.register('link', elements.LinkElement)
|
|
116
|
-
.register('sourceMap', elements.SourceMapElement);
|
|
117
|
-
|
|
118
|
-
// Add instance detection functions to convert existing objects into
|
|
119
|
-
// the corresponding refract elements.
|
|
120
|
-
this.detect(isNull, elements.NullElement, false)
|
|
121
|
-
.detect(isString, elements.StringElement, false)
|
|
122
|
-
.detect(isNumber, elements.NumberElement, false)
|
|
123
|
-
.detect(isBoolean, elements.BooleanElement, false)
|
|
124
|
-
.detect(Array.isArray, elements.ArrayElement, false)
|
|
125
|
-
.detect(isObject, elements.ObjectElement, false);
|
|
126
|
-
|
|
127
|
-
return this;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Register a new element class for an element.
|
|
132
|
-
*/
|
|
133
|
-
register(name: string, ElementClass: ElementClass): this {
|
|
134
|
-
this._elements = undefined;
|
|
135
|
-
this.elementMap[name] = ElementClass;
|
|
136
|
-
return this;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Unregister a previously registered class for an element.
|
|
141
|
-
*/
|
|
142
|
-
unregister(name: string): this {
|
|
143
|
-
this._elements = undefined;
|
|
144
|
-
delete this.elementMap[name];
|
|
145
|
-
return this;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Add a new detection function to determine which element
|
|
150
|
-
* class to use when converting existing JS instances into
|
|
151
|
-
* refract elements.
|
|
152
|
-
*/
|
|
153
|
-
detect(test: DetectionTest, ElementClass: ElementClass, givenPrepend?: boolean): this {
|
|
154
|
-
const prepend = givenPrepend === undefined ? true : givenPrepend;
|
|
155
|
-
|
|
156
|
-
if (prepend) {
|
|
157
|
-
this.elementDetection.unshift([test, ElementClass]);
|
|
158
|
-
} else {
|
|
159
|
-
this.elementDetection.push([test, ElementClass]);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return this;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Convert an existing JavaScript object into refract element instances.
|
|
167
|
-
* If the item passed in is already refracted, then it is returned unmodified.
|
|
168
|
-
*/
|
|
169
|
-
toElement(value: unknown): Element | undefined {
|
|
170
|
-
if (value instanceof this.Element) {
|
|
171
|
-
return value as Element;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
let element: Element | undefined;
|
|
175
|
-
|
|
176
|
-
for (const [test, ElementClass] of this.elementDetection) {
|
|
177
|
-
if (test(value)) {
|
|
178
|
-
element = new ElementClass(value);
|
|
179
|
-
break;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
return element;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Get an element class given an element name.
|
|
188
|
-
*/
|
|
189
|
-
getElementClass(element: string): ElementClass {
|
|
190
|
-
const ElementClass = this.elementMap[element];
|
|
191
|
-
|
|
192
|
-
if (ElementClass === undefined) {
|
|
193
|
-
// Fall back to the base element. We may not know what
|
|
194
|
-
// to do with the `content`, but downstream software
|
|
195
|
-
// may know.
|
|
196
|
-
return this.Element as unknown as ElementClass;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
return ElementClass;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Convert a refract document into refract element instances.
|
|
204
|
-
*/
|
|
205
|
-
fromRefract(doc: Record<string, unknown>): Element {
|
|
206
|
-
return this.serialiser.deserialise(
|
|
207
|
-
doc as unknown as Parameters<JSONSerialiser['deserialise']>[0],
|
|
208
|
-
);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Convert an element to a Refracted JSON object.
|
|
213
|
-
*/
|
|
214
|
-
toRefract(element: Element): ReturnType<JSONSerialiser['serialise']> {
|
|
215
|
-
return this.serialiser.serialise(element);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Get an object that contains all registered element classes, where
|
|
220
|
-
* the key is the PascalCased element name and the value is the class.
|
|
221
|
-
*/
|
|
222
|
-
get elements(): ElementsMap {
|
|
223
|
-
if (this._elements === undefined) {
|
|
224
|
-
this._elements = {
|
|
225
|
-
Element: this.Element,
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
Object.keys(this.elementMap).forEach((name) => {
|
|
229
|
-
// Currently, all registered element types use a camelCaseName.
|
|
230
|
-
// Converting to PascalCase is as simple as upper-casing the first letter.
|
|
231
|
-
const pascal = name[0].toUpperCase() + name.substring(1);
|
|
232
|
-
this._elements![pascal] = this.elementMap[name];
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
return this._elements;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Convenience method for getting a JSON Serialiser configured with the
|
|
241
|
-
* current namespace.
|
|
242
|
-
*/
|
|
243
|
-
get serialiser(): JSONSerialiser {
|
|
244
|
-
return new JSONSerialiser(this);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// Set up the circular reference for JSONSerialiser
|
|
249
|
-
JSONSerialiser.prototype.Namespace = Namespace;
|
|
250
|
-
|
|
251
|
-
export default Namespace;
|
|
252
|
-
|
|
253
|
-
export type {
|
|
254
|
-
ElementClass,
|
|
255
|
-
DetectionTest,
|
|
256
|
-
DetectionEntry,
|
|
257
|
-
NamespaceOptions,
|
|
258
|
-
NamespacePlugin,
|
|
259
|
-
ElementsMap,
|
|
260
|
-
};
|
package/src/ObjectSlice.ts
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
import type Element from './primitives/Element.ts';
|
|
2
|
-
import type MemberElement from './primitives/MemberElement.ts';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Callback type for ObjectSlice iteration methods.
|
|
6
|
-
* Receives (value, key, member) - the standard pattern for object-like iteration.
|
|
7
|
-
* @public
|
|
8
|
-
*/
|
|
9
|
-
export type ObjectSliceCallback<U> = (value: Element, key: Element, member: MemberElement) => U;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Callback type for ObjectSlice forEach that also receives the index.
|
|
13
|
-
* @public
|
|
14
|
-
*/
|
|
15
|
-
export type ObjectSliceForEachCallback = (
|
|
16
|
-
value: Element,
|
|
17
|
-
key: Element,
|
|
18
|
-
member: MemberElement,
|
|
19
|
-
index: number,
|
|
20
|
-
) => void;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* ObjectSlice is a collection wrapper for MemberElement arrays.
|
|
24
|
-
* It provides functional methods with (value, key, member) callback signatures,
|
|
25
|
-
* which is the standard pattern for iterating over object-like structures.
|
|
26
|
-
*
|
|
27
|
-
* Unlike ArraySlice, ObjectSlice uses composition rather than inheritance
|
|
28
|
-
* because its callback signatures are fundamentally different.
|
|
29
|
-
*
|
|
30
|
-
* @public
|
|
31
|
-
*/
|
|
32
|
-
class ObjectSlice {
|
|
33
|
-
public readonly elements: MemberElement[];
|
|
34
|
-
|
|
35
|
-
constructor(elements?: MemberElement[]) {
|
|
36
|
-
this.elements = elements ?? [];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Converts all member elements to their JavaScript values.
|
|
41
|
-
* Returns an array of \{ key, value \} objects.
|
|
42
|
-
*/
|
|
43
|
-
toValue(): Array<{ key: unknown; value: unknown }> {
|
|
44
|
-
return this.elements.map((member) => ({
|
|
45
|
-
key: member.key?.toValue(),
|
|
46
|
-
value: member.value?.toValue(),
|
|
47
|
-
}));
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Maps over the member elements, calling callback with (value, key, member).
|
|
52
|
-
* @param callback - Function to execute for each member
|
|
53
|
-
* @param thisArg - Value to use as this when executing callback
|
|
54
|
-
*/
|
|
55
|
-
map<U>(callback: ObjectSliceCallback<U>, thisArg?: unknown): U[] {
|
|
56
|
-
return this.elements.map((member) => {
|
|
57
|
-
const value = member.value;
|
|
58
|
-
const key = member.key;
|
|
59
|
-
|
|
60
|
-
if (value === undefined || key === undefined) {
|
|
61
|
-
throw new Error('MemberElement must have both key and value');
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return thisArg !== undefined
|
|
65
|
-
? callback.call(thisArg, value, key, member)
|
|
66
|
-
: callback(value, key, member);
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Filters member elements using the provided callback.
|
|
72
|
-
* @param callback - Function that receives (value, key, member) and returns boolean
|
|
73
|
-
* @param thisArg - Value to use as this when executing callback
|
|
74
|
-
*/
|
|
75
|
-
filter(callback: ObjectSliceCallback<boolean>, thisArg?: unknown): ObjectSlice {
|
|
76
|
-
const filtered = this.elements.filter((member) => {
|
|
77
|
-
const value = member.value;
|
|
78
|
-
const key = member.key;
|
|
79
|
-
|
|
80
|
-
if (value === undefined || key === undefined) {
|
|
81
|
-
return false; // Skip malformed members
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return thisArg !== undefined
|
|
85
|
-
? callback.call(thisArg, value, key, member)
|
|
86
|
-
: callback(value, key, member);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
return new ObjectSlice(filtered);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Rejects member elements that match the provided callback.
|
|
94
|
-
* @param callback - Function that receives (value, key, member) and returns boolean
|
|
95
|
-
* @param thisArg - Value to use as this when executing callback
|
|
96
|
-
*/
|
|
97
|
-
reject(callback: ObjectSliceCallback<boolean>, thisArg?: unknown): ObjectSlice {
|
|
98
|
-
const results: MemberElement[] = [];
|
|
99
|
-
for (const member of this.elements) {
|
|
100
|
-
const value = member.value;
|
|
101
|
-
const key = member.key;
|
|
102
|
-
|
|
103
|
-
if (value === undefined || key === undefined) {
|
|
104
|
-
continue;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (!callback.call(thisArg, value, key, member)) {
|
|
108
|
-
results.push(member);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return new ObjectSlice(results);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Executes a provided function once for each member element.
|
|
116
|
-
* @param callback - Function that receives (value, key, member, index)
|
|
117
|
-
* @param thisArg - Value to use as this when executing callback
|
|
118
|
-
*/
|
|
119
|
-
forEach(callback: ObjectSliceForEachCallback, thisArg?: unknown): void {
|
|
120
|
-
this.elements.forEach((member, index) => {
|
|
121
|
-
const value = member.value;
|
|
122
|
-
const key = member.key;
|
|
123
|
-
|
|
124
|
-
if (value === undefined || key === undefined) {
|
|
125
|
-
return; // Skip malformed members
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (thisArg !== undefined) {
|
|
129
|
-
callback.call(thisArg, value, key, member, index);
|
|
130
|
-
} else {
|
|
131
|
-
callback(value, key, member, index);
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Returns the first member element that satisfies the callback.
|
|
138
|
-
* @param callback - Function that receives (value, key, member) and returns boolean
|
|
139
|
-
* @param thisArg - Value to use as this when executing callback
|
|
140
|
-
*/
|
|
141
|
-
find(callback: ObjectSliceCallback<boolean>, thisArg?: unknown): MemberElement | undefined {
|
|
142
|
-
return this.elements.find((member) => {
|
|
143
|
-
const value = member.value;
|
|
144
|
-
const key = member.key;
|
|
145
|
-
|
|
146
|
-
if (value === undefined || key === undefined) {
|
|
147
|
-
return false;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return thisArg !== undefined
|
|
151
|
-
? callback.call(thisArg, value, key, member)
|
|
152
|
-
: callback(value, key, member);
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Returns an array of all keys' values.
|
|
158
|
-
*/
|
|
159
|
-
keys(): unknown[] {
|
|
160
|
-
return this.elements
|
|
161
|
-
.map((member) => member.key?.toValue())
|
|
162
|
-
.filter((key): key is unknown => key !== undefined);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Returns an array of all values' values.
|
|
167
|
-
*/
|
|
168
|
-
values(): unknown[] {
|
|
169
|
-
return this.elements
|
|
170
|
-
.map((member) => member.value?.toValue())
|
|
171
|
-
.filter((value): value is unknown => value !== undefined);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Returns the number of elements in the slice.
|
|
176
|
-
*/
|
|
177
|
-
get length(): number {
|
|
178
|
-
return this.elements.length;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Returns whether the slice is empty.
|
|
183
|
-
*/
|
|
184
|
-
get isEmpty(): boolean {
|
|
185
|
-
return this.length === 0;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Returns the first element in the slice or undefined if empty.
|
|
190
|
-
*/
|
|
191
|
-
get first(): MemberElement | undefined {
|
|
192
|
-
return this.elements[0];
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Gets the element at the specified index.
|
|
197
|
-
* @param index - The index of the element to get
|
|
198
|
-
*/
|
|
199
|
-
get(index: number): MemberElement | undefined {
|
|
200
|
-
return this.elements[index];
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Adds the given member element to the end of the slice.
|
|
205
|
-
* @param member - The member element to add
|
|
206
|
-
*/
|
|
207
|
-
push(member: MemberElement): this {
|
|
208
|
-
this.elements.push(member);
|
|
209
|
-
return this;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Determines whether the slice includes a member with the given key value.
|
|
214
|
-
* @param keyValue - The key value to search for
|
|
215
|
-
*/
|
|
216
|
-
includesKey(keyValue: unknown): boolean {
|
|
217
|
-
return this.elements.some((member) => member.key?.equals(keyValue));
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Iterator support - allows for...of loops.
|
|
222
|
-
*/
|
|
223
|
-
[Symbol.iterator](): IterableIterator<MemberElement> {
|
|
224
|
-
return this.elements[Symbol.iterator]();
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
export default ObjectSlice;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { ApiDOMStructuredError } from '@speclynx/apidom-error';
|
|
2
|
-
import type { ApiDOMErrorOptions } from '@speclynx/apidom-error';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @public
|
|
6
|
-
*/
|
|
7
|
-
export interface CloneErrorOptions extends ApiDOMErrorOptions {
|
|
8
|
-
readonly value: unknown;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @public
|
|
13
|
-
*/
|
|
14
|
-
class CloneError extends ApiDOMStructuredError {
|
|
15
|
-
public readonly value: unknown;
|
|
16
|
-
|
|
17
|
-
constructor(message?: string, structuredOptions?: CloneErrorOptions) {
|
|
18
|
-
super(message, structuredOptions);
|
|
19
|
-
|
|
20
|
-
if (typeof structuredOptions !== 'undefined') {
|
|
21
|
-
this.value = structuredOptions.value;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export default CloneError;
|