@speclynx/apidom-traverse 4.0.2 → 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 +6 -0
- package/package.json +7 -8
- package/src/Path.cjs +342 -0
- package/src/Path.mjs +338 -0
- package/src/Path.ts +368 -0
- package/src/index.cjs +43 -0
- package/src/index.mjs +19 -0
- package/src/index.ts +44 -0
- package/src/operations/filter.cjs +21 -0
- package/src/operations/filter.mjs +17 -0
- package/src/operations/filter.ts +27 -0
- package/src/operations/find-at-offset.cjs +45 -0
- package/src/operations/find-at-offset.mjs +40 -0
- package/src/operations/find-at-offset.ts +61 -0
- package/src/operations/find.cjs +22 -0
- package/src/operations/find.mjs +18 -0
- package/src/operations/find.ts +28 -0
- package/src/operations/for-each.cjs +36 -0
- package/src/operations/for-each.mjs +30 -0
- package/src/operations/for-each.ts +44 -0
- package/src/operations/parents.cjs +21 -0
- package/src/operations/parents.mjs +17 -0
- package/src/operations/parents.ts +24 -0
- package/src/operations/reject.cjs +14 -0
- package/src/operations/reject.mjs +9 -0
- package/src/operations/reject.ts +17 -0
- package/src/operations/some.cjs +14 -0
- package/src/operations/some.mjs +9 -0
- package/src/operations/some.ts +17 -0
- package/src/traversal.cjs +313 -0
- package/src/traversal.mjs +305 -0
- package/src/traversal.ts +391 -0
- package/src/visitors.cjs +420 -0
- package/src/visitors.mjs +407 -0
- package/src/visitors.ts +490 -0
package/src/index.cjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
|
4
|
+
exports.__esModule = true;
|
|
5
|
+
exports.some = exports.reject = exports.parents = exports.mutateNode = exports.mergeVisitorsAsync = exports.mergeVisitors = exports.isNode = exports.getVisitFn = exports.getNodeType = exports.getNodePrimitiveType = exports.getNodeKeys = exports.forEach = exports.findAtOffset = exports.find = exports.filter = exports.cloneNode = void 0;
|
|
6
|
+
var _Path = require("./Path.cjs");
|
|
7
|
+
exports.Path = _Path.Path;
|
|
8
|
+
var _traversal = require("./traversal.cjs");
|
|
9
|
+
exports.traverse = _traversal.traverse;
|
|
10
|
+
exports.traverseAsync = _traversal.traverseAsync;
|
|
11
|
+
var _visitors = require("./visitors.cjs");
|
|
12
|
+
exports.getNodeType = _visitors.getNodeType;
|
|
13
|
+
exports.getNodePrimitiveType = _visitors.getNodePrimitiveType;
|
|
14
|
+
exports.isNode = _visitors.isNode;
|
|
15
|
+
exports.cloneNode = _visitors.cloneNode;
|
|
16
|
+
exports.mutateNode = _visitors.mutateNode;
|
|
17
|
+
exports.getNodeKeys = _visitors.getNodeKeys;
|
|
18
|
+
exports.getVisitFn = _visitors.getVisitFn;
|
|
19
|
+
exports.mergeVisitors = _visitors.mergeVisitors;
|
|
20
|
+
exports.mergeVisitorsAsync = _visitors.mergeVisitorsAsync;
|
|
21
|
+
var _filter = _interopRequireDefault(require("./operations/filter.cjs"));
|
|
22
|
+
exports.filter = _filter.default;
|
|
23
|
+
var _find = _interopRequireDefault(require("./operations/find.cjs"));
|
|
24
|
+
exports.find = _find.default;
|
|
25
|
+
var _some = _interopRequireDefault(require("./operations/some.cjs"));
|
|
26
|
+
exports.some = _some.default;
|
|
27
|
+
var _reject = _interopRequireDefault(require("./operations/reject.cjs"));
|
|
28
|
+
exports.reject = _reject.default;
|
|
29
|
+
var _forEach = _interopRequireDefault(require("./operations/for-each.cjs"));
|
|
30
|
+
exports.forEach = _forEach.default;
|
|
31
|
+
var _parents = _interopRequireDefault(require("./operations/parents.cjs"));
|
|
32
|
+
exports.parents = _parents.default;
|
|
33
|
+
var _findAtOffset = _interopRequireDefault(require("./operations/find-at-offset.cjs"));
|
|
34
|
+
exports.findAtOffset = _findAtOffset.default;
|
|
35
|
+
// Wire up Path.prototype.traverse methods to avoid circular imports
|
|
36
|
+
_Path.Path.prototype.traverse = function (visitor, options) {
|
|
37
|
+
return (0, _traversal.traverse)(this.node, visitor, options);
|
|
38
|
+
};
|
|
39
|
+
_Path.Path.prototype.traverseAsync = function (visitor, options) {
|
|
40
|
+
return (0, _traversal.traverseAsync)(this.node, visitor, options);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Operations
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Path } from "./Path.mjs";
|
|
2
|
+
import { traverse, traverseAsync } from "./traversal.mjs"; // Wire up Path.prototype.traverse methods to avoid circular imports
|
|
3
|
+
Path.prototype.traverse = function (visitor, options) {
|
|
4
|
+
return traverse(this.node, visitor, options);
|
|
5
|
+
};
|
|
6
|
+
Path.prototype.traverseAsync = function (visitor, options) {
|
|
7
|
+
return traverseAsync(this.node, visitor, options);
|
|
8
|
+
};
|
|
9
|
+
export { Path };
|
|
10
|
+
export { getNodeType, getNodePrimitiveType, isNode, cloneNode, mutateNode, getNodeKeys, getVisitFn, mergeVisitors, mergeVisitorsAsync } from "./visitors.mjs";
|
|
11
|
+
export { traverse, traverseAsync } from "./traversal.mjs";
|
|
12
|
+
// Operations
|
|
13
|
+
export { default as filter } from "./operations/filter.mjs";
|
|
14
|
+
export { default as find } from "./operations/find.mjs";
|
|
15
|
+
export { default as some } from "./operations/some.mjs";
|
|
16
|
+
export { default as reject } from "./operations/reject.mjs";
|
|
17
|
+
export { default as forEach } from "./operations/for-each.mjs";
|
|
18
|
+
export { default as parents } from "./operations/parents.mjs";
|
|
19
|
+
export { default as findAtOffset } from "./operations/find-at-offset.mjs";
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Path } from './Path.ts';
|
|
2
|
+
import { traverse, traverseAsync } from './traversal.ts';
|
|
3
|
+
|
|
4
|
+
// Wire up Path.prototype.traverse methods to avoid circular imports
|
|
5
|
+
Path.prototype.traverse = function (this: Path<any>, visitor: any, options?: any) {
|
|
6
|
+
return traverse(this.node, visitor, options);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
Path.prototype.traverseAsync = function (this: Path<any>, visitor: any, options?: any) {
|
|
10
|
+
return traverseAsync(this.node, visitor, options);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { Path };
|
|
14
|
+
export type { VisitorFn, VisitorResult } from './Path.ts';
|
|
15
|
+
export {
|
|
16
|
+
getNodeType,
|
|
17
|
+
getNodePrimitiveType,
|
|
18
|
+
isNode,
|
|
19
|
+
cloneNode,
|
|
20
|
+
mutateNode,
|
|
21
|
+
getNodeKeys,
|
|
22
|
+
getVisitFn,
|
|
23
|
+
mergeVisitors,
|
|
24
|
+
mergeVisitorsAsync,
|
|
25
|
+
} from './visitors.ts';
|
|
26
|
+
export type {
|
|
27
|
+
NodeVisitor,
|
|
28
|
+
MergeVisitorsOptions,
|
|
29
|
+
MergedVisitor,
|
|
30
|
+
MergedVisitorAsync,
|
|
31
|
+
} from './visitors.ts';
|
|
32
|
+
export { traverse, traverseAsync } from './traversal.ts';
|
|
33
|
+
export type { TraverseOptions } from './traversal.ts';
|
|
34
|
+
|
|
35
|
+
// Operations
|
|
36
|
+
export { default as filter } from './operations/filter.ts';
|
|
37
|
+
export { default as find } from './operations/find.ts';
|
|
38
|
+
export { default as some } from './operations/some.ts';
|
|
39
|
+
export { default as reject } from './operations/reject.ts';
|
|
40
|
+
export { default as forEach } from './operations/for-each.ts';
|
|
41
|
+
export type { Callback as ForEachCallback, ForEachOptions } from './operations/for-each.ts';
|
|
42
|
+
export { default as parents } from './operations/parents.ts';
|
|
43
|
+
export { default as findAtOffset } from './operations/find-at-offset.ts';
|
|
44
|
+
export type { FindAtOffsetOptions } from './operations/find-at-offset.ts';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _traversal = require("../traversal.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Finds all paths whose elements match the predicate.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
const filter = (element, predicate) => {
|
|
11
|
+
const result = [];
|
|
12
|
+
(0, _traversal.traverse)(element, {
|
|
13
|
+
enter(path) {
|
|
14
|
+
if (predicate(path)) {
|
|
15
|
+
result.push(path);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var _default = exports.default = filter;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { traverse } from "../traversal.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Finds all paths whose elements match the predicate.
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
const filter = (element, predicate) => {
|
|
7
|
+
const result = [];
|
|
8
|
+
traverse(element, {
|
|
9
|
+
enter(path) {
|
|
10
|
+
if (predicate(path)) {
|
|
11
|
+
result.push(path);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
return result;
|
|
16
|
+
};
|
|
17
|
+
export default filter;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Element } from '@speclynx/apidom-datamodel';
|
|
2
|
+
|
|
3
|
+
import { traverse } from '../traversal.ts';
|
|
4
|
+
import type { Path } from '../Path.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Finds all paths whose elements match the predicate.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
const filter = <T extends Element>(
|
|
11
|
+
element: T,
|
|
12
|
+
predicate: (path: Path<Element>) => boolean,
|
|
13
|
+
): Path<Element>[] => {
|
|
14
|
+
const result: Path<Element>[] = [];
|
|
15
|
+
|
|
16
|
+
traverse(element, {
|
|
17
|
+
enter(path: Path<Element>) {
|
|
18
|
+
if (predicate(path)) {
|
|
19
|
+
result.push(path);
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export default filter;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _apidomDatamodel = require("@speclynx/apidom-datamodel");
|
|
6
|
+
var _traversal = require("../traversal.cjs");
|
|
7
|
+
/**
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Finds the path of the most inner node at the given offset.
|
|
13
|
+
* If includeRightBound is set, also finds nodes that end at the given offset.
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
const findAtOffset = (element, options) => {
|
|
17
|
+
let offset;
|
|
18
|
+
let includeRightBound;
|
|
19
|
+
if (typeof options === 'number') {
|
|
20
|
+
offset = options;
|
|
21
|
+
includeRightBound = false;
|
|
22
|
+
} else {
|
|
23
|
+
offset = options.offset ?? 0;
|
|
24
|
+
includeRightBound = options.includeRightBound ?? false;
|
|
25
|
+
}
|
|
26
|
+
const result = [];
|
|
27
|
+
(0, _traversal.traverse)(element, {
|
|
28
|
+
enter(path) {
|
|
29
|
+
const node = path.node;
|
|
30
|
+
if (!(0, _apidomDatamodel.hasElementSourceMap)(node)) {
|
|
31
|
+
return; // dive in
|
|
32
|
+
}
|
|
33
|
+
const startOffset = node.startOffset;
|
|
34
|
+
const endOffset = node.endOffset;
|
|
35
|
+
const isWithinOffsetRange = offset >= startOffset && (offset < endOffset || includeRightBound && offset <= endOffset);
|
|
36
|
+
if (isWithinOffsetRange) {
|
|
37
|
+
result.push(path);
|
|
38
|
+
return; // push to stack and dive in
|
|
39
|
+
}
|
|
40
|
+
path.skip(); // skip entire sub-tree
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return result.at(-1);
|
|
44
|
+
};
|
|
45
|
+
var _default = exports.default = findAtOffset;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { hasElementSourceMap } from '@speclynx/apidom-datamodel';
|
|
2
|
+
import { traverse } from "../traversal.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Finds the path of the most inner node at the given offset.
|
|
8
|
+
* If includeRightBound is set, also finds nodes that end at the given offset.
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
const findAtOffset = (element, options) => {
|
|
12
|
+
let offset;
|
|
13
|
+
let includeRightBound;
|
|
14
|
+
if (typeof options === 'number') {
|
|
15
|
+
offset = options;
|
|
16
|
+
includeRightBound = false;
|
|
17
|
+
} else {
|
|
18
|
+
offset = options.offset ?? 0;
|
|
19
|
+
includeRightBound = options.includeRightBound ?? false;
|
|
20
|
+
}
|
|
21
|
+
const result = [];
|
|
22
|
+
traverse(element, {
|
|
23
|
+
enter(path) {
|
|
24
|
+
const node = path.node;
|
|
25
|
+
if (!hasElementSourceMap(node)) {
|
|
26
|
+
return; // dive in
|
|
27
|
+
}
|
|
28
|
+
const startOffset = node.startOffset;
|
|
29
|
+
const endOffset = node.endOffset;
|
|
30
|
+
const isWithinOffsetRange = offset >= startOffset && (offset < endOffset || includeRightBound && offset <= endOffset);
|
|
31
|
+
if (isWithinOffsetRange) {
|
|
32
|
+
result.push(path);
|
|
33
|
+
return; // push to stack and dive in
|
|
34
|
+
}
|
|
35
|
+
path.skip(); // skip entire sub-tree
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return result.at(-1);
|
|
39
|
+
};
|
|
40
|
+
export default findAtOffset;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Element, hasElementSourceMap } from '@speclynx/apidom-datamodel';
|
|
2
|
+
|
|
3
|
+
import { traverse } from '../traversal.ts';
|
|
4
|
+
import type { Path } from '../Path.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export interface FindAtOffsetOptions {
|
|
10
|
+
offset: number;
|
|
11
|
+
includeRightBound?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Finds the path of the most inner node at the given offset.
|
|
16
|
+
* If includeRightBound is set, also finds nodes that end at the given offset.
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
const findAtOffset = <T extends Element>(
|
|
20
|
+
element: T,
|
|
21
|
+
options: number | FindAtOffsetOptions,
|
|
22
|
+
): Path<Element> | undefined => {
|
|
23
|
+
let offset: number;
|
|
24
|
+
let includeRightBound: boolean;
|
|
25
|
+
|
|
26
|
+
if (typeof options === 'number') {
|
|
27
|
+
offset = options;
|
|
28
|
+
includeRightBound = false;
|
|
29
|
+
} else {
|
|
30
|
+
offset = options.offset ?? 0;
|
|
31
|
+
includeRightBound = options.includeRightBound ?? false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const result: Path<Element>[] = [];
|
|
35
|
+
|
|
36
|
+
traverse(element, {
|
|
37
|
+
enter(path: Path<Element>) {
|
|
38
|
+
const node = path.node;
|
|
39
|
+
|
|
40
|
+
if (!hasElementSourceMap(node)) {
|
|
41
|
+
return; // dive in
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const startOffset: number = node.startOffset!;
|
|
45
|
+
const endOffset: number = node.endOffset!;
|
|
46
|
+
const isWithinOffsetRange =
|
|
47
|
+
offset >= startOffset && (offset < endOffset || (includeRightBound && offset <= endOffset));
|
|
48
|
+
|
|
49
|
+
if (isWithinOffsetRange) {
|
|
50
|
+
result.push(path);
|
|
51
|
+
return; // push to stack and dive in
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
path.skip(); // skip entire sub-tree
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
return result.at(-1);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export default findAtOffset;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _traversal = require("../traversal.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Finds first path whose element satisfies the provided predicate.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
const find = (element, predicate) => {
|
|
11
|
+
let result;
|
|
12
|
+
(0, _traversal.traverse)(element, {
|
|
13
|
+
enter(path) {
|
|
14
|
+
if (predicate(path)) {
|
|
15
|
+
result = path;
|
|
16
|
+
path.stop();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
return result;
|
|
21
|
+
};
|
|
22
|
+
var _default = exports.default = find;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { traverse } from "../traversal.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Finds first path whose element satisfies the provided predicate.
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
const find = (element, predicate) => {
|
|
7
|
+
let result;
|
|
8
|
+
traverse(element, {
|
|
9
|
+
enter(path) {
|
|
10
|
+
if (predicate(path)) {
|
|
11
|
+
result = path;
|
|
12
|
+
path.stop();
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
return result;
|
|
17
|
+
};
|
|
18
|
+
export default find;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Element } from '@speclynx/apidom-datamodel';
|
|
2
|
+
|
|
3
|
+
import { traverse } from '../traversal.ts';
|
|
4
|
+
import type { Path } from '../Path.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Finds first path whose element satisfies the provided predicate.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
const find = <T extends Element>(
|
|
11
|
+
element: T,
|
|
12
|
+
predicate: (path: Path<Element>) => boolean,
|
|
13
|
+
): Path<Element> | undefined => {
|
|
14
|
+
let result: Path<Element> | undefined;
|
|
15
|
+
|
|
16
|
+
traverse(element, {
|
|
17
|
+
enter(path: Path<Element>) {
|
|
18
|
+
if (predicate(path)) {
|
|
19
|
+
result = path;
|
|
20
|
+
path.stop();
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export default find;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _traversal = require("../traversal.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Executes the callback on this element's path and all descendant paths.
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
const forEach = (element, options) => {
|
|
19
|
+
let callback;
|
|
20
|
+
let predicate;
|
|
21
|
+
if (typeof options === 'function') {
|
|
22
|
+
callback = options;
|
|
23
|
+
predicate = () => true;
|
|
24
|
+
} else {
|
|
25
|
+
callback = options.callback ?? (() => {});
|
|
26
|
+
predicate = options.predicate ?? (() => true);
|
|
27
|
+
}
|
|
28
|
+
(0, _traversal.traverse)(element, {
|
|
29
|
+
enter(path) {
|
|
30
|
+
if (predicate(path)) {
|
|
31
|
+
callback(path);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
var _default = exports.default = forEach;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { traverse } from "../traversal.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* @public
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Executes the callback on this element's path and all descendant paths.
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
const forEach = (element, options) => {
|
|
13
|
+
let callback;
|
|
14
|
+
let predicate;
|
|
15
|
+
if (typeof options === 'function') {
|
|
16
|
+
callback = options;
|
|
17
|
+
predicate = () => true;
|
|
18
|
+
} else {
|
|
19
|
+
callback = options.callback ?? (() => {});
|
|
20
|
+
predicate = options.predicate ?? (() => true);
|
|
21
|
+
}
|
|
22
|
+
traverse(element, {
|
|
23
|
+
enter(path) {
|
|
24
|
+
if (predicate(path)) {
|
|
25
|
+
callback(path);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
export default forEach;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Element } from '@speclynx/apidom-datamodel';
|
|
2
|
+
|
|
3
|
+
import { traverse } from '../traversal.ts';
|
|
4
|
+
import type { Path } from '../Path.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export type Callback = (path: Path<Element>) => void;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
export interface ForEachOptions {
|
|
15
|
+
callback?: Callback;
|
|
16
|
+
predicate?: (path: Path<Element>) => boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Executes the callback on this element's path and all descendant paths.
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
const forEach = <T extends Element>(element: T, options: Callback | ForEachOptions): void => {
|
|
24
|
+
let callback: Callback;
|
|
25
|
+
let predicate: (path: Path<Element>) => boolean;
|
|
26
|
+
|
|
27
|
+
if (typeof options === 'function') {
|
|
28
|
+
callback = options;
|
|
29
|
+
predicate = () => true;
|
|
30
|
+
} else {
|
|
31
|
+
callback = options.callback ?? (() => {});
|
|
32
|
+
predicate = options.predicate ?? (() => true);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
traverse(element, {
|
|
36
|
+
enter(path: Path<Element>) {
|
|
37
|
+
if (predicate(path)) {
|
|
38
|
+
callback(path);
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export default forEach;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _traversal = require("../traversal.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Computes upwards edges from every child to its parent.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
const parents = element => {
|
|
11
|
+
const parentEdges = new WeakMap();
|
|
12
|
+
(0, _traversal.traverse)(element, {
|
|
13
|
+
enter(path) {
|
|
14
|
+
// Use parentPath.node to get the actual Element parent.
|
|
15
|
+
// path.parent could be an array (ArraySlice) when inside ArrayElement/ObjectElement content.
|
|
16
|
+
parentEdges.set(path.node, path.parentPath?.node);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
return parentEdges;
|
|
20
|
+
};
|
|
21
|
+
var _default = exports.default = parents;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { traverse } from "../traversal.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Computes upwards edges from every child to its parent.
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
const parents = element => {
|
|
7
|
+
const parentEdges = new WeakMap();
|
|
8
|
+
traverse(element, {
|
|
9
|
+
enter(path) {
|
|
10
|
+
// Use parentPath.node to get the actual Element parent.
|
|
11
|
+
// path.parent could be an array (ArraySlice) when inside ArrayElement/ObjectElement content.
|
|
12
|
+
parentEdges.set(path.node, path.parentPath?.node);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
return parentEdges;
|
|
16
|
+
};
|
|
17
|
+
export default parents;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Element } from '@speclynx/apidom-datamodel';
|
|
2
|
+
|
|
3
|
+
import { traverse } from '../traversal.ts';
|
|
4
|
+
import type { Path } from '../Path.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Computes upwards edges from every child to its parent.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
const parents = <T extends Element>(element: T): WeakMap<Element, Element | undefined> => {
|
|
11
|
+
const parentEdges = new WeakMap<Element, Element | undefined>();
|
|
12
|
+
|
|
13
|
+
traverse(element, {
|
|
14
|
+
enter(path: Path<Element>) {
|
|
15
|
+
// Use parentPath.node to get the actual Element parent.
|
|
16
|
+
// path.parent could be an array (ArraySlice) when inside ArrayElement/ObjectElement content.
|
|
17
|
+
parentEdges.set(path.node, path.parentPath?.node);
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
return parentEdges;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default parents;
|
|
@@ -0,0 +1,14 @@
|
|
|
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 _filter = _interopRequireDefault(require("./filter.cjs"));
|
|
7
|
+
/**
|
|
8
|
+
* Complement of filter. Finds all paths whose elements do NOT match the predicate.
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
const reject = (element, predicate) => {
|
|
12
|
+
return (0, _filter.default)(element, path => !predicate(path));
|
|
13
|
+
};
|
|
14
|
+
var _default = exports.default = reject;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import filter from "./filter.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Complement of filter. Finds all paths whose elements do NOT match the predicate.
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
const reject = (element, predicate) => {
|
|
7
|
+
return filter(element, path => !predicate(path));
|
|
8
|
+
};
|
|
9
|
+
export default reject;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Element } from '@speclynx/apidom-datamodel';
|
|
2
|
+
|
|
3
|
+
import filter from './filter.ts';
|
|
4
|
+
import type { Path } from '../Path.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Complement of filter. Finds all paths whose elements do NOT match the predicate.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
const reject = <T extends Element>(
|
|
11
|
+
element: T,
|
|
12
|
+
predicate: (path: Path<Element>) => boolean,
|
|
13
|
+
): Path<Element>[] => {
|
|
14
|
+
return filter(element, (path) => !predicate(path));
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export default reject;
|
|
@@ -0,0 +1,14 @@
|
|
|
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 _find = _interopRequireDefault(require("./find.cjs"));
|
|
7
|
+
/**
|
|
8
|
+
* Tests whether at least one path's element passes the predicate.
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
const some = (element, predicate) => {
|
|
12
|
+
return (0, _find.default)(element, predicate) !== undefined;
|
|
13
|
+
};
|
|
14
|
+
var _default = exports.default = some;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Element } from '@speclynx/apidom-datamodel';
|
|
2
|
+
|
|
3
|
+
import find from './find.ts';
|
|
4
|
+
import type { Path } from '../Path.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Tests whether at least one path's element passes the predicate.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
const some = <T extends Element>(
|
|
11
|
+
element: T,
|
|
12
|
+
predicate: (path: Path<Element>) => boolean,
|
|
13
|
+
): boolean => {
|
|
14
|
+
return find(element, predicate) !== undefined;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export default some;
|