JS: Overrides the toString method on terms

pull/190/head
Tpt 3 years ago
parent 181439ca4f
commit ed13b7bcf3
  1. 2
      js/README.md
  2. 30
      js/src/model.rs
  3. 34
      js/test/model.js

@ -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.

@ -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

@ -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('<http://example.com>', 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(
'<http://example.com/s> <http://example.com/p> <<<http://example.com/s1> <http://example.com/p1> <http://example.com/o1>>> <http://example.com/g>',
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()
)
})
})
})

Loading…
Cancel
Save