diff --git a/__tests__/main-test.ts b/__tests__/main-test.ts index 7707495..70204a0 100644 --- a/__tests__/main-test.ts +++ b/__tests__/main-test.ts @@ -4,9 +4,9 @@ import path from 'path'; import { write } from '../lib'; import 'jest-rdf'; -async function getQuads(file: string) { +async function getQuads(file: string, _prefixes: Record = {}) { const parser = new Parser({ rdfStar: true } as any); - const prefixes: Record = {}; + const prefixes = { ..._prefixes }; const quads = parser.parse(fs.readFileSync(path.join(__dirname, '..', 'data', file)).toString(), undefined, (prefix, iri) => { prefixes[prefix] = iri.value; }); @@ -42,6 +42,30 @@ it('It should correctly write turtle files', async () => { } }); +it('Should should strip unnecessary prefixes', async () => { + for (const file of fs.readdirSync(path.join(__dirname, '..', 'data'))) { + try { + const { string, quads } = await getQuads(file, { + alt: 'http://example.alt.org/', + v1: 'http://example.v1.org/', + }); + + if (loose[file]) { + // If loose we only need the quads to match when we re-parse the string + expect((new Parser()).parse(string)).toBeRdfIsomorphic(quads); + } else { + // If not loose we expect an exact string match + expect(string.replace(/b\d+_/g, '')).toEqual(fs.readFileSync(path.join(__dirname, '..', 'data', file)).toString()); + } + } catch (e: any) { + // Suppress errors on {| syntax since N3 cannot parse it for now + if (!`${e}`.includes('Unexpected "|"')) { + throw e; + } + } + } +}); + it('Should throw error named graphs in quoted and asserted triples', () => { expect(write([DataFactory.quad( DataFactory.namedNode('s'), diff --git a/data/bnodes4.ttl b/data/bnodes4.ttl index df5ccd6..2842eb6 100644 --- a/data/bnodes4.ttl +++ b/data/bnodes4.ttl @@ -1,3 +1,2 @@ -@prefix ex: . _:s3 a _:s3 . diff --git a/data/star-subject.ttl b/data/star-subject.ttl index 0f2f664..64d4960 100644 --- a/data/star-subject.ttl +++ b/data/star-subject.ttl @@ -1,8 +1,4 @@ @prefix ex: . @prefix owl: . -@prefix rdf: . -@prefix rdfs: . -@prefix sh: . -@prefix xsd: . <> a <> . diff --git a/lib/ttlwriter.ts b/lib/ttlwriter.ts index 711e033..6373663 100644 --- a/lib/ttlwriter.ts +++ b/lib/ttlwriter.ts @@ -32,10 +32,37 @@ export class TTLWriter { writer: Writer, prefixes: { [prefix: string]: string } = {}, ) { + const terms: Set = new Set(); + + function addTerm(term: RDF.Term) { + if (term.termType === 'NamedNode') { + terms.add(term.value); + } else if (term.termType === 'Quad') { + addTerm(term.subject); + addTerm(term.predicate); + addTerm(term.object); + addTerm(term.graph); + } + } + + for (const list of [ + this.store.getSubjects(null, null, null), + this.store.getPredicates(null, null, null), + this.store.getObjects(null, null, null), + ]) { + for (const node of list) { + addTerm(node); + } + } + + const termList = [...terms]; + for (const key of Object.keys(prefixes)) { - const iri = prefixes[key]; - this.prefixRev[iri] = key; - this.prefixes[key] = iri; + if (termList.some((term) => term.startsWith(prefixes[key]))) { + const iri = prefixes[key]; + this.prefixRev[iri] = key; + this.prefixes[key] = iri; + } } this.writer = writer; }