diff --git a/js/README.md b/js/README.md index f847aac1..f5dffff2 100644 --- a/js/README.md +++ b/js/README.md @@ -57,6 +57,8 @@ const foo = oxigraph.literal("foo"); const quad = oxigraph.quad(blank, ex, foo); ``` +All terms overrides the the `toString()` method to return a N-Quads/SPARQL-like representation of the terms. + ### `Store` Oxigraph API is centered around the `Store` class. diff --git a/js/src/model.rs b/js/src/model.rs index bc56c422..83b7f175 100644 --- a/js/src/model.rs +++ b/js/src/model.rs @@ -112,6 +112,11 @@ impl JsNamedNode { self.inner.as_str().to_owned() } + #[wasm_bindgen(js_name = toString)] + pub fn to_string(&self) -> String { + self.inner.to_string() + } + pub fn equals(&self, other: &JsValue) -> bool { if let Ok(Some(JsTerm::NamedNode(other))) = FromJsConverter::default().to_optional_term(other) @@ -177,6 +182,11 @@ impl JsBlankNode { self.inner.as_str().to_owned() } + #[wasm_bindgen(js_name = toString)] + pub fn to_string(&self) -> String { + self.inner.to_string() + } + pub fn equals(&self, other: &JsValue) -> bool { if let Ok(Some(JsTerm::BlankNode(other))) = FromJsConverter::default().to_optional_term(other) @@ -252,6 +262,11 @@ impl JsLiteral { self.inner.datatype().into_owned().into() } + #[wasm_bindgen(js_name = toString)] + pub fn to_string(&self) -> String { + self.inner.to_string() + } + pub fn equals(&self, other: &JsValue) -> bool { if let Ok(Some(JsTerm::Literal(other))) = FromJsConverter::default().to_optional_term(other) { @@ -296,6 +311,11 @@ impl JsDefaultGraph { "".to_owned() } + #[wasm_bindgen(js_name = toString)] + pub fn to_string(&self) -> String { + "DEFAULT".to_string() + } + pub fn equals(&self, other: &JsValue) -> bool { if let Ok(Some(JsTerm::DefaultGraph(other))) = FromJsConverter::default().to_optional_term(other) @@ -325,6 +345,11 @@ impl JsVariable { self.inner.as_str().to_owned() } + #[wasm_bindgen(js_name = toString)] + pub fn to_string(&self) -> String { + self.inner.to_string() + } + pub fn equals(&self, other: &JsValue) -> bool { if let Ok(Some(JsTerm::Variable(other))) = FromJsConverter::default().to_optional_term(other) @@ -386,6 +411,11 @@ impl JsQuad { JsTerm::from(self.inner.graph_name.clone()).into() } + #[wasm_bindgen(js_name = toString)] + pub fn to_string(&self) -> String { + self.inner.to_string() + } + pub fn equals(&self, other: &JsValue) -> bool { if let Ok(Some(JsTerm::Quad(other))) = FromJsConverter::default().to_optional_term(other) { self == &other diff --git a/js/test/model.js b/js/test/model.js index 5c8404dd..7900073f 100644 --- a/js/test/model.js +++ b/js/test/model.js @@ -1,2 +1,36 @@ +/* global describe, it */ + const oxigraph = require('../pkg/oxigraph.js') +const assert = require('assert') require('../node_modules/@rdfjs/data-model/test/index.js')(oxigraph) + +describe('DataModel', function () { + describe('#toString()', function () { + it('namedNode().toString() should return SPARQL compatible syntax', function () { + assert.strictEqual('', oxigraph.namedNode('http://example.com').toString()) + }) + + it('blankNode().toString() should return SPARQL compatible syntax', function () { + assert.strictEqual('_:a', oxigraph.blankNode('a').toString()) + }) + + it('literal().toString() should return SPARQL compatible syntax', function () { + assert.strictEqual('"a\\"b"@en', oxigraph.literal('a"b', 'en').toString()) + }) + + it('defaultGraph().toString() should return SPARQL compatible syntax', function () { + assert.strictEqual('DEFAULT', oxigraph.defaultGraph().toString()) + }) + + it('variable().toString() should return SPARQL compatible syntax', function () { + assert.strictEqual('?a', oxigraph.variable('a').toString()) + }) + + it('quad().toString() should return SPARQL compatible syntax', function () { + assert.strictEqual( + ' << >> ', + oxigraph.quad(oxigraph.namedNode('http://example.com/s'), oxigraph.namedNode('http://example.com/p'), oxigraph.quad(oxigraph.namedNode('http://example.com/s1'), oxigraph.namedNode('http://example.com/p1'), oxigraph.namedNode('http://example.com/o1')), oxigraph.namedNode('http://example.com/g')).toString() + ) + }) + }) +})