@ldo/jsonld-dataset-proxy 0.0.1-alpha.29 → 1.0.0-alpha.1
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/README.md +86 -71
- package/dist/ContextUtil.d.ts +1 -1
- package/dist/ContextUtil.js +1 -1
- package/dist/ContextUtil.js.map +1 -1
- package/dist/JsonldDatasetProxyBuilder.d.ts +4 -3
- package/dist/JsonldDatasetProxyBuilder.js +2 -2
- package/dist/JsonldDatasetProxyBuilder.js.map +1 -1
- package/dist/ProxyContext.d.ts +7 -8
- package/dist/ProxyContext.js +14 -25
- package/dist/ProxyContext.js.map +1 -1
- package/dist/graphOf.d.ts +2 -1
- package/dist/graphOf.js +3 -15
- package/dist/graphOf.js.map +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.js +6 -5
- package/dist/index.js.map +1 -1
- package/dist/language/languagesOf.d.ts +2 -1
- package/dist/language/languagesOf.js +1 -1
- package/dist/language/languagesOf.js.map +1 -1
- package/dist/setProxy/ObjectSetProxy.d.ts +17 -0
- package/dist/setProxy/ObjectSetProxy.js +31 -0
- package/dist/setProxy/ObjectSetProxy.js.map +1 -0
- package/dist/setProxy/SetProxy.d.ts +29 -0
- package/dist/setProxy/SetProxy.js +72 -0
- package/dist/setProxy/SetProxy.js.map +1 -0
- package/dist/setProxy/SubjectSetProxy.d.ts +15 -0
- package/dist/setProxy/SubjectSetProxy.js +38 -0
- package/dist/setProxy/SubjectSetProxy.js.map +1 -0
- package/dist/setProxy/WildcardObjectSetProxy.d.ts +20 -0
- package/dist/setProxy/WildcardObjectSetProxy.js +49 -0
- package/dist/setProxy/WildcardObjectSetProxy.js.map +1 -0
- package/dist/setProxy/WildcardSubjectSetProxy.d.ts +19 -0
- package/dist/setProxy/WildcardSubjectSetProxy.js +31 -0
- package/dist/setProxy/WildcardSubjectSetProxy.js.map +1 -0
- package/dist/setProxy/createNewSetProxy.d.ts +5 -0
- package/dist/setProxy/createNewSetProxy.js +27 -0
- package/dist/setProxy/createNewSetProxy.js.map +1 -0
- package/dist/setProxy/isSetProxy.d.ts +2 -0
- package/dist/setProxy/isSetProxy.js +13 -0
- package/dist/setProxy/isSetProxy.js.map +1 -0
- package/dist/setProxy/ldSet/BasicLdSet.d.ts +36 -0
- package/dist/setProxy/ldSet/BasicLdSet.js +193 -0
- package/dist/setProxy/ldSet/BasicLdSet.js.map +1 -0
- package/dist/setProxy/ldSet/LdSet.d.ts +30 -0
- package/dist/setProxy/ldSet/LdSet.js +3 -0
- package/dist/setProxy/ldSet/LdSet.js.map +1 -0
- package/dist/setProxy/set.d.ts +2 -0
- package/dist/setProxy/set.js +9 -0
- package/dist/setProxy/set.js.map +1 -0
- package/dist/subjectProxy/SubjectProxy.d.ts +1 -1
- package/dist/subjectProxy/createSubjectHandler.js +6 -4
- package/dist/subjectProxy/createSubjectHandler.js.map +1 -1
- package/dist/subjectProxy/deleteFromDataset.js +6 -25
- package/dist/subjectProxy/deleteFromDataset.js.map +1 -1
- package/dist/subjectProxy/getValueForKey.d.ts +2 -2
- package/dist/subjectProxy/getValueForKey.js +3 -6
- package/dist/subjectProxy/getValueForKey.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/types.js +2 -3
- package/dist/types.js.map +1 -1
- package/dist/util/NodeSet.d.ts +0 -2
- package/dist/util/NodeSet.js +0 -10
- package/dist/util/NodeSet.js.map +1 -1
- package/dist/util/RawObject.d.ts +3 -2
- package/dist/util/addObjectToDataset.js +3 -5
- package/dist/util/addObjectToDataset.js.map +1 -1
- package/dist/util/getNodeFromRaw.d.ts +1 -1
- package/dist/util/getNodeFromRaw.js +9 -3
- package/dist/util/getNodeFromRaw.js.map +1 -1
- package/dist/util/isProxy.d.ts +4 -3
- package/dist/util/isProxy.js +2 -2
- package/dist/util/isProxy.js.map +1 -1
- package/dist/util/nodeToJsonldRepresentation.d.ts +3 -2
- package/dist/util/nodeToJsonldRepresentation.js.map +1 -1
- package/package.json +5 -5
- package/src/ContextUtil.ts +1 -1
- package/src/JsonldDatasetProxyBuilder.ts +14 -13
- package/src/ProxyContext.ts +35 -38
- package/src/graphOf.ts +12 -26
- package/src/index.ts +6 -5
- package/src/language/languagesOf.ts +3 -2
- package/src/setProxy/ObjectSetProxy.ts +66 -0
- package/src/setProxy/SetProxy.ts +141 -0
- package/src/setProxy/SubjectSetProxy.ts +85 -0
- package/src/setProxy/WildcardObjectSetProxy.ts +89 -0
- package/src/setProxy/WildcardSubjectSetProxy.ts +58 -0
- package/src/setProxy/createNewSetProxy.ts +47 -0
- package/src/setProxy/isSetProxy.ts +7 -0
- package/src/setProxy/ldSet/BasicLdSet.ts +266 -0
- package/src/setProxy/ldSet/LdSet.ts +189 -0
- package/src/setProxy/set.ts +14 -0
- package/src/subjectProxy/SubjectProxy.ts +1 -1
- package/src/subjectProxy/createSubjectHandler.ts +9 -5
- package/src/subjectProxy/deleteFromDataset.ts +12 -25
- package/src/subjectProxy/getValueForKey.ts +5 -8
- package/src/types.ts +2 -2
- package/src/util/NodeSet.ts +0 -12
- package/src/util/RawObject.ts +10 -2
- package/src/util/addObjectToDataset.ts +7 -5
- package/src/util/getNodeFromRaw.ts +11 -6
- package/src/util/isProxy.ts +7 -6
- package/src/util/nodeToJsonldRepresentation.ts +3 -2
- package/src/arrayProxy/ArrayProxy.ts +0 -20
- package/src/arrayProxy/arrayMethods.ts +0 -219
- package/src/arrayProxy/createArrayHandler.ts +0 -177
- package/src/arrayProxy/isArrayProxy.ts +0 -23
- package/src/arrayProxy/modifyArray.ts +0 -141
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type GraphNode,
|
|
3
|
+
type ObjectNode,
|
|
4
|
+
type PredicateNode,
|
|
5
|
+
} from "@ldo/rdf-utils";
|
|
6
|
+
import type { RawObject } from "../util/RawObject";
|
|
7
|
+
import { addObjectToDataset } from "../util/addObjectToDataset";
|
|
8
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
9
|
+
import { WildcardSubjectSetProxy } from "./WildcardSubjectSetProxy";
|
|
10
|
+
import { _getUnderlyingNode } from "../types";
|
|
11
|
+
import { defaultGraph, quad } from "@rdfjs/data-model";
|
|
12
|
+
import {
|
|
13
|
+
createTransactionDatasetFactory,
|
|
14
|
+
TransactionDataset,
|
|
15
|
+
} from "@ldo/subscribable-dataset";
|
|
16
|
+
import { createDatasetFactory } from "@ldo/dataset";
|
|
17
|
+
import { getNodeFromRawObject } from "../util/getNodeFromRaw";
|
|
18
|
+
import { nodeToString } from "../util/NodeSet";
|
|
19
|
+
|
|
20
|
+
export type SubjectSetProxyQuadMatch = [
|
|
21
|
+
undefined | null,
|
|
22
|
+
PredicateNode,
|
|
23
|
+
ObjectNode,
|
|
24
|
+
GraphNode | undefined | null,
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
export class SubjectSetProxy<
|
|
28
|
+
T extends RawObject,
|
|
29
|
+
> extends WildcardSubjectSetProxy<T> {
|
|
30
|
+
protected quadMatch: SubjectSetProxyQuadMatch;
|
|
31
|
+
|
|
32
|
+
constructor(context: ProxyContext, quadMatch: SubjectSetProxyQuadMatch) {
|
|
33
|
+
super(context, quadMatch);
|
|
34
|
+
this.quadMatch = quadMatch;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Appends a new element with a specified value to the end of the Set.
|
|
39
|
+
*/
|
|
40
|
+
add(value: T): this {
|
|
41
|
+
if (typeof value !== "object") {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Cannot add a literal "${value}"(${typeof value}) to a subject-oriented collection.`,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
// Create a test dataset to see if the inputted data is valid
|
|
47
|
+
const testDataset = new TransactionDataset(
|
|
48
|
+
this.context.dataset,
|
|
49
|
+
createDatasetFactory(),
|
|
50
|
+
createTransactionDatasetFactory(),
|
|
51
|
+
);
|
|
52
|
+
addObjectToDataset(
|
|
53
|
+
value,
|
|
54
|
+
false,
|
|
55
|
+
this.context.duplicate({
|
|
56
|
+
writeGraphs: [defaultGraph()],
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
const isValidAddition =
|
|
60
|
+
testDataset.match(
|
|
61
|
+
getNodeFromRawObject(value, this.context.contextUtil),
|
|
62
|
+
this.quadMatch[1],
|
|
63
|
+
this.quadMatch[2],
|
|
64
|
+
).size !== 0;
|
|
65
|
+
if (!isValidAddition) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`Cannot add value to collection. This must contain a quad that matches (${nodeToString(
|
|
68
|
+
this.quadMatch[0],
|
|
69
|
+
)}, ${nodeToString(this.quadMatch[1])}, ${nodeToString(
|
|
70
|
+
this.quadMatch[2],
|
|
71
|
+
)}, ${nodeToString(this.quadMatch[3])})`,
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Add the object if everything's okay
|
|
76
|
+
const added = addObjectToDataset(value as RawObject, false, this.context);
|
|
77
|
+
const addedNode = added[_getUnderlyingNode];
|
|
78
|
+
this.context.writeGraphs.forEach((graph) => {
|
|
79
|
+
this.context.dataset.add(
|
|
80
|
+
quad(addedNode, this.quadMatch[1], this.quadMatch[2], graph),
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SubjectNode,
|
|
3
|
+
PredicateNode,
|
|
4
|
+
ObjectNode,
|
|
5
|
+
GraphNode,
|
|
6
|
+
} from "@ldo/rdf-utils";
|
|
7
|
+
import type { Dataset, Quad } from "@rdfjs/types";
|
|
8
|
+
import type { RawValue } from "../util/RawObject";
|
|
9
|
+
import { SetProxy } from "./SetProxy";
|
|
10
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
11
|
+
import { getNodeFromRawValue } from "../util/getNodeFromRaw";
|
|
12
|
+
import { _isSubjectOriented } from "../types";
|
|
13
|
+
import { filterQuadsByLanguageOrdering } from "../language/languageUtils";
|
|
14
|
+
|
|
15
|
+
export type WildcardObjectSetProxyQuadMatch = [
|
|
16
|
+
SubjectNode | undefined | null,
|
|
17
|
+
PredicateNode | undefined | null,
|
|
18
|
+
undefined | null,
|
|
19
|
+
GraphNode | undefined | null,
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A WildcardObjectProxy represents a set of nodes in a dataset that are all the
|
|
24
|
+
* object of a given subject and predicate. Because this is a wildcard, the
|
|
25
|
+
* subject and predicate don't necissarily need to be defined.
|
|
26
|
+
*/
|
|
27
|
+
export class WildcardObjectSetProxy<
|
|
28
|
+
T extends NonNullable<RawValue>,
|
|
29
|
+
> extends SetProxy<T> {
|
|
30
|
+
protected quadMatch: WildcardObjectSetProxyQuadMatch;
|
|
31
|
+
protected isLangStringSet: boolean;
|
|
32
|
+
|
|
33
|
+
constructor(
|
|
34
|
+
context: ProxyContext,
|
|
35
|
+
quadMatch: WildcardObjectSetProxyQuadMatch,
|
|
36
|
+
isLangStringSet?: boolean,
|
|
37
|
+
) {
|
|
38
|
+
super(context, quadMatch);
|
|
39
|
+
this.quadMatch = quadMatch;
|
|
40
|
+
this.isLangStringSet = isLangStringSet ?? false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected getQuads(value?: T | undefined): Dataset<Quad, Quad> {
|
|
44
|
+
const { dataset } = this.context;
|
|
45
|
+
let quads: Dataset<Quad, Quad>;
|
|
46
|
+
// Get the RDF Node that represents the value, skip if no value
|
|
47
|
+
const subject = this.quadMatch[0] ?? undefined;
|
|
48
|
+
const predicate = this.quadMatch[1] ?? undefined;
|
|
49
|
+
const graph = this.quadMatch[3] ?? undefined;
|
|
50
|
+
if (value) {
|
|
51
|
+
// Get datatype if applicable
|
|
52
|
+
let datatype: string | undefined = undefined;
|
|
53
|
+
if (this.quadMatch[0] && predicate) {
|
|
54
|
+
const rdfType = this.context.getRdfType(this.quadMatch[0]);
|
|
55
|
+
const key = this.context.contextUtil.iriToKey(predicate.value, rdfType);
|
|
56
|
+
datatype = this.context.contextUtil.getDataType(key, rdfType);
|
|
57
|
+
}
|
|
58
|
+
const valueNode = getNodeFromRawValue(value, this.context, datatype);
|
|
59
|
+
quads = dataset.match(subject, predicate, valueNode, graph);
|
|
60
|
+
// If there is no valueNode, we must filter by value manually as we
|
|
61
|
+
// weren't able to deduce the datatype.
|
|
62
|
+
if (!valueNode) {
|
|
63
|
+
quads = quads.filter(
|
|
64
|
+
(quad) =>
|
|
65
|
+
quad.object.termType === "Literal" && quad.object.value === value,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
} else {
|
|
69
|
+
// SPO for no value
|
|
70
|
+
quads = dataset.match(subject, predicate, undefined, graph);
|
|
71
|
+
}
|
|
72
|
+
// If this is a langStringSet, filter by language preferences
|
|
73
|
+
if (this.isLangStringSet) {
|
|
74
|
+
return filterQuadsByLanguageOrdering(
|
|
75
|
+
quads,
|
|
76
|
+
this.context.languageOrdering,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
return quads;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
protected getNodeOfFocus(quad: Quad): ObjectNode {
|
|
83
|
+
return quad.object as ObjectNode;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
get [_isSubjectOriented](): false {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SubjectNode,
|
|
3
|
+
PredicateNode,
|
|
4
|
+
ObjectNode,
|
|
5
|
+
GraphNode,
|
|
6
|
+
} from "@ldo/rdf-utils";
|
|
7
|
+
import type { Dataset, Quad } from "@rdfjs/types";
|
|
8
|
+
import type { RawObject } from "../util/RawObject";
|
|
9
|
+
import { SetProxy } from "./SetProxy";
|
|
10
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
11
|
+
import { getNodeFromRawObject } from "../util/getNodeFromRaw";
|
|
12
|
+
import { _isSubjectOriented } from "../types";
|
|
13
|
+
|
|
14
|
+
export type WildcardSubjectSetProxyQuadMatch = [
|
|
15
|
+
undefined | null,
|
|
16
|
+
PredicateNode | undefined | null,
|
|
17
|
+
ObjectNode | undefined | null,
|
|
18
|
+
GraphNode | undefined | null,
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A WildcardObjectProxy represents a set of nodes in a dataset that are all the
|
|
23
|
+
* object of a given subject and predicate. Because this is a wildcard, the
|
|
24
|
+
* subject and predicate don't necissarily need to be defined.
|
|
25
|
+
*/
|
|
26
|
+
export class WildcardSubjectSetProxy<T extends RawObject> extends SetProxy<T> {
|
|
27
|
+
protected quadMatch: WildcardSubjectSetProxyQuadMatch;
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
context: ProxyContext,
|
|
31
|
+
quadMatch: WildcardSubjectSetProxyQuadMatch,
|
|
32
|
+
) {
|
|
33
|
+
super(context, quadMatch);
|
|
34
|
+
this.quadMatch = quadMatch;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
protected getQuads(value?: T | undefined): Dataset<Quad, Quad> {
|
|
38
|
+
const { dataset } = this.context;
|
|
39
|
+
// Get the RDF Node that represents the value, skip is no value
|
|
40
|
+
const predicate = this.quadMatch[1];
|
|
41
|
+
const object = this.quadMatch[2];
|
|
42
|
+
const graph = this.quadMatch[3];
|
|
43
|
+
if (value) {
|
|
44
|
+
const valueNode = getNodeFromRawObject(value, this.context.contextUtil);
|
|
45
|
+
return dataset.match(valueNode, predicate, object, graph);
|
|
46
|
+
}
|
|
47
|
+
// SPO for no value
|
|
48
|
+
return dataset.match(undefined, predicate, object, graph);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
protected getNodeOfFocus(quad: Quad): SubjectNode {
|
|
52
|
+
return quad.subject as SubjectNode;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get [_isSubjectOriented](): true {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { QuadMatch } from "@ldo/rdf-utils";
|
|
2
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
3
|
+
import type { RawObject, RawValue } from "../util/RawObject";
|
|
4
|
+
import type { ObjectSetProxyQuadMatch } from "./ObjectSetProxy";
|
|
5
|
+
import { ObjectSetProxy } from "./ObjectSetProxy";
|
|
6
|
+
import type { SubjectSetProxyQuadMatch } from "./SubjectSetProxy";
|
|
7
|
+
import { SubjectSetProxy } from "./SubjectSetProxy";
|
|
8
|
+
import type { WildcardObjectSetProxyQuadMatch } from "./WildcardObjectSetProxy";
|
|
9
|
+
import { WildcardObjectSetProxy } from "./WildcardObjectSetProxy";
|
|
10
|
+
import type { WildcardSubjectSetProxyQuadMatch } from "./WildcardSubjectSetProxy";
|
|
11
|
+
import { WildcardSubjectSetProxy } from "./WildcardSubjectSetProxy";
|
|
12
|
+
import type { SetProxy } from "./SetProxy";
|
|
13
|
+
|
|
14
|
+
export function createNewSetProxy<T extends NonNullable<RawValue>>(
|
|
15
|
+
quadMatch: QuadMatch,
|
|
16
|
+
isSubjectOriented: boolean,
|
|
17
|
+
proxyContext: ProxyContext,
|
|
18
|
+
isLangSet?: boolean,
|
|
19
|
+
): SetProxy<T> {
|
|
20
|
+
if (!isSubjectOriented) {
|
|
21
|
+
if (quadMatch[0] && quadMatch[1]) {
|
|
22
|
+
return new ObjectSetProxy<T>(
|
|
23
|
+
proxyContext,
|
|
24
|
+
quadMatch as ObjectSetProxyQuadMatch,
|
|
25
|
+
isLangSet,
|
|
26
|
+
);
|
|
27
|
+
} else {
|
|
28
|
+
return new WildcardObjectSetProxy<T>(
|
|
29
|
+
proxyContext,
|
|
30
|
+
quadMatch as WildcardObjectSetProxyQuadMatch,
|
|
31
|
+
isLangSet,
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
if (quadMatch[1] && quadMatch[2]) {
|
|
36
|
+
return new SubjectSetProxy<T & RawObject>(
|
|
37
|
+
proxyContext,
|
|
38
|
+
quadMatch as SubjectSetProxyQuadMatch,
|
|
39
|
+
);
|
|
40
|
+
} else {
|
|
41
|
+
return new WildcardSubjectSetProxy<T & RawObject>(
|
|
42
|
+
proxyContext,
|
|
43
|
+
quadMatch as WildcardSubjectSetProxyQuadMatch,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import type { BlankNode, NamedNode } from "@rdfjs/types";
|
|
2
|
+
import { _getUnderlyingNode } from "../../types";
|
|
3
|
+
import type { RawValue } from "../../util/RawObject";
|
|
4
|
+
import type { LdSet } from "./LdSet";
|
|
5
|
+
import { blankNode } from "@rdfjs/data-model";
|
|
6
|
+
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
8
|
+
export class BasicLdSet<T extends NonNullable<RawValue> = NonNullable<RawValue>>
|
|
9
|
+
implements LdSet<T>
|
|
10
|
+
{
|
|
11
|
+
private hashMap: Map<string, T>;
|
|
12
|
+
|
|
13
|
+
constructor(values?: Iterable<T> | null) {
|
|
14
|
+
this.hashMap = new Map();
|
|
15
|
+
if (values) {
|
|
16
|
+
for (const value of values) {
|
|
17
|
+
this.add(value);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private hashFn(value: T): string {
|
|
23
|
+
if (typeof value !== "object") return value.toString();
|
|
24
|
+
if (value[_getUnderlyingNode]) {
|
|
25
|
+
return (value[_getUnderlyingNode] as NamedNode | BlankNode).value;
|
|
26
|
+
} else if (!value["@id"]) {
|
|
27
|
+
return blankNode().value;
|
|
28
|
+
} else if (typeof value["@id"] === "string") {
|
|
29
|
+
return value["@id"];
|
|
30
|
+
} else {
|
|
31
|
+
return value["@id"].value;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* ===========================================================================
|
|
37
|
+
* Base Set Functions
|
|
38
|
+
* ===========================================================================
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
add(value: T): this {
|
|
42
|
+
const key = this.hashFn(value);
|
|
43
|
+
if (!this.hashMap.has(key)) {
|
|
44
|
+
this.hashMap.set(key, value);
|
|
45
|
+
}
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
clear(): void {
|
|
50
|
+
this.hashMap.clear();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
delete(value: T): boolean {
|
|
54
|
+
const key = this.hashFn(value);
|
|
55
|
+
return this.hashMap.delete(key);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
has(value: T): boolean {
|
|
59
|
+
const key = this.hashFn(value);
|
|
60
|
+
return this.hashMap.has(key);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
get size(): number {
|
|
64
|
+
return this.hashMap.size;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
*entries(): IterableIterator<[T, T]> {
|
|
68
|
+
for (const [, value] of this.hashMap.entries()) {
|
|
69
|
+
yield [value, value];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
keys(): IterableIterator<T> {
|
|
74
|
+
return this.hashMap.values();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
values(): IterableIterator<T> {
|
|
78
|
+
return this.hashMap.values();
|
|
79
|
+
}
|
|
80
|
+
[Symbol.iterator](): IterableIterator<T> {
|
|
81
|
+
return this.hashMap.values();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
get [Symbol.toStringTag]() {
|
|
85
|
+
// TODO: Change this to be human readable.
|
|
86
|
+
return "BasicLdSet";
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* ===========================================================================
|
|
91
|
+
* Array Functions
|
|
92
|
+
* ===========================================================================
|
|
93
|
+
*/
|
|
94
|
+
|
|
95
|
+
every<S extends T>(
|
|
96
|
+
predicate: (value: T, set: LdSet<T>) => value is S,
|
|
97
|
+
thisArg?: any,
|
|
98
|
+
): this is LdSet<S>;
|
|
99
|
+
every(
|
|
100
|
+
predicate: (value: T, set: LdSet<T>) => unknown,
|
|
101
|
+
thisArg?: any,
|
|
102
|
+
): boolean;
|
|
103
|
+
every(predicate: (value: T, set: LdSet<T>) => any, thisArg?: any): boolean {
|
|
104
|
+
for (const value of this) {
|
|
105
|
+
if (!predicate.call(thisArg, value, this)) return false;
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
some(
|
|
111
|
+
predicate: (value: T, set: LdSet<T>) => unknown,
|
|
112
|
+
thisArg?: any,
|
|
113
|
+
): boolean {
|
|
114
|
+
for (const value of this) {
|
|
115
|
+
if (predicate.call(thisArg, value, this)) return true;
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
forEach(
|
|
121
|
+
callbackfn: (value: T, value2: T, set: LdSet<T>) => void,
|
|
122
|
+
thisArg?: any,
|
|
123
|
+
): void {
|
|
124
|
+
for (const value of this) {
|
|
125
|
+
callbackfn.call(thisArg, value, value, this);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
map<U>(callbackfn: (value: T, set: LdSet<T>) => U, thisArg?: any): U[] {
|
|
130
|
+
const returnValues: U[] = [];
|
|
131
|
+
for (const value of this) {
|
|
132
|
+
returnValues.push(callbackfn.call(thisArg, value, this));
|
|
133
|
+
}
|
|
134
|
+
return returnValues;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
filter<S extends T>(
|
|
138
|
+
predicate: (value: T, set: LdSet<T>) => value is S,
|
|
139
|
+
thisArg?: any,
|
|
140
|
+
): LdSet<S>;
|
|
141
|
+
filter(
|
|
142
|
+
predicate: (value: T, set: LdSet<T>) => unknown,
|
|
143
|
+
thisArg?: any,
|
|
144
|
+
): LdSet<T>;
|
|
145
|
+
filter(
|
|
146
|
+
predicate: (value: T, set: LdSet<T>) => any,
|
|
147
|
+
thisArg?: unknown,
|
|
148
|
+
): LdSet<T> {
|
|
149
|
+
const newSet = new BasicLdSet<T>();
|
|
150
|
+
for (const value of this) {
|
|
151
|
+
if (predicate.call(thisArg, value, this)) newSet.add(value);
|
|
152
|
+
}
|
|
153
|
+
return newSet;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
reduce(
|
|
157
|
+
callbackfn: (previousValue: T, currentValue: T, set: LdSet<T>) => T,
|
|
158
|
+
): T;
|
|
159
|
+
reduce(
|
|
160
|
+
callbackfn: (previousValue: T, currentValue: T, set: LdSet<T>) => T,
|
|
161
|
+
initialValue: T,
|
|
162
|
+
): T;
|
|
163
|
+
reduce<U>(
|
|
164
|
+
callbackfn: (previousValue: U, currentValue: T, set: LdSet<T>) => U,
|
|
165
|
+
initialValue: U,
|
|
166
|
+
): U;
|
|
167
|
+
reduce(callbackfn: any, initialValue?: any): any {
|
|
168
|
+
const iterator = this[Symbol.iterator]();
|
|
169
|
+
let accumulator;
|
|
170
|
+
|
|
171
|
+
if (initialValue === undefined) {
|
|
172
|
+
const first = iterator.next();
|
|
173
|
+
if (first.done) {
|
|
174
|
+
throw new TypeError("Reduce of empty collection with no initial value");
|
|
175
|
+
}
|
|
176
|
+
accumulator = first.value;
|
|
177
|
+
} else {
|
|
178
|
+
accumulator = initialValue;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
let result = iterator.next();
|
|
182
|
+
while (!result.done) {
|
|
183
|
+
accumulator = callbackfn(accumulator, result.value, this);
|
|
184
|
+
result = iterator.next();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return accumulator;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
toArray(): T[] {
|
|
191
|
+
const arr: T[] = [];
|
|
192
|
+
this.forEach((value) => arr.push(value));
|
|
193
|
+
return arr;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
toJSON(): T[] {
|
|
197
|
+
return this.toArray();
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* ===========================================================================
|
|
202
|
+
* Set Methods
|
|
203
|
+
* ===========================================================================
|
|
204
|
+
*/
|
|
205
|
+
|
|
206
|
+
difference(other: Set<T>): LdSet<T> {
|
|
207
|
+
return this.filter((value) => !other.has(value));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
intersection(other: Set<T>): LdSet<T> {
|
|
211
|
+
const newSet = new BasicLdSet<T>();
|
|
212
|
+
const iteratingSet = this.size < other.size ? this : other;
|
|
213
|
+
const comparingSet = this.size < other.size ? other : this;
|
|
214
|
+
for (const value of iteratingSet) {
|
|
215
|
+
if (comparingSet.has(value)) {
|
|
216
|
+
newSet.add(value);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return newSet;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
isDisjointFrom(other: Set<T>): boolean {
|
|
223
|
+
const iteratingSet = this.size < other.size ? this : other;
|
|
224
|
+
const comparingSet = this.size < other.size ? other : this;
|
|
225
|
+
for (const value of iteratingSet) {
|
|
226
|
+
if (comparingSet.has(value)) return false;
|
|
227
|
+
}
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
isSubsetOf(other: Set<T>): boolean {
|
|
232
|
+
if (this.size > other.size) return false;
|
|
233
|
+
for (const value of this) {
|
|
234
|
+
if (!other.has(value)) return false;
|
|
235
|
+
}
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
isSupersetOf(other: Set<T>): boolean {
|
|
240
|
+
if (this.size < other.size) return false;
|
|
241
|
+
for (const value of other) {
|
|
242
|
+
if (!this.has(value)) return false;
|
|
243
|
+
}
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
symmetricDifference(other: Set<T>): LdSet<T> {
|
|
248
|
+
const newSet = new BasicLdSet<T>();
|
|
249
|
+
this.forEach((value) => newSet.add(value));
|
|
250
|
+
other.forEach((value) => {
|
|
251
|
+
if (newSet.has(value)) {
|
|
252
|
+
newSet.delete(value);
|
|
253
|
+
} else {
|
|
254
|
+
newSet.add(value);
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
return newSet;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
union(other: Set<T>): LdSet<T> {
|
|
261
|
+
const newSet = new BasicLdSet<T>();
|
|
262
|
+
this.forEach((value) => newSet.add(value));
|
|
263
|
+
other.forEach((value) => newSet.add(value));
|
|
264
|
+
return newSet;
|
|
265
|
+
}
|
|
266
|
+
}
|