@speclynx/apidom-datamodel 4.0.1 → 4.0.3
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 +10 -0
- package/package.json +4 -5
- package/src/KeyValuePair.cjs +31 -0
- package/src/KeyValuePair.mjs +27 -0
- package/src/KeyValuePair.ts +31 -0
- package/src/Metadata.cjs +91 -0
- package/src/Metadata.mjs +87 -0
- package/src/Metadata.ts +100 -0
- package/src/Namespace.cjs +212 -0
- package/src/Namespace.mjs +206 -0
- package/src/Namespace.ts +260 -0
- package/src/ObjectSlice.cjs +199 -0
- package/src/ObjectSlice.mjs +195 -0
- package/src/ObjectSlice.ts +228 -0
- package/src/clone/errors/CloneError.cjs +22 -0
- package/src/clone/errors/CloneError.mjs +19 -0
- package/src/clone/errors/CloneError.ts +26 -0
- package/src/clone/errors/DeepCloneError.cjs +11 -0
- package/src/clone/errors/DeepCloneError.mjs +6 -0
- package/src/clone/errors/DeepCloneError.ts +8 -0
- package/src/clone/errors/ShallowCloneError.cjs +11 -0
- package/src/clone/errors/ShallowCloneError.mjs +6 -0
- package/src/clone/errors/ShallowCloneError.ts +8 -0
- package/src/clone/index.cjs +188 -0
- package/src/clone/index.mjs +178 -0
- package/src/clone/index.ts +195 -0
- package/src/elements/Annotation.cjs +35 -0
- package/src/elements/Annotation.mjs +30 -0
- package/src/elements/Annotation.ts +35 -0
- package/src/elements/Comment.cjs +18 -0
- package/src/elements/Comment.mjs +13 -0
- package/src/elements/Comment.ts +16 -0
- package/src/elements/LinkElement.cjs +50 -0
- package/src/elements/LinkElement.mjs +45 -0
- package/src/elements/LinkElement.ts +49 -0
- package/src/elements/ParseResult.cjs +91 -0
- package/src/elements/ParseResult.mjs +86 -0
- package/src/elements/ParseResult.ts +94 -0
- package/src/elements/RefElement.cjs +34 -0
- package/src/elements/RefElement.mjs +29 -0
- package/src/elements/RefElement.ts +33 -0
- package/src/elements/SourceMap.cjs +140 -0
- package/src/elements/SourceMap.mjs +134 -0
- package/src/elements/SourceMap.ts +170 -0
- package/src/elements/Style.cjs +54 -0
- package/src/elements/Style.mjs +48 -0
- package/src/elements/Style.ts +56 -0
- package/src/index.cjs +58 -0
- package/src/index.mjs +11 -0
- package/src/index.ts +79 -0
- package/src/predicates/elements.cjs +46 -0
- package/src/predicates/elements.mjs +35 -0
- package/src/predicates/elements.ts +42 -0
- package/src/predicates/index.cjs +77 -0
- package/src/predicates/index.mjs +56 -0
- package/src/predicates/index.ts +89 -0
- package/src/predicates/primitives.cjs +69 -0
- package/src/predicates/primitives.mjs +56 -0
- package/src/predicates/primitives.ts +79 -0
- package/src/primitives/ArrayElement.cjs +155 -0
- package/src/primitives/ArrayElement.mjs +148 -0
- package/src/primitives/ArrayElement.ts +161 -0
- package/src/primitives/BooleanElement.cjs +20 -0
- package/src/primitives/BooleanElement.mjs +15 -0
- package/src/primitives/BooleanElement.ts +18 -0
- package/src/primitives/CollectionElement.cjs +180 -0
- package/src/primitives/CollectionElement.mjs +173 -0
- package/src/primitives/CollectionElement.ts +191 -0
- package/src/primitives/Element.cjs +510 -0
- package/src/primitives/Element.mjs +505 -0
- package/src/primitives/Element.ts +556 -0
- package/src/primitives/MemberElement.cjs +58 -0
- package/src/primitives/MemberElement.mjs +53 -0
- package/src/primitives/MemberElement.ts +61 -0
- package/src/primitives/NullElement.cjs +28 -0
- package/src/primitives/NullElement.mjs +23 -0
- package/src/primitives/NullElement.ts +26 -0
- package/src/primitives/NumberElement.cjs +20 -0
- package/src/primitives/NumberElement.mjs +15 -0
- package/src/primitives/NumberElement.ts +18 -0
- package/src/primitives/ObjectElement.cjs +220 -0
- package/src/primitives/ObjectElement.mjs +214 -0
- package/src/primitives/ObjectElement.ts +263 -0
- package/src/primitives/StringElement.cjs +27 -0
- package/src/primitives/StringElement.mjs +22 -0
- package/src/primitives/StringElement.ts +25 -0
- package/src/registration.cjs +101 -0
- package/src/registration.mjs +79 -0
- package/src/registration.ts +111 -0
- package/src/serialisers/JSONSerialiser.cjs +230 -0
- package/src/serialisers/JSONSerialiser.mjs +221 -0
- package/src/serialisers/JSONSerialiser.ts +295 -0
- package/src/types.cjs +3 -0
- package/src/types.mjs +1 -0
- package/src/types.ts +72 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import KeyValuePair from '../KeyValuePair.ts';
|
|
2
|
+
import Element, { type Meta, type Attributes } from './Element.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* MemberElement represents a key-value pair member in an ObjectElement.
|
|
6
|
+
*
|
|
7
|
+
* The member's content is always a KeyValuePair containing:
|
|
8
|
+
* - `key`: The key element (typically a StringElement)
|
|
9
|
+
* - `value`: The value element (any Element type)
|
|
10
|
+
*
|
|
11
|
+
* @typeParam K - The key element type, defaults to Element
|
|
12
|
+
* @typeParam V - The value element type, defaults to Element
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
class MemberElement<K extends Element = Element, V extends Element = Element> extends Element {
|
|
16
|
+
declare protected _content: KeyValuePair;
|
|
17
|
+
|
|
18
|
+
constructor(key?: unknown, value?: unknown, meta?: Meta, attributes?: Attributes) {
|
|
19
|
+
super(new KeyValuePair(), meta, attributes);
|
|
20
|
+
this.element = 'member';
|
|
21
|
+
|
|
22
|
+
if (key !== undefined) {
|
|
23
|
+
this.key = key;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Note: We check arguments.length to distinguish between:
|
|
27
|
+
// - new MemberElement('key') - value not provided, don't set
|
|
28
|
+
// - new MemberElement('key', undefined) - value explicitly undefined, set it
|
|
29
|
+
if (arguments.length >= 2) {
|
|
30
|
+
this.value = value;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
primitive(): string {
|
|
35
|
+
return 'member';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The key element of this member.
|
|
40
|
+
*/
|
|
41
|
+
get key(): K | undefined {
|
|
42
|
+
return this._content.key as K | undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
set key(value: unknown) {
|
|
46
|
+
this._content.key = this.refract(value);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The value element of this member.
|
|
51
|
+
*/
|
|
52
|
+
get value(): V | undefined {
|
|
53
|
+
return this._content.value as V | undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
set value(value: unknown) {
|
|
57
|
+
this._content.value = value === undefined ? undefined : this.refract(value);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export default MemberElement;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
|
4
|
+
exports.__esModule = true;
|
|
5
|
+
exports.default = void 0;
|
|
6
|
+
var _Element = _interopRequireDefault(require("./Element.cjs"));
|
|
7
|
+
/**
|
|
8
|
+
* NullElement represents a null value in ApiDOM.
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
class NullElement extends _Element.default {
|
|
12
|
+
constructor(content, meta, attributes) {
|
|
13
|
+
super(content ?? null, meta, attributes);
|
|
14
|
+
this.element = 'null';
|
|
15
|
+
}
|
|
16
|
+
primitive() {
|
|
17
|
+
return 'null';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* NullElement cannot have its value changed.
|
|
22
|
+
* @throws Error - NullElement value cannot be modified
|
|
23
|
+
*/
|
|
24
|
+
set(_content) {
|
|
25
|
+
throw new Error('Cannot set the value of null');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
var _default = exports.default = NullElement;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import Element from "./Element.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* NullElement represents a null value in ApiDOM.
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
class NullElement extends Element {
|
|
7
|
+
constructor(content, meta, attributes) {
|
|
8
|
+
super(content ?? null, meta, attributes);
|
|
9
|
+
this.element = 'null';
|
|
10
|
+
}
|
|
11
|
+
primitive() {
|
|
12
|
+
return 'null';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* NullElement cannot have its value changed.
|
|
17
|
+
* @throws Error - NullElement value cannot be modified
|
|
18
|
+
*/
|
|
19
|
+
set(_content) {
|
|
20
|
+
throw new Error('Cannot set the value of null');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export default NullElement;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import Element, { type Meta, type Attributes } from './Element.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* NullElement represents a null value in ApiDOM.
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
class NullElement extends Element {
|
|
8
|
+
constructor(content?: null, meta?: Meta, attributes?: Attributes) {
|
|
9
|
+
super(content ?? null, meta, attributes);
|
|
10
|
+
this.element = 'null';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
primitive(): string {
|
|
14
|
+
return 'null';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* NullElement cannot have its value changed.
|
|
19
|
+
* @throws Error - NullElement value cannot be modified
|
|
20
|
+
*/
|
|
21
|
+
override set(_content?: unknown): this {
|
|
22
|
+
throw new Error('Cannot set the value of null');
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default NullElement;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
|
4
|
+
exports.__esModule = true;
|
|
5
|
+
exports.default = void 0;
|
|
6
|
+
var _Element = _interopRequireDefault(require("./Element.cjs"));
|
|
7
|
+
/**
|
|
8
|
+
* NumberElement represents a numeric value in ApiDOM.
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
class NumberElement extends _Element.default {
|
|
12
|
+
constructor(content, meta, attributes) {
|
|
13
|
+
super(content, meta, attributes);
|
|
14
|
+
this.element = 'number';
|
|
15
|
+
}
|
|
16
|
+
primitive() {
|
|
17
|
+
return 'number';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
var _default = exports.default = NumberElement;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import Element from "./Element.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* NumberElement represents a numeric value in ApiDOM.
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
class NumberElement extends Element {
|
|
7
|
+
constructor(content, meta, attributes) {
|
|
8
|
+
super(content, meta, attributes);
|
|
9
|
+
this.element = 'number';
|
|
10
|
+
}
|
|
11
|
+
primitive() {
|
|
12
|
+
return 'number';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export default NumberElement;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import Element, { type Meta, type Attributes } from './Element.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* NumberElement represents a numeric value in ApiDOM.
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
class NumberElement extends Element {
|
|
8
|
+
constructor(content?: number, meta?: Meta, attributes?: Attributes) {
|
|
9
|
+
super(content, meta, attributes);
|
|
10
|
+
this.element = 'number';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
primitive(): string {
|
|
14
|
+
return 'number';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default NumberElement;
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
|
4
|
+
exports.__esModule = true;
|
|
5
|
+
exports.default = void 0;
|
|
6
|
+
var _CollectionElement = _interopRequireDefault(require("./CollectionElement.cjs"));
|
|
7
|
+
var _MemberElement = _interopRequireDefault(require("./MemberElement.cjs"));
|
|
8
|
+
var _ObjectSlice = _interopRequireDefault(require("../ObjectSlice.cjs"));
|
|
9
|
+
/**
|
|
10
|
+
* Callback type for ObjectElement iteration methods.
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* ObjectElement represents an object/dictionary of key-value pairs in ApiDOM.
|
|
16
|
+
*
|
|
17
|
+
* @typeParam K - The key element type, defaults to Element
|
|
18
|
+
* @typeParam V - The value element type, defaults to Element
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
class ObjectElement extends _CollectionElement.default {
|
|
22
|
+
constructor(content, meta, attributes) {
|
|
23
|
+
super(content || [], meta, attributes);
|
|
24
|
+
this.element = 'object';
|
|
25
|
+
}
|
|
26
|
+
primitive() {
|
|
27
|
+
return 'object';
|
|
28
|
+
}
|
|
29
|
+
toValue() {
|
|
30
|
+
return this._content.reduce((results, el) => {
|
|
31
|
+
results[el.key.toValue()] = el.value ? el.value.toValue() : undefined;
|
|
32
|
+
return results;
|
|
33
|
+
}, {});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Gets the value element for the given key.
|
|
38
|
+
*/
|
|
39
|
+
get(name) {
|
|
40
|
+
const member = this.getMember(name);
|
|
41
|
+
if (member) {
|
|
42
|
+
return member.value;
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Helper for returning the value of an item by key.
|
|
49
|
+
*/
|
|
50
|
+
getValue(name) {
|
|
51
|
+
const item = this.get(name);
|
|
52
|
+
if (item) {
|
|
53
|
+
return item.toValue();
|
|
54
|
+
}
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Gets the member element for the given key.
|
|
60
|
+
*/
|
|
61
|
+
getMember(name) {
|
|
62
|
+
if (name === undefined) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
return this._content.find(element => element.key.toValue() === name);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Removes the member with the given key.
|
|
70
|
+
*/
|
|
71
|
+
remove(name) {
|
|
72
|
+
let removed = null;
|
|
73
|
+
this.content = this._content.filter(item => {
|
|
74
|
+
if (item.key.toValue() === name) {
|
|
75
|
+
removed = item;
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
});
|
|
80
|
+
return removed;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Gets the key element for the given key name.
|
|
85
|
+
*/
|
|
86
|
+
getKey(name) {
|
|
87
|
+
const member = this.getMember(name);
|
|
88
|
+
if (member) {
|
|
89
|
+
return member.key;
|
|
90
|
+
}
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Set allows either a key/value pair to be given or an object.
|
|
96
|
+
* If an object is given, each key is set to its respective value.
|
|
97
|
+
*/
|
|
98
|
+
set(keyOrObject, value) {
|
|
99
|
+
if (typeof keyOrObject === 'string') {
|
|
100
|
+
const member = this.getMember(keyOrObject);
|
|
101
|
+
if (member) {
|
|
102
|
+
member.value = value;
|
|
103
|
+
} else {
|
|
104
|
+
this._content.push(new _MemberElement.default(keyOrObject, value));
|
|
105
|
+
}
|
|
106
|
+
} else if (typeof keyOrObject === 'object' && !Array.isArray(keyOrObject)) {
|
|
107
|
+
for (const objectKey of Object.keys(keyOrObject)) {
|
|
108
|
+
this.set(objectKey, keyOrObject[objectKey]);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Returns an array of all keys' values.
|
|
116
|
+
*/
|
|
117
|
+
keys() {
|
|
118
|
+
return this._content.map(item => item.key.toValue());
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Returns an array of all values' values.
|
|
123
|
+
*/
|
|
124
|
+
values() {
|
|
125
|
+
return this._content.map(item => item.value.toValue());
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Returns whether the object has the given key.
|
|
130
|
+
*/
|
|
131
|
+
hasKey(value) {
|
|
132
|
+
return this._content.some(member => member.key.equals(value));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Returns an array of [key, value] pairs.
|
|
137
|
+
*/
|
|
138
|
+
items() {
|
|
139
|
+
return this._content.map(item => [item.key.toValue(), item.value.toValue()]);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Maps over the member elements, calling callback with (value, key, member).
|
|
144
|
+
*/
|
|
145
|
+
map(callback, thisArg) {
|
|
146
|
+
return this._content.map(item => callback.call(thisArg, item.value, item.key, item));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Returns an array containing the truthy results of calling the given transformation.
|
|
151
|
+
*/
|
|
152
|
+
compactMap(callback, thisArg) {
|
|
153
|
+
const results = [];
|
|
154
|
+
this.forEach((value, key, member) => {
|
|
155
|
+
const result = callback.call(thisArg, value, key, member);
|
|
156
|
+
if (result) {
|
|
157
|
+
results.push(result);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
return results;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Filters member elements using the provided callback.
|
|
165
|
+
*/
|
|
166
|
+
filter(callback, thisArg) {
|
|
167
|
+
return new _ObjectSlice.default(this._content).filter(callback, thisArg);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Rejects member elements that match the provided callback.
|
|
172
|
+
*/
|
|
173
|
+
reject(callback, thisArg) {
|
|
174
|
+
const results = [];
|
|
175
|
+
for (const member of this._content) {
|
|
176
|
+
if (!callback.call(thisArg, member.value, member.key, member)) {
|
|
177
|
+
results.push(member);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return new _ObjectSlice.default(results);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Executes a provided function once for each member element.
|
|
185
|
+
*/
|
|
186
|
+
forEach(callback, thisArg) {
|
|
187
|
+
this._content.forEach(item => callback.call(thisArg, item.value, item.key, item));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Reduces the object to a single value.
|
|
192
|
+
* Callback receives (memo, value, key, member, obj).
|
|
193
|
+
*/
|
|
194
|
+
reduce(callback, initialValue) {
|
|
195
|
+
let startIndex;
|
|
196
|
+
let memo;
|
|
197
|
+
if (initialValue !== undefined) {
|
|
198
|
+
startIndex = 0;
|
|
199
|
+
memo = this.refract(initialValue);
|
|
200
|
+
} else {
|
|
201
|
+
startIndex = 1;
|
|
202
|
+
// For objects, memo starts as the first member's value
|
|
203
|
+
memo = this._content[0]?.value;
|
|
204
|
+
}
|
|
205
|
+
for (let i = startIndex; i < this._content.length; i += 1) {
|
|
206
|
+
const member = this._content[i];
|
|
207
|
+
const result = callback(memo, member.value, member.key, member, this);
|
|
208
|
+
memo = result === undefined ? result : this.refract(result);
|
|
209
|
+
}
|
|
210
|
+
return memo;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Returns an empty object element.
|
|
215
|
+
*/
|
|
216
|
+
empty() {
|
|
217
|
+
return new this.constructor([]);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
var _default = exports.default = ObjectElement;
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import CollectionElement from "./CollectionElement.mjs";
|
|
2
|
+
import MemberElement from "./MemberElement.mjs";
|
|
3
|
+
import ObjectSlice from "../ObjectSlice.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Callback type for ObjectElement iteration methods.
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* ObjectElement represents an object/dictionary of key-value pairs in ApiDOM.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam K - The key element type, defaults to Element
|
|
12
|
+
* @typeParam V - The value element type, defaults to Element
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
class ObjectElement extends CollectionElement {
|
|
16
|
+
constructor(content, meta, attributes) {
|
|
17
|
+
super(content || [], meta, attributes);
|
|
18
|
+
this.element = 'object';
|
|
19
|
+
}
|
|
20
|
+
primitive() {
|
|
21
|
+
return 'object';
|
|
22
|
+
}
|
|
23
|
+
toValue() {
|
|
24
|
+
return this._content.reduce((results, el) => {
|
|
25
|
+
results[el.key.toValue()] = el.value ? el.value.toValue() : undefined;
|
|
26
|
+
return results;
|
|
27
|
+
}, {});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Gets the value element for the given key.
|
|
32
|
+
*/
|
|
33
|
+
get(name) {
|
|
34
|
+
const member = this.getMember(name);
|
|
35
|
+
if (member) {
|
|
36
|
+
return member.value;
|
|
37
|
+
}
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Helper for returning the value of an item by key.
|
|
43
|
+
*/
|
|
44
|
+
getValue(name) {
|
|
45
|
+
const item = this.get(name);
|
|
46
|
+
if (item) {
|
|
47
|
+
return item.toValue();
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Gets the member element for the given key.
|
|
54
|
+
*/
|
|
55
|
+
getMember(name) {
|
|
56
|
+
if (name === undefined) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
return this._content.find(element => element.key.toValue() === name);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Removes the member with the given key.
|
|
64
|
+
*/
|
|
65
|
+
remove(name) {
|
|
66
|
+
let removed = null;
|
|
67
|
+
this.content = this._content.filter(item => {
|
|
68
|
+
if (item.key.toValue() === name) {
|
|
69
|
+
removed = item;
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
});
|
|
74
|
+
return removed;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Gets the key element for the given key name.
|
|
79
|
+
*/
|
|
80
|
+
getKey(name) {
|
|
81
|
+
const member = this.getMember(name);
|
|
82
|
+
if (member) {
|
|
83
|
+
return member.key;
|
|
84
|
+
}
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Set allows either a key/value pair to be given or an object.
|
|
90
|
+
* If an object is given, each key is set to its respective value.
|
|
91
|
+
*/
|
|
92
|
+
set(keyOrObject, value) {
|
|
93
|
+
if (typeof keyOrObject === 'string') {
|
|
94
|
+
const member = this.getMember(keyOrObject);
|
|
95
|
+
if (member) {
|
|
96
|
+
member.value = value;
|
|
97
|
+
} else {
|
|
98
|
+
this._content.push(new MemberElement(keyOrObject, value));
|
|
99
|
+
}
|
|
100
|
+
} else if (typeof keyOrObject === 'object' && !Array.isArray(keyOrObject)) {
|
|
101
|
+
for (const objectKey of Object.keys(keyOrObject)) {
|
|
102
|
+
this.set(objectKey, keyOrObject[objectKey]);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Returns an array of all keys' values.
|
|
110
|
+
*/
|
|
111
|
+
keys() {
|
|
112
|
+
return this._content.map(item => item.key.toValue());
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Returns an array of all values' values.
|
|
117
|
+
*/
|
|
118
|
+
values() {
|
|
119
|
+
return this._content.map(item => item.value.toValue());
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Returns whether the object has the given key.
|
|
124
|
+
*/
|
|
125
|
+
hasKey(value) {
|
|
126
|
+
return this._content.some(member => member.key.equals(value));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Returns an array of [key, value] pairs.
|
|
131
|
+
*/
|
|
132
|
+
items() {
|
|
133
|
+
return this._content.map(item => [item.key.toValue(), item.value.toValue()]);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Maps over the member elements, calling callback with (value, key, member).
|
|
138
|
+
*/
|
|
139
|
+
map(callback, thisArg) {
|
|
140
|
+
return this._content.map(item => callback.call(thisArg, item.value, item.key, item));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Returns an array containing the truthy results of calling the given transformation.
|
|
145
|
+
*/
|
|
146
|
+
compactMap(callback, thisArg) {
|
|
147
|
+
const results = [];
|
|
148
|
+
this.forEach((value, key, member) => {
|
|
149
|
+
const result = callback.call(thisArg, value, key, member);
|
|
150
|
+
if (result) {
|
|
151
|
+
results.push(result);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
return results;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Filters member elements using the provided callback.
|
|
159
|
+
*/
|
|
160
|
+
filter(callback, thisArg) {
|
|
161
|
+
return new ObjectSlice(this._content).filter(callback, thisArg);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Rejects member elements that match the provided callback.
|
|
166
|
+
*/
|
|
167
|
+
reject(callback, thisArg) {
|
|
168
|
+
const results = [];
|
|
169
|
+
for (const member of this._content) {
|
|
170
|
+
if (!callback.call(thisArg, member.value, member.key, member)) {
|
|
171
|
+
results.push(member);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return new ObjectSlice(results);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Executes a provided function once for each member element.
|
|
179
|
+
*/
|
|
180
|
+
forEach(callback, thisArg) {
|
|
181
|
+
this._content.forEach(item => callback.call(thisArg, item.value, item.key, item));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Reduces the object to a single value.
|
|
186
|
+
* Callback receives (memo, value, key, member, obj).
|
|
187
|
+
*/
|
|
188
|
+
reduce(callback, initialValue) {
|
|
189
|
+
let startIndex;
|
|
190
|
+
let memo;
|
|
191
|
+
if (initialValue !== undefined) {
|
|
192
|
+
startIndex = 0;
|
|
193
|
+
memo = this.refract(initialValue);
|
|
194
|
+
} else {
|
|
195
|
+
startIndex = 1;
|
|
196
|
+
// For objects, memo starts as the first member's value
|
|
197
|
+
memo = this._content[0]?.value;
|
|
198
|
+
}
|
|
199
|
+
for (let i = startIndex; i < this._content.length; i += 1) {
|
|
200
|
+
const member = this._content[i];
|
|
201
|
+
const result = callback(memo, member.value, member.key, member, this);
|
|
202
|
+
memo = result === undefined ? result : this.refract(result);
|
|
203
|
+
}
|
|
204
|
+
return memo;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Returns an empty object element.
|
|
209
|
+
*/
|
|
210
|
+
empty() {
|
|
211
|
+
return new this.constructor([]);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
export default ObjectElement;
|