Updated array tests to use set()

main
Jackson Morgan 7 months ago
parent df839189cf
commit ffe5d3f047
  1. 12
      packages/jsonld-dataset-proxy/src/JsonldDatasetProxyBuilder.ts
  2. 1
      packages/jsonld-dataset-proxy/src/index.ts
  3. 25
      packages/jsonld-dataset-proxy/src/setProxy/ldSet/BasicLdSet.ts
  4. 14
      packages/jsonld-dataset-proxy/src/setProxy/set.ts
  5. 5
      packages/jsonld-dataset-proxy/src/util/isProxy.ts
  6. 6
      packages/jsonld-dataset-proxy/test/ContextUtil.test.ts
  7. 8
      packages/jsonld-dataset-proxy/test/isProxy.test.ts
  8. 897
      packages/jsonld-dataset-proxy/test/jsonldDatasetProxy.test.ts
  9. 7
      packages/jsonld-dataset-proxy/test/patientExampleData.ts

@ -57,9 +57,9 @@ export class JsonldDatasetProxyBuilder {
* @param graph The graph to match
*/
matchSubject<T extends ObjectLike>(
predicate: QuadMatch[1] | undefined | null,
object: QuadMatch[2] | undefined | null,
graph: QuadMatch[3] | undefined | null,
predicate?: QuadMatch[1] | undefined | null,
object?: QuadMatch[2] | undefined | null,
graph?: QuadMatch[3] | undefined | null,
): LdSet<T> {
return this.proxyContext.createSetProxy(
[null, predicate, object, graph],
@ -75,9 +75,9 @@ export class JsonldDatasetProxyBuilder {
* @param graph The graph to match
*/
matchObject<T extends ObjectLike>(
subject: QuadMatch[0] | undefined | null,
predicate: QuadMatch[1],
graph: QuadMatch[3] | undefined | null,
subject?: QuadMatch[0] | undefined | null,
predicate?: QuadMatch[1],
graph?: QuadMatch[3] | undefined | null,
): LdSet<T> {
return this.proxyContext.createSetProxy([
subject,

@ -19,6 +19,7 @@ export * from "./language/languageUtils";
export * from "./setProxy/isSetProxy";
export * from "./setProxy/SetProxy";
export * from "./setProxy/set";
export * from "./setProxy/ldSet/LdSet";
export * from "./setProxy/ldSet/BasicLdSet";

@ -1,9 +1,9 @@
import type { BlankNode, NamedNode } from "@rdfjs/types";
import type { ProxyContext } from "../../ProxyContext";
import { _getUnderlyingNode } from "../../types";
import { getNodeFromRawObject } from "../../util/getNodeFromRaw";
import { nodeToString } from "../../util/NodeSet";
import type { RawValue } from "../../util/RawObject";
import type { LdSet } from "./LdSet";
import { blankNode } from "@rdfjs/data-model";
/* eslint-disable @typescript-eslint/no-explicit-any */
export class BasicLdSet<T extends NonNullable<RawValue> = NonNullable<RawValue>>
@ -13,14 +13,19 @@ export class BasicLdSet<T extends NonNullable<RawValue> = NonNullable<RawValue>>
protected context: ProxyContext;
private hashMap = new Map();
constructor(proxyContext: ProxyContext) {
super();
this.context = proxyContext;
constructor(values?: Iterable<T> | null) {
super(values);
}
private hashFn(value: T) {
if (typeof value !== "object") return value.toString();
return nodeToString(getNodeFromRawObject(value, this.context.contextUtil));
if (value[_getUnderlyingNode]) {
return (value[_getUnderlyingNode] as NamedNode | BlankNode).value;
} else if (!value["@id"]) {
return blankNode().value;
} else {
return value["@id"];
}
}
/**
@ -117,7 +122,7 @@ export class BasicLdSet<T extends NonNullable<RawValue> = NonNullable<RawValue>>
predicate: (value: T, set: LdSet<T>) => any,
thisArg?: unknown,
): LdSet<T> {
const newSet = new BasicLdSet<T>(this.context);
const newSet = new BasicLdSet<T>();
for (const value of this) {
if (predicate.call(thisArg, value, this)) newSet.add(value);
}
@ -169,7 +174,7 @@ export class BasicLdSet<T extends NonNullable<RawValue> = NonNullable<RawValue>>
}
intersection(other: Set<T>): LdSet<T> {
const newSet = new BasicLdSet<T>(this.context);
const newSet = new BasicLdSet<T>();
const iteratingSet = this.size < other.size ? this : other;
const comparingSet = this.size < other.size ? other : this;
for (const value of iteratingSet) {
@ -206,7 +211,7 @@ export class BasicLdSet<T extends NonNullable<RawValue> = NonNullable<RawValue>>
}
symmetricDifference(other: Set<T>): LdSet<T> {
const newSet = new BasicLdSet<T>(this.context);
const newSet = new BasicLdSet<T>();
this.forEach((value) => newSet.add(value));
other.forEach((value) => {
if (newSet.has(value)) {
@ -219,7 +224,7 @@ export class BasicLdSet<T extends NonNullable<RawValue> = NonNullable<RawValue>>
}
union(other: Set<T>): LdSet<T> {
const newSet = new BasicLdSet<T>(this.context);
const newSet = new BasicLdSet<T>();
this.forEach((value) => newSet.add(value));
other.forEach((value) => newSet.add(value));
return newSet;

@ -0,0 +1,14 @@
import type { RawValue } from "../util/RawObject";
import { BasicLdSet } from "./ldSet/BasicLdSet";
import type { LdSet } from "./ldSet/LdSet";
/**
* Creates an LdSet used by LDO as a list of items.
* @param values The list of items in the set
* @returns An LdSet
*/
export function set<T>(...values: T[]): LdSet<T> {
return new BasicLdSet(
values as Iterable<NonNullable<RawValue>>,
) as unknown as LdSet<T>;
}

@ -1,5 +1,6 @@
import { isSetProxy } from "../setProxy/isSetProxy";
import type { SetProxy } from "../setProxy/setProxy";
import type { LdSet } from "../setProxy/ldSet/LdSet";
import type { SetProxy } from "../setProxy/SetProxy";
import { isSubjectProxy } from "../subjectProxy/isSubjectProxy";
import type { SubjectProxy } from "../subjectProxy/SubjectProxy";
import type { ObjectLike } from "../types";
@ -11,7 +12,7 @@ export function isProxy(
}
export function getProxyFromObject(
object: ObjectLike | ObjectLike[],
object: ObjectLike | LdSet<ObjectLike>,
): SubjectProxy | SetProxy {
if (!isProxy(object)) {
throw new Error(`${object} is not a Jsonld Dataset Proxy`);

@ -53,13 +53,11 @@ describe("ContextUtil", () => {
});
});
describe("isArray", () => {
describe("isSet", () => {
it("indicates that the special @isCollection field means array", () => {
const contextUtil = new ContextUtil(scopedContext);
expect(
contextUtil.isArray("element", [
namedNode("http://example.com/Avatar"),
]),
contextUtil.isSet("element", [namedNode("http://example.com/Avatar")]),
).toBe(true);
});
});

@ -1,7 +1,7 @@
import {
getProxyFromObject,
getSubjectProxyFromObject,
isArrayProxy,
isSetProxy,
isSubjectProxy,
} from "../src";
@ -25,12 +25,12 @@ describe("isProxy", () => {
});
});
describe("isArrayProxy", () => {
describe("isSetProxy", () => {
it("returns false if undefined is passed as a parameter", () => {
expect(isArrayProxy(undefined)).toBe(false);
expect(isSetProxy(undefined)).toBe(false);
});
it("returns false if string is passed as a parameter", () => {
expect(isArrayProxy("hello")).toBe(false);
expect(isSetProxy("hello")).toBe(false);
});
});

@ -1,6 +1,7 @@
import type { ContextDefinition } from "jsonld";
import type { Schema } from "shexj";
import type { LdoJsonldContext } from "../src/LdoJsonldContext";
import type { LdSet } from "../src";
export interface ObservationShape {
"@id"?: string;
@ -15,12 +16,12 @@ export type PatientShape = {
"@id"?: string;
"@context"?: ContextDefinition;
type: { "@id": "Patient" };
name?: string[];
langName?: string[];
name?: LdSet<string>;
langName?: LdSet<string>;
birthdate?: string;
age?: number;
isHappy?: boolean;
roommate?: PatientShape[];
roommate?: LdSet<PatientShape>;
};
// No need to fully define the schema because this library doesn't use it

Loading…
Cancel
Save