Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correctly handle custom data factories by binding functions #467

Merged
merged 5 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 5 additions & 32 deletions package-lock.json

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

25 changes: 18 additions & 7 deletions src/N3Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1083,13 +1083,24 @@ function noop() {}
// Initializes the parser with the given data factory
function initDataFactory(parser, factory) {
// Set factory methods
const namedNode = factory.namedNode;
parser._namedNode = namedNode;
parser._blankNode = factory.blankNode;
parser._literal = factory.literal;
parser._variable = factory.variable;
parser._quad = factory.quad;
parser.DEFAULTGRAPH = factory.defaultGraph();
let namedNode;
if (factory === N3DataFactory) {
namedNode = factory.namedNode;
parser._namedNode = namedNode;
parser._blankNode = factory.blankNode;
parser._literal = factory.literal;
parser._variable = factory.variable;
parser._quad = factory.quad;
parser.DEFAULTGRAPH = factory.defaultGraph();
} else {
namedNode = factory.namedNode.bind(factory);
parser._namedNode = namedNode;
parser._blankNode = factory.blankNode.bind(factory);
parser._literal = factory.literal.bind(factory);
parser._variable = factory.variable.bind(factory);
parser._quad = factory.quad.bind(factory);
parser.DEFAULTGRAPH = factory.defaultGraph();
}

// Set common named nodes
parser.RDF_FIRST = namedNode(namespaces.rdf.first);
Expand Down
43 changes: 43 additions & 0 deletions test/N3Parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2592,6 +2592,49 @@ describe('Parser', () => {
),
])).toBe(true);
});

it('should use the internal `this` state', () => {
const customFactory = {
blankI: 0,
...rdfDataModel,
blankNode: function () {
return DF.blankNode(`b-custom-${this.blankI++}`);
},
};

const parser = new Parser({
baseIRI: BASE_IRI,
format: 'n3',
factory: customFactory,
});

const quads = parser.parse(`
@prefix : <http://example.com/> .
[ :friend [
:name "Thomas" ;
]
] .
`);

expect(quads.length).toBeGreaterThan(0);

// The blank node should be named b-custom-0 and b-custom-1
const bnode1 = DF.blankNode('b-custom-0');
const bnode2 = DF.blankNode('b-custom-1');

expect(isomorphic(quads, [
DF.quad(
bnode1,
DF.namedNode('http://example.com/friend'),
bnode2,
),
DF.quad(
bnode2,
DF.namedNode('http://example.com/name'),
DF.literal('Thomas'),
),
])).toBe(true);
});
});

describe('A turtle parser instance with external data factory', () => {
Expand Down