@speclynx/apidom-datamodel 3.2.0 → 4.0.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.
- package/CHANGELOG.md +8 -0
- package/package.json +3 -3
- package/src/KeyValuePair.cjs +0 -31
- package/src/KeyValuePair.mjs +0 -27
- package/src/Metadata.cjs +0 -91
- package/src/Metadata.mjs +0 -87
- package/src/Namespace.cjs +0 -212
- package/src/Namespace.mjs +0 -206
- package/src/ObjectSlice.cjs +0 -199
- package/src/ObjectSlice.mjs +0 -195
- package/src/clone/errors/CloneError.cjs +0 -22
- package/src/clone/errors/CloneError.mjs +0 -19
- package/src/clone/errors/DeepCloneError.cjs +0 -11
- package/src/clone/errors/DeepCloneError.mjs +0 -6
- package/src/clone/errors/ShallowCloneError.cjs +0 -11
- package/src/clone/errors/ShallowCloneError.mjs +0 -6
- package/src/clone/index.cjs +0 -188
- package/src/clone/index.mjs +0 -178
- package/src/elements/Annotation.cjs +0 -35
- package/src/elements/Annotation.mjs +0 -30
- package/src/elements/Comment.cjs +0 -18
- package/src/elements/Comment.mjs +0 -13
- package/src/elements/LinkElement.cjs +0 -50
- package/src/elements/LinkElement.mjs +0 -45
- package/src/elements/ParseResult.cjs +0 -91
- package/src/elements/ParseResult.mjs +0 -86
- package/src/elements/RefElement.cjs +0 -34
- package/src/elements/RefElement.mjs +0 -29
- package/src/elements/SourceMap.cjs +0 -140
- package/src/elements/SourceMap.mjs +0 -134
- package/src/elements/Style.cjs +0 -54
- package/src/elements/Style.mjs +0 -48
- package/src/index.cjs +0 -58
- package/src/index.mjs +0 -11
- package/src/predicates/elements.cjs +0 -46
- package/src/predicates/elements.mjs +0 -35
- package/src/predicates/index.cjs +0 -77
- package/src/predicates/index.mjs +0 -56
- package/src/predicates/primitives.cjs +0 -69
- package/src/predicates/primitives.mjs +0 -56
- package/src/primitives/ArrayElement.cjs +0 -155
- package/src/primitives/ArrayElement.mjs +0 -148
- package/src/primitives/BooleanElement.cjs +0 -20
- package/src/primitives/BooleanElement.mjs +0 -15
- package/src/primitives/CollectionElement.cjs +0 -180
- package/src/primitives/CollectionElement.mjs +0 -173
- package/src/primitives/Element.cjs +0 -510
- package/src/primitives/Element.mjs +0 -505
- package/src/primitives/MemberElement.cjs +0 -58
- package/src/primitives/MemberElement.mjs +0 -53
- package/src/primitives/NullElement.cjs +0 -28
- package/src/primitives/NullElement.mjs +0 -23
- package/src/primitives/NumberElement.cjs +0 -20
- package/src/primitives/NumberElement.mjs +0 -15
- package/src/primitives/ObjectElement.cjs +0 -220
- package/src/primitives/ObjectElement.mjs +0 -214
- package/src/primitives/StringElement.cjs +0 -27
- package/src/primitives/StringElement.mjs +0 -22
- package/src/registration.cjs +0 -101
- package/src/registration.mjs +0 -79
- package/src/serialisers/JSONSerialiser.cjs +0 -230
- package/src/serialisers/JSONSerialiser.mjs +0 -221
- package/src/types.cjs +0 -3
- package/src/types.mjs +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
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.0](https://github.com/speclynx/apidom/compare/v3.2.1...v4.0.0) (2026-03-11)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @speclynx/apidom-datamodel
|
|
9
|
+
|
|
10
|
+
## [3.2.1](https://github.com/speclynx/apidom/compare/v3.2.0...v3.2.1) (2026-03-09)
|
|
11
|
+
|
|
12
|
+
**Note:** Version bump only for package @speclynx/apidom-datamodel
|
|
13
|
+
|
|
6
14
|
# [3.2.0](https://github.com/speclynx/apidom/compare/v3.1.0...v3.2.0) (2026-03-08)
|
|
7
15
|
|
|
8
16
|
### Features
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@speclynx/apidom-datamodel",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Data model primitives for ApiDOM.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"apidom",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"license": "Apache-2.0",
|
|
56
56
|
"dependencies": {
|
|
57
57
|
"@babel/runtime-corejs3": "^7.28.4",
|
|
58
|
-
"@speclynx/apidom-error": "
|
|
58
|
+
"@speclynx/apidom-error": "4.0.0",
|
|
59
59
|
"ramda": "~0.32.0"
|
|
60
60
|
},
|
|
61
61
|
"files": [
|
|
@@ -68,5 +68,5 @@
|
|
|
68
68
|
"README.md",
|
|
69
69
|
"CHANGELOG.md"
|
|
70
70
|
],
|
|
71
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "384ff82a1bb68d015fd6801de641898b4b582876"
|
|
72
72
|
}
|
package/src/KeyValuePair.cjs
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.default = void 0;
|
|
5
|
-
/**
|
|
6
|
-
* Represents a key-value pair used in MemberElement content.
|
|
7
|
-
* This is used internally to store object member data.
|
|
8
|
-
*
|
|
9
|
-
* @typeParam K - Key element type
|
|
10
|
-
* @typeParam V - Value element type
|
|
11
|
-
* @public
|
|
12
|
-
*/
|
|
13
|
-
class KeyValuePair {
|
|
14
|
-
key;
|
|
15
|
-
value;
|
|
16
|
-
constructor(key, value) {
|
|
17
|
-
this.key = key;
|
|
18
|
-
this.value = value;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Converts to a plain JavaScript object representation.
|
|
23
|
-
*/
|
|
24
|
-
toValue() {
|
|
25
|
-
return {
|
|
26
|
-
key: this.key?.toValue(),
|
|
27
|
-
value: this.value?.toValue()
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
var _default = exports.default = KeyValuePair;
|
package/src/KeyValuePair.mjs
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents a key-value pair used in MemberElement content.
|
|
3
|
-
* This is used internally to store object member data.
|
|
4
|
-
*
|
|
5
|
-
* @typeParam K - Key element type
|
|
6
|
-
* @typeParam V - Value element type
|
|
7
|
-
* @public
|
|
8
|
-
*/
|
|
9
|
-
class KeyValuePair {
|
|
10
|
-
key;
|
|
11
|
-
value;
|
|
12
|
-
constructor(key, value) {
|
|
13
|
-
this.key = key;
|
|
14
|
-
this.value = value;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Converts to a plain JavaScript object representation.
|
|
19
|
-
*/
|
|
20
|
-
toValue() {
|
|
21
|
-
return {
|
|
22
|
-
key: this.key?.toValue(),
|
|
23
|
-
value: this.value?.toValue()
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
export default KeyValuePair;
|
package/src/Metadata.cjs
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.default = void 0;
|
|
5
|
-
var _ramda = require("ramda");
|
|
6
|
-
/**
|
|
7
|
-
* Lightweight meta container for Element metadata.
|
|
8
|
-
*
|
|
9
|
-
* Data is stored as own properties on the instance; methods live on the prototype.
|
|
10
|
-
* `Object.keys()`, `Object.entries()`, etc. only see data properties.
|
|
11
|
-
*
|
|
12
|
-
* @public
|
|
13
|
-
*/
|
|
14
|
-
class Metadata {
|
|
15
|
-
// Set via prototype assignment in registration.ts to avoid circular dependency
|
|
16
|
-
|
|
17
|
-
get(name) {
|
|
18
|
-
return this[name];
|
|
19
|
-
}
|
|
20
|
-
set(name, value) {
|
|
21
|
-
this[name] = value;
|
|
22
|
-
}
|
|
23
|
-
hasKey(name) {
|
|
24
|
-
return Object.hasOwn(this, name);
|
|
25
|
-
}
|
|
26
|
-
keys() {
|
|
27
|
-
return Object.keys(this);
|
|
28
|
-
}
|
|
29
|
-
remove(name) {
|
|
30
|
-
delete this[name];
|
|
31
|
-
}
|
|
32
|
-
get isEmpty() {
|
|
33
|
-
return Object.keys(this).length === 0;
|
|
34
|
-
}
|
|
35
|
-
get isFrozen() {
|
|
36
|
-
return Object.isFrozen(this);
|
|
37
|
-
}
|
|
38
|
-
freeze() {
|
|
39
|
-
for (const value of Object.values(this)) {
|
|
40
|
-
if (value instanceof this.Element) {
|
|
41
|
-
value.freeze();
|
|
42
|
-
} else if (Array.isArray(value) || value !== null && typeof value === 'object') {
|
|
43
|
-
Object.freeze(value);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
Object.freeze(this);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Creates a shallow clone. Same references, new container.
|
|
51
|
-
*/
|
|
52
|
-
cloneShallow() {
|
|
53
|
-
const clone = new Metadata();
|
|
54
|
-
Object.assign(clone, this);
|
|
55
|
-
return clone;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Merges another Metadata into a new instance.
|
|
60
|
-
* Arrays are concatenated, all other values are overwritten by source.
|
|
61
|
-
*/
|
|
62
|
-
merge(source) {
|
|
63
|
-
const result = this.cloneShallow();
|
|
64
|
-
for (const [key, value] of Object.entries(source)) {
|
|
65
|
-
const existing = result.get(key);
|
|
66
|
-
if (Array.isArray(existing) && Array.isArray(value)) {
|
|
67
|
-
result.set(key, [...existing, ...value]);
|
|
68
|
-
} else {
|
|
69
|
-
result.set(key, value);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return result;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Creates a deep clone. Elements are deep cloned,
|
|
77
|
-
* all other values are deep cloned via ramda clone.
|
|
78
|
-
*/
|
|
79
|
-
cloneDeep() {
|
|
80
|
-
const copy = new Metadata();
|
|
81
|
-
for (const [key, value] of Object.entries(this)) {
|
|
82
|
-
if (value instanceof this.Element) {
|
|
83
|
-
copy.set(key, this.cloneDeepElement(value));
|
|
84
|
-
} else {
|
|
85
|
-
copy.set(key, (0, _ramda.clone)(value));
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return copy;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
var _default = exports.default = Metadata;
|
package/src/Metadata.mjs
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { clone } from 'ramda';
|
|
2
|
-
/**
|
|
3
|
-
* Lightweight meta container for Element metadata.
|
|
4
|
-
*
|
|
5
|
-
* Data is stored as own properties on the instance; methods live on the prototype.
|
|
6
|
-
* `Object.keys()`, `Object.entries()`, etc. only see data properties.
|
|
7
|
-
*
|
|
8
|
-
* @public
|
|
9
|
-
*/
|
|
10
|
-
class Metadata {
|
|
11
|
-
// Set via prototype assignment in registration.ts to avoid circular dependency
|
|
12
|
-
|
|
13
|
-
get(name) {
|
|
14
|
-
return this[name];
|
|
15
|
-
}
|
|
16
|
-
set(name, value) {
|
|
17
|
-
this[name] = value;
|
|
18
|
-
}
|
|
19
|
-
hasKey(name) {
|
|
20
|
-
return Object.hasOwn(this, name);
|
|
21
|
-
}
|
|
22
|
-
keys() {
|
|
23
|
-
return Object.keys(this);
|
|
24
|
-
}
|
|
25
|
-
remove(name) {
|
|
26
|
-
delete this[name];
|
|
27
|
-
}
|
|
28
|
-
get isEmpty() {
|
|
29
|
-
return Object.keys(this).length === 0;
|
|
30
|
-
}
|
|
31
|
-
get isFrozen() {
|
|
32
|
-
return Object.isFrozen(this);
|
|
33
|
-
}
|
|
34
|
-
freeze() {
|
|
35
|
-
for (const value of Object.values(this)) {
|
|
36
|
-
if (value instanceof this.Element) {
|
|
37
|
-
value.freeze();
|
|
38
|
-
} else if (Array.isArray(value) || value !== null && typeof value === 'object') {
|
|
39
|
-
Object.freeze(value);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
Object.freeze(this);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Creates a shallow clone. Same references, new container.
|
|
47
|
-
*/
|
|
48
|
-
cloneShallow() {
|
|
49
|
-
const clone = new Metadata();
|
|
50
|
-
Object.assign(clone, this);
|
|
51
|
-
return clone;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Merges another Metadata into a new instance.
|
|
56
|
-
* Arrays are concatenated, all other values are overwritten by source.
|
|
57
|
-
*/
|
|
58
|
-
merge(source) {
|
|
59
|
-
const result = this.cloneShallow();
|
|
60
|
-
for (const [key, value] of Object.entries(source)) {
|
|
61
|
-
const existing = result.get(key);
|
|
62
|
-
if (Array.isArray(existing) && Array.isArray(value)) {
|
|
63
|
-
result.set(key, [...existing, ...value]);
|
|
64
|
-
} else {
|
|
65
|
-
result.set(key, value);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return result;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Creates a deep clone. Elements are deep cloned,
|
|
73
|
-
* all other values are deep cloned via ramda clone.
|
|
74
|
-
*/
|
|
75
|
-
cloneDeep() {
|
|
76
|
-
const copy = new Metadata();
|
|
77
|
-
for (const [key, value] of Object.entries(this)) {
|
|
78
|
-
if (value instanceof this.Element) {
|
|
79
|
-
copy.set(key, this.cloneDeepElement(value));
|
|
80
|
-
} else {
|
|
81
|
-
copy.set(key, clone(value));
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return copy;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
export default Metadata;
|
package/src/Namespace.cjs
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard").default;
|
|
4
|
-
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
|
5
|
-
exports.__esModule = true;
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
var _JSONSerialiser = _interopRequireDefault(require("./serialisers/JSONSerialiser.cjs"));
|
|
8
|
-
var elements = _interopRequireWildcard(require("./registration.cjs"));
|
|
9
|
-
const isNull = value => value === null;
|
|
10
|
-
const isString = value => typeof value === 'string';
|
|
11
|
-
const isNumber = value => typeof value === 'number';
|
|
12
|
-
const isBoolean = value => typeof value === 'boolean';
|
|
13
|
-
const isObject = value => value !== null && typeof value === 'object';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Constructor type for Element classes.
|
|
17
|
-
* @public
|
|
18
|
-
*/
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Function to test if a value should be converted to a specific element type.
|
|
23
|
-
* @public
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Tuple of detection test and element class.
|
|
28
|
-
* @public
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Options for Namespace constructor.
|
|
33
|
-
* @public
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Plugin interface for extending Namespace.
|
|
38
|
-
* @public
|
|
39
|
-
*/
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Map of registered element classes.
|
|
43
|
-
* @public
|
|
44
|
-
*/
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* A refract element implementation with an extensible namespace, able to
|
|
48
|
-
* load other namespaces into it.
|
|
49
|
-
*
|
|
50
|
-
* The namespace allows you to register your own classes to be instantiated
|
|
51
|
-
* when a particular refract element is encountered, and allows you to specify
|
|
52
|
-
* which elements get instantiated for existing JavaScript objects.
|
|
53
|
-
*
|
|
54
|
-
* @public
|
|
55
|
-
*/
|
|
56
|
-
class Namespace {
|
|
57
|
-
elementMap = {};
|
|
58
|
-
elementDetection = [];
|
|
59
|
-
Element;
|
|
60
|
-
KeyValuePair;
|
|
61
|
-
_elements;
|
|
62
|
-
_attributeElementKeys = [];
|
|
63
|
-
_attributeElementArrayKeys = [];
|
|
64
|
-
constructor(options) {
|
|
65
|
-
this.Element = elements.Element;
|
|
66
|
-
this.KeyValuePair = elements.KeyValuePair;
|
|
67
|
-
if (!options || !options.noDefault) {
|
|
68
|
-
this.useDefault();
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Use a namespace plugin or load a generic plugin.
|
|
74
|
-
*/
|
|
75
|
-
use(plugin) {
|
|
76
|
-
if (plugin.namespace) {
|
|
77
|
-
plugin.namespace({
|
|
78
|
-
base: this
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
if (plugin.load) {
|
|
82
|
-
plugin.load({
|
|
83
|
-
base: this
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
return this;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Use the default namespace. This preloads all the default elements
|
|
91
|
-
* into this registry instance.
|
|
92
|
-
*/
|
|
93
|
-
useDefault() {
|
|
94
|
-
// Set up classes for default elements
|
|
95
|
-
this.register('null', elements.NullElement).register('string', elements.StringElement).register('number', elements.NumberElement).register('boolean', elements.BooleanElement).register('array', elements.ArrayElement).register('object', elements.ObjectElement).register('member', elements.MemberElement).register('ref', elements.RefElement).register('link', elements.LinkElement).register('sourceMap', elements.SourceMapElement);
|
|
96
|
-
|
|
97
|
-
// Add instance detection functions to convert existing objects into
|
|
98
|
-
// the corresponding refract elements.
|
|
99
|
-
this.detect(isNull, elements.NullElement, false).detect(isString, elements.StringElement, false).detect(isNumber, elements.NumberElement, false).detect(isBoolean, elements.BooleanElement, false).detect(Array.isArray, elements.ArrayElement, false).detect(isObject, elements.ObjectElement, false);
|
|
100
|
-
return this;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Register a new element class for an element.
|
|
105
|
-
*/
|
|
106
|
-
register(name, ElementClass) {
|
|
107
|
-
this._elements = undefined;
|
|
108
|
-
this.elementMap[name] = ElementClass;
|
|
109
|
-
return this;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Unregister a previously registered class for an element.
|
|
114
|
-
*/
|
|
115
|
-
unregister(name) {
|
|
116
|
-
this._elements = undefined;
|
|
117
|
-
delete this.elementMap[name];
|
|
118
|
-
return this;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Add a new detection function to determine which element
|
|
123
|
-
* class to use when converting existing JS instances into
|
|
124
|
-
* refract elements.
|
|
125
|
-
*/
|
|
126
|
-
detect(test, ElementClass, givenPrepend) {
|
|
127
|
-
const prepend = givenPrepend === undefined ? true : givenPrepend;
|
|
128
|
-
if (prepend) {
|
|
129
|
-
this.elementDetection.unshift([test, ElementClass]);
|
|
130
|
-
} else {
|
|
131
|
-
this.elementDetection.push([test, ElementClass]);
|
|
132
|
-
}
|
|
133
|
-
return this;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Convert an existing JavaScript object into refract element instances.
|
|
138
|
-
* If the item passed in is already refracted, then it is returned unmodified.
|
|
139
|
-
*/
|
|
140
|
-
toElement(value) {
|
|
141
|
-
if (value instanceof this.Element) {
|
|
142
|
-
return value;
|
|
143
|
-
}
|
|
144
|
-
let element;
|
|
145
|
-
for (const [test, ElementClass] of this.elementDetection) {
|
|
146
|
-
if (test(value)) {
|
|
147
|
-
element = new ElementClass(value);
|
|
148
|
-
break;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
return element;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Get an element class given an element name.
|
|
156
|
-
*/
|
|
157
|
-
getElementClass(element) {
|
|
158
|
-
const ElementClass = this.elementMap[element];
|
|
159
|
-
if (ElementClass === undefined) {
|
|
160
|
-
// Fall back to the base element. We may not know what
|
|
161
|
-
// to do with the `content`, but downstream software
|
|
162
|
-
// may know.
|
|
163
|
-
return this.Element;
|
|
164
|
-
}
|
|
165
|
-
return ElementClass;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Convert a refract document into refract element instances.
|
|
170
|
-
*/
|
|
171
|
-
fromRefract(doc) {
|
|
172
|
-
return this.serialiser.deserialise(doc);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Convert an element to a Refracted JSON object.
|
|
177
|
-
*/
|
|
178
|
-
toRefract(element) {
|
|
179
|
-
return this.serialiser.serialise(element);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Get an object that contains all registered element classes, where
|
|
184
|
-
* the key is the PascalCased element name and the value is the class.
|
|
185
|
-
*/
|
|
186
|
-
get elements() {
|
|
187
|
-
if (this._elements === undefined) {
|
|
188
|
-
this._elements = {
|
|
189
|
-
Element: this.Element
|
|
190
|
-
};
|
|
191
|
-
Object.keys(this.elementMap).forEach(name => {
|
|
192
|
-
// Currently, all registered element types use a camelCaseName.
|
|
193
|
-
// Converting to PascalCase is as simple as upper-casing the first letter.
|
|
194
|
-
const pascal = name[0].toUpperCase() + name.substring(1);
|
|
195
|
-
this._elements[pascal] = this.elementMap[name];
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
return this._elements;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Convenience method for getting a JSON Serialiser configured with the
|
|
203
|
-
* current namespace.
|
|
204
|
-
*/
|
|
205
|
-
get serialiser() {
|
|
206
|
-
return new _JSONSerialiser.default(this);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// Set up the circular reference for JSONSerialiser
|
|
211
|
-
_JSONSerialiser.default.prototype.Namespace = Namespace;
|
|
212
|
-
var _default = exports.default = Namespace;
|
package/src/Namespace.mjs
DELETED
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
import JSONSerialiser from "./serialisers/JSONSerialiser.mjs";
|
|
2
|
-
import * as elements from "./registration.mjs";
|
|
3
|
-
const isNull = value => value === null;
|
|
4
|
-
const isString = value => typeof value === 'string';
|
|
5
|
-
const isNumber = value => typeof value === 'number';
|
|
6
|
-
const isBoolean = value => typeof value === 'boolean';
|
|
7
|
-
const isObject = value => value !== null && typeof value === 'object';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Constructor type for Element classes.
|
|
11
|
-
* @public
|
|
12
|
-
*/
|
|
13
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Function to test if a value should be converted to a specific element type.
|
|
17
|
-
* @public
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Tuple of detection test and element class.
|
|
22
|
-
* @public
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Options for Namespace constructor.
|
|
27
|
-
* @public
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Plugin interface for extending Namespace.
|
|
32
|
-
* @public
|
|
33
|
-
*/
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Map of registered element classes.
|
|
37
|
-
* @public
|
|
38
|
-
*/
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* A refract element implementation with an extensible namespace, able to
|
|
42
|
-
* load other namespaces into it.
|
|
43
|
-
*
|
|
44
|
-
* The namespace allows you to register your own classes to be instantiated
|
|
45
|
-
* when a particular refract element is encountered, and allows you to specify
|
|
46
|
-
* which elements get instantiated for existing JavaScript objects.
|
|
47
|
-
*
|
|
48
|
-
* @public
|
|
49
|
-
*/
|
|
50
|
-
class Namespace {
|
|
51
|
-
elementMap = {};
|
|
52
|
-
elementDetection = [];
|
|
53
|
-
Element;
|
|
54
|
-
KeyValuePair;
|
|
55
|
-
_elements;
|
|
56
|
-
_attributeElementKeys = [];
|
|
57
|
-
_attributeElementArrayKeys = [];
|
|
58
|
-
constructor(options) {
|
|
59
|
-
this.Element = elements.Element;
|
|
60
|
-
this.KeyValuePair = elements.KeyValuePair;
|
|
61
|
-
if (!options || !options.noDefault) {
|
|
62
|
-
this.useDefault();
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Use a namespace plugin or load a generic plugin.
|
|
68
|
-
*/
|
|
69
|
-
use(plugin) {
|
|
70
|
-
if (plugin.namespace) {
|
|
71
|
-
plugin.namespace({
|
|
72
|
-
base: this
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
if (plugin.load) {
|
|
76
|
-
plugin.load({
|
|
77
|
-
base: this
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
return this;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Use the default namespace. This preloads all the default elements
|
|
85
|
-
* into this registry instance.
|
|
86
|
-
*/
|
|
87
|
-
useDefault() {
|
|
88
|
-
// Set up classes for default elements
|
|
89
|
-
this.register('null', elements.NullElement).register('string', elements.StringElement).register('number', elements.NumberElement).register('boolean', elements.BooleanElement).register('array', elements.ArrayElement).register('object', elements.ObjectElement).register('member', elements.MemberElement).register('ref', elements.RefElement).register('link', elements.LinkElement).register('sourceMap', elements.SourceMapElement);
|
|
90
|
-
|
|
91
|
-
// Add instance detection functions to convert existing objects into
|
|
92
|
-
// the corresponding refract elements.
|
|
93
|
-
this.detect(isNull, elements.NullElement, false).detect(isString, elements.StringElement, false).detect(isNumber, elements.NumberElement, false).detect(isBoolean, elements.BooleanElement, false).detect(Array.isArray, elements.ArrayElement, false).detect(isObject, elements.ObjectElement, false);
|
|
94
|
-
return this;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Register a new element class for an element.
|
|
99
|
-
*/
|
|
100
|
-
register(name, ElementClass) {
|
|
101
|
-
this._elements = undefined;
|
|
102
|
-
this.elementMap[name] = ElementClass;
|
|
103
|
-
return this;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Unregister a previously registered class for an element.
|
|
108
|
-
*/
|
|
109
|
-
unregister(name) {
|
|
110
|
-
this._elements = undefined;
|
|
111
|
-
delete this.elementMap[name];
|
|
112
|
-
return this;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Add a new detection function to determine which element
|
|
117
|
-
* class to use when converting existing JS instances into
|
|
118
|
-
* refract elements.
|
|
119
|
-
*/
|
|
120
|
-
detect(test, ElementClass, givenPrepend) {
|
|
121
|
-
const prepend = givenPrepend === undefined ? true : givenPrepend;
|
|
122
|
-
if (prepend) {
|
|
123
|
-
this.elementDetection.unshift([test, ElementClass]);
|
|
124
|
-
} else {
|
|
125
|
-
this.elementDetection.push([test, ElementClass]);
|
|
126
|
-
}
|
|
127
|
-
return this;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Convert an existing JavaScript object into refract element instances.
|
|
132
|
-
* If the item passed in is already refracted, then it is returned unmodified.
|
|
133
|
-
*/
|
|
134
|
-
toElement(value) {
|
|
135
|
-
if (value instanceof this.Element) {
|
|
136
|
-
return value;
|
|
137
|
-
}
|
|
138
|
-
let element;
|
|
139
|
-
for (const [test, ElementClass] of this.elementDetection) {
|
|
140
|
-
if (test(value)) {
|
|
141
|
-
element = new ElementClass(value);
|
|
142
|
-
break;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return element;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Get an element class given an element name.
|
|
150
|
-
*/
|
|
151
|
-
getElementClass(element) {
|
|
152
|
-
const ElementClass = this.elementMap[element];
|
|
153
|
-
if (ElementClass === undefined) {
|
|
154
|
-
// Fall back to the base element. We may not know what
|
|
155
|
-
// to do with the `content`, but downstream software
|
|
156
|
-
// may know.
|
|
157
|
-
return this.Element;
|
|
158
|
-
}
|
|
159
|
-
return ElementClass;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Convert a refract document into refract element instances.
|
|
164
|
-
*/
|
|
165
|
-
fromRefract(doc) {
|
|
166
|
-
return this.serialiser.deserialise(doc);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Convert an element to a Refracted JSON object.
|
|
171
|
-
*/
|
|
172
|
-
toRefract(element) {
|
|
173
|
-
return this.serialiser.serialise(element);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Get an object that contains all registered element classes, where
|
|
178
|
-
* the key is the PascalCased element name and the value is the class.
|
|
179
|
-
*/
|
|
180
|
-
get elements() {
|
|
181
|
-
if (this._elements === undefined) {
|
|
182
|
-
this._elements = {
|
|
183
|
-
Element: this.Element
|
|
184
|
-
};
|
|
185
|
-
Object.keys(this.elementMap).forEach(name => {
|
|
186
|
-
// Currently, all registered element types use a camelCaseName.
|
|
187
|
-
// Converting to PascalCase is as simple as upper-casing the first letter.
|
|
188
|
-
const pascal = name[0].toUpperCase() + name.substring(1);
|
|
189
|
-
this._elements[pascal] = this.elementMap[name];
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
return this._elements;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Convenience method for getting a JSON Serialiser configured with the
|
|
197
|
-
* current namespace.
|
|
198
|
-
*/
|
|
199
|
-
get serialiser() {
|
|
200
|
-
return new JSONSerialiser(this);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// Set up the circular reference for JSONSerialiser
|
|
205
|
-
JSONSerialiser.prototype.Namespace = Namespace;
|
|
206
|
-
export default Namespace;
|