Skip to content

Commit fdb5f4b

Browse files
authored
Merge pull request #36 from LDflex/fix/rdfjs-source
fix: allow copying of rdfjs source prototype
2 parents 3ee705e + 7f4dd5f commit fdb5f4b

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"husky": "^6.0.0",
4040
"jest": "^26.0.1",
4141
"mkdirp": "^1.0.4",
42+
"n3": "^1.10.0",
4243
"semantic-release": "^17.4.2",
4344
"setup-polly-jest": "^0.9.1"
4445
},

src/ComunicaEngine.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default class ComunicaEngine {
6262
sources = await flattenAsync(sources.map(s => this.parseSources(s)));
6363
// Needs to be after the string check since those also have a match functions
6464
else if (typeof sources.match === 'function')
65-
sources = [Object.assign({ type: 'rdfjsSource' }, sources)];
65+
sources = [assign({ type: 'rdfjsSource' }, sources)];
6666
// Wrap a single source in an array
6767
else if (typeof source.value === 'string')
6868
sources = [sources];
@@ -144,6 +144,17 @@ export default class ComunicaEngine {
144144
}
145145
}
146146

147+
/**
148+
* Extends Object.assign by also copying prototype methods
149+
* @param {Object} props To add to the object
150+
* @param {Object} orig Original Object
151+
* @returns Copy of original object with added props
152+
*/
153+
function assign(props, orig) {
154+
// https://stackoverflow.com/questions/41474986/how-to-clone-a-javascript-es6-class-instance
155+
return Object.assign(Object.create(orig), { ...orig, ...props });
156+
}
157+
147158
// Flattens the given array one level deep
148159
async function flattenAsync(array) {
149160
return [].concat(...(await Promise.all(array)));

test/ComunicaEngine-test.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import ComunicaEngine from '../src/ComunicaEngine';
22

33
import { mockHttp } from './util';
4-
import { namedNode, defaultGraph } from '@rdfjs/data-model';
4+
import { namedNode, defaultGraph, quad } from '@rdfjs/data-model';
5+
import { Store } from 'n3';
56
import { Readable } from 'stream';
67

78
const SELECT_TYPES = `
@@ -195,6 +196,57 @@ describe('An ComunicaEngine instance with a default source', () => {
195196
});
196197
});
197198

199+
describe('A ComunicaEngine instance with an rdfjs source (in list)', () => {
200+
const store = new Store([
201+
quad(
202+
namedNode('http://example.org/Jesse'),
203+
namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
204+
namedNode('http://xmlns.com/foaf/0.1/Person'),
205+
),
206+
]);
207+
208+
const engine = new ComunicaEngine([store]);
209+
210+
it('yields results for a SELECT query', async () => {
211+
const result = engine.execute(SELECT_TYPES);
212+
expect(await readAll(result)).toHaveLength(1);
213+
});
214+
});
215+
216+
describe('A ComunicaEngine instance with an rdfjs source', () => {
217+
const store = new Store([
218+
quad(
219+
namedNode('http://example.org/Jesse'),
220+
namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
221+
namedNode('http://xmlns.com/foaf/0.1/Person'),
222+
),
223+
]);
224+
225+
const engine = new ComunicaEngine(store);
226+
227+
it('yields results for a SELECT query', async () => {
228+
const result = engine.execute(SELECT_TYPES);
229+
expect(await readAll(result)).toHaveLength(1);
230+
});
231+
});
232+
233+
describe('A ComunicaEngine instance with an rdfjs source (as input to execute)', () => {
234+
const store = new Store([
235+
quad(
236+
namedNode('http://example.org/Jesse'),
237+
namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
238+
namedNode('http://xmlns.com/foaf/0.1/Person'),
239+
),
240+
]);
241+
242+
const engine = new ComunicaEngine();
243+
244+
it('yields results for a SELECT query', async () => {
245+
const result = engine.execute(SELECT_TYPES, store);
246+
expect(await readAll(result)).toHaveLength(1);
247+
});
248+
});
249+
198250
describe('An ComunicaEngine instance with a default source that errors', () => {
199251
const engine = new ComunicaEngine(Promise.reject(new Error('my error')));
200252

0 commit comments

Comments
 (0)