Skip to content

Commit

Permalink
feat(datacube): fetch datasets by an IRI or a graph IRI
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Sep 4, 2019
1 parent 36ecf65 commit ca917fd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/__tests__/datacube.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { DataCube } from "../datacube";

const cube = new DataCube("https://ld.stadt-zuerich.ch/query");

describe(".datasetByIri()", () => {
it("takes an IRI", async () => {
const datasets = await cube.datasets();
for (const dataset of datasets) {
const iri = dataset.iri;
expect(await cube.datasetByIri(iri)).toBe(dataset);
}
});
});

describe(".datasetsByGraph()", () => {
it("takes a graph IRI", async () => {
const datasets = await cube.datasets();
const graphs = await cube.graphs();
for (const graph of graphs) {
const graphIri = graph.value;
const expected = datasets.filter((dataset) => dataset.graphIri.equals(graph));
expect(await cube.datasetsByGraph(graphIri)).toStrictEqual(expected);
}
});
});
20 changes: 20 additions & 0 deletions src/datacube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ export class DataCube {
new DataSet(this.endpoint, { dataSetIri, dataSetLabel, graphIri }));
}

/**
* Fetch a [[DataSet]] by its IRI.
*
* @param iri IRI of the DataSet to return.
*/
public async datasetByIri(iri: string): Promise<DataSet> {
const datasets = await this.datasets();
return datasets.find((dataset) => dataset.iri === iri);
}

/**
* Fetch [[DataSet]]s by their graph IRI.
*
* @param graphIri IRI of the graph to look for in all DataSets.
*/
public async datasetsByGraph(graphIri: string): Promise<DataSet[]> {
const datasets = await this.datasets();
return datasets.filter((dataset) => dataset.graphIri.value === graphIri);
}

/**
* Fetch all graphs from the endpoint.
*/
Expand Down

0 comments on commit ca917fd

Please sign in to comment.