Skip to content

Commit

Permalink
feat: add resourceShapeFromQuads to discovery (#201)
Browse files Browse the repository at this point in the history
* feat: add resourceShapeFromQuads to discovery

* test: remove redundant casts

Signed-off-by: Wouter Termont <[email protected]>

Co-authored-by: Wouter Termont <[email protected]>
  • Loading branch information
Arne Vandoorslaer and woutermont authored Dec 9, 2021
1 parent 0fbe961 commit e6b863d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
21 changes: 21 additions & 0 deletions packages/semcom-sdk/lib/discovery.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { DataFactory } from 'n3';
import { resourceShapeFromQuads } from './discovery';

describe('resourceShapeFromQuads', () => {

const quadPredicate = 'http://example.com/predicate';

const quad = DataFactory.quad(
DataFactory.namedNode('test'),
DataFactory.namedNode(quadPredicate),
DataFactory.namedNode('test')
);

it('should discover shapes from a quad', () => {

const shapes = resourceShapeFromQuads([ quad ]);
expect(shapes).toContainEqual(quadPredicate);

});

});
26 changes: 18 additions & 8 deletions packages/semcom-sdk/lib/discovery.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
import { Store, Parser, DataFactory } from 'n3';
import { Store, Parser, DataFactory, Quad } from 'n3';

/**
* Discovers the shape of a resource, given quads.
*
* @param quads the quads for which to discover shapes.
* @returns the uris of the discovered shapes.
*/
export const resourceShapeFromQuads = (quads: Quad[]): string[] => {

const predicates = new Set<string>(quads.map((quad) => quad.predicate.value));

const classes = new Store(quads).getQuads(null, DataFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), null, null).map((quad) => quad.object.value);

return Array.from(predicates).concat(classes);

};

/**
* Discovers the shape of a resource, given its uri and a (custom) fetch function.
Expand All @@ -21,13 +37,7 @@ export const resourceShape = (

const parser = new Parser();

return response.text().then((body) => {

const classes = new Store(parser.parse(body)).getQuads(null, DataFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), null, null).map((quad) => quad.object.value);

return classes;

});
return response.text().then((body) => resourceShapeFromQuads(parser.parse(body)));

});

Expand Down
5 changes: 5 additions & 0 deletions packages/semcom-sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/semcom-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@
"coverageThreshold": {
"global": {
"branches": 87.5,
"functions": 50,
"lines": 80.95,
"statements": 77.61
"functions": 55.56,
"lines": 87.69,
"statements": 84.72
}
}
}
}
}

0 comments on commit e6b863d

Please sign in to comment.