Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rdf-ext

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rdf-ext - npm Package Compare versions

Comparing version 0.2.6 to 0.3.0-rc1

.travis.yml

79

lib/utils-browser.js

@@ -1,61 +0,52 @@

/* global DOMParser, XMLHttpRequest */
'use strict';
/* global XMLHttpRequest */
var utils = {}
utils.defaultRequest = function (method, requestUrl, headers, content, callback) {
return new Promise(function (resolve, reject) {
callback = callback || function () {}
var utils = {};
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function () {
if (xhr.readyState === xhr.DONE) {
var headerLines = xhr.getAllResponseHeaders().split('\r\n')
var resHeaders = {}
utils.defaultRequest = function (method, requestUrl, headers, content, callback) {
var xhr = new XMLHttpRequest();
for (var i = 0; i < headerLines.length; i++) {
var headerLine = headerLines[i].split(': ', 2)
xhr.onreadystatechange = function () {
if (xhr.readyState === xhr.DONE) {
var
headerLines = xhr.getAllResponseHeaders().split('\r\n'),
resHeaders = {};
resHeaders[headerLine[0].toLowerCase()] = headerLine[1]
}
for (var i = 0; i < headerLines.length; i++) {
var headerLine = headerLines[i].split(': ', 2);
resHeaders[headerLine[0].toLowerCase()] = headerLine[1];
callback(xhr.status, resHeaders, xhr.responseText)
resolve({
statusCode: xhr.status,
headers: resHeaders,
content: xhr.responseText
})
}
callback(xhr.status, resHeaders, xhr.responseText);
}
};
xhr.open(method, requestUrl, true);
xhr.open(method, requestUrl, true)
for (var header in headers) {
xhr.setRequestHeader(header, headers[header]);
}
for (var header in headers) {
xhr.setRequestHeader(header, headers[header])
}
xhr.send(content);
};
xhr.send(content)
})
}
utils.corsProxyRequest = function (proxyUrl, method, requestUrl, headers, content, callback) {
var url = proxyUrl + '?url=' + encodeURIComponent(requestUrl);
var url = proxyUrl + '?url=' + encodeURIComponent(requestUrl)
utils.defaultRequest(method, url, headers, content, callback);
};
return utils.defaultRequest(method, url, headers, content, callback)
}
utils.parseHtml = function (toparse, base) {
var parser = new DOMParser();
return parser.parseFromString(toparse, 'text/html');
};
utils.parseXml = function (toparse, base) {
var parser = new DOMParser();
return parser.parseFromString(toparse, 'application/xml');
};
utils.mixin = function (rdf) {
rdf.defaultRequest = utils.defaultRequest;
rdf.corsProxyRequest = utils.corsProxyRequest;
rdf.parseHtml = utils.parseHtml;
rdf.parseXml = utils.parseXml;
};
rdf.defaultRequest = utils.defaultRequest
rdf.corsProxyRequest = utils.corsProxyRequest
}
module.exports = utils.mixin;
module.exports = utils.mixin

@@ -1,62 +0,60 @@

'use strict';
var http = require('http')
var https = require('https')
var url = require('url')
var
http = require('http'),
https = require('https'),
url = require('url');
var utils = {}
utils.defaultRequest = function (method, requestUrl, headers, content, callback) {
return new Promise(function (resolve, reject) {
callback = callback || function () {}
var utils = {};
var options = url.parse(requestUrl)
var client = http
options.hash = null
options.method = method
options.headers = headers
utils.defaultRequest = function (method, requestUrl, headers, content, callback) {
var
options = url.parse(requestUrl),
client = http;
if (options.protocol === 'https:') {
client = https
}
options.hash = null;
options.method = method;
options.headers = headers;
var req = client.request(options, function (res) {
var resContent = ''
if (options.protocol === 'https:') {
client = https;
}
res.setEncoding('utf8')
var req = client.request(options, function (res) {
var resContent = '';
res.on('data', function (chunk) {
resContent += chunk
})
res.setEncoding('utf8');
res.on('data', function (chunk) { resContent += chunk; });
res.on('end', function () { callback(res.statusCode, res.headers, resContent); });
});
res.on('end', function () {
callback(res.statusCode, res.headers, resContent)
req.on('error', function (error) { callback(null, null, null, error); });
resolve({
statusCode: res.statusCode,
headers: res.headers,
content: resContent
})
})
})
if (content) {
req.write(content);
}
req.on('error', function (error) {
callback(null, null, null, error)
req.end();
};
reject(error)
})
utils.parseHtml = function (toparse, base) {
var parser = new (require('xmldom').DOMParser)();
if (content) {
req.write(content)
}
return parser.parseFromString(toparse, 'text/html');
};
req.end()
})
}
utils.parseXml = function (toparse, base) {
var parser = new (require('xmldom').DOMParser)();
return parser.parseFromString(toparse, 'application/xml');
};
utils.mixin = function (rdf) {
rdf.Promise = rdf.Promise || require('es6-promise').Promise;
rdf.defaultRequest = utils.defaultRequest;
rdf.parseHtml = utils.parseHtml;
rdf.parseXml = utils.parseXml;
};
rdf.defaultRequest = utils.defaultRequest
}
module.exports = utils.mixin;
module.exports = utils.mixin

@@ -1,69 +0,63 @@

'use strict';
var utils = {}
var utils = {};
/*
* generic filters
*/
utils.filter = {};
utils.filter = {}
utils.filter.namedNode = function (node) {
return node.interfaceName === 'NamedNode';
};
return node.interfaceName === 'NamedNode'
}
utils.filter.blankNode = function (node) {
return node.interfaceName === 'BlankNode';
};
return node.interfaceName === 'BlankNode'
}
utils.filter.literal = function (node) {
return node.interfaceName === 'Literal';
};
return node.interfaceName === 'Literal'
}
utils.filter.namedNodeSubject = function (subject) {
return function (triple) {
return triple.subject.interfaceName !== 'NamedNode' || triple.subject.equals(subject);
};
};
return triple.subject.interfaceName !== 'NamedNode' || triple.subject.equals(subject)
}
}
/*
* list triple parts
*/
utils.list = {};
utils.list = {}
utils.list.tripleParts = function (graph, part, filter) {
var nodes = {};
var nodes = {}
filter = filter || function () { return true; };
filter = filter || function () { return true }
graph.forEach(function (triple) {
nodes[triple[part].toNT()] = triple[part];
});
nodes[triple[part].toString()] = triple[part]
})
nodes = Object.keys(nodes)
.map(function (key) {
return nodes[key];
return nodes[key]
})
.filter(function (node) {
return filter(node);
});
return filter(node)
})
return nodes;
};
return nodes
}
utils.list.subjects = function (graph, filter) {
return utils.list.tripleParts(graph, 'subject', filter);
};
return utils.list.tripleParts(graph, 'subject', filter)
}
utils.list.predicates = function (graph, filter) {
return utils.list.tripleParts(graph, 'predicate', filter);
};
return utils.list.tripleParts(graph, 'predicate', filter)
}
utils.list.objects = function (graph, filter) {
return utils.list.tripleParts(graph, 'object', filter);
};
return utils.list.tripleParts(graph, 'object', filter)
}
/*

@@ -73,43 +67,40 @@ * Creates a subgraph by traversing a graph with filter support

utils.createSubGraph = function (rdf, graph, entry, filter) {
var
processedEntries = {},
subGraph = rdf.createGraph();
var processedEntries = {}
var subGraph = rdf.createGraph()
var processSubject = function (entries) {
if (entries.length === 0) {
return;
return
}
var
newEntries = [],
matches = rdf.createGraph();
var newEntries = []
var matches = rdf.createGraph()
entries.forEach(function (entry) {
matches.addAll(graph.match(entry));
});
matches.addAll(graph.match(entry))
})
if (filter) {
matches = matches.filter(filter);
matches = matches.filter(filter)
}
matches.forEach(function (match) {
var key = match.object.toNT();
var key = match.object.toString()
if (!(key in processedEntries)) {
newEntries.push(match.object);
processedEntries[key] = true;
newEntries.push(match.object)
processedEntries[key] = true
}
});
})
subGraph.addAll(matches);
subGraph.addAll(matches)
processSubject(newEntries);
};
processSubject(newEntries)
}
processSubject([entry]);
processSubject([entry])
return subGraph;
};
return subGraph
}
/*

@@ -119,4 +110,4 @@ * Create a subgraph based on a named node subject without crossing named node borders

utils.createSubGraphByNamedNodeSubject = function (rdf, graph, subject) {
return rdf.utils.createSubGraph(graph, subject, utils.filter.namedNodeSubject(subject));
};
return rdf.utils.createSubGraph(graph, subject, utils.filter.namedNodeSubject(subject))
}

@@ -127,16 +118,15 @@ /*

utils.splitGraphByNamedNodeSubject = function (rdf, graph, store) {
store = store || new rdf.promise.Store(rdf.createStore());
store = store || rdf.createStore()
var adds = [];
var adds = []
utils.list.subjects(graph, utils.filter.namedNode).forEach(function (subject) {
adds.push(store.add(subject, utils.createSubGraphByNamedNodeSubject(rdf, graph, subject)));
});
adds.push(store.add(subject, utils.createSubGraphByNamedNodeSubject(rdf, graph, subject)))
})
return Promise.all(adds).then(function () {
return store;
});
};
return store
})
}
/*

@@ -148,3 +138,3 @@ * namespace mapping

if (node.interfaceName !== 'NamedNode') {
return node;
return node
}

@@ -154,8 +144,8 @@

if (node.nominalValue.toString().indexOf(search) !== 0) {
return node;
return node
}
// create new named node with replace + original node without search
return rdf.createNamedNode(replace + node.nominalValue.toString().substr(search.length));
};
return rdf.createNamedNode(replace + node.nominalValue.toString().substr(search.length))
}

@@ -167,45 +157,54 @@ utils.mapNamespaceTriple = function (rdf, triple, search, replace) {

utils.mapNamespaceNode(rdf, triple.object, search, replace)
);
};
)
}
utils.mapNamespaceGraph = function (rdf, graph, search, replace) {
var mappedGraph = rdf.createGraph();
var mappedGraph = rdf.createGraph()
graph.forEach(function (triple) {
mappedGraph.add(utils.mapNamespaceTriple(rdf, triple, search, replace));
});
mappedGraph.add(utils.mapNamespaceTriple(rdf, triple, search, replace))
})
return mappedGraph;
};
return mappedGraph
}
/*
* mime type handling
*/
utils.mimeTypeParserMap = {};
utils.mimeTypeParserMap = {}
utils.parse = function (mimeType, data, base, filter, graph) {
utils.parse = function (mimeType, data, callback, base, filter, graph) {
if (!(mimeType in utils.mimeTypeParserMap)) {
return Promise.reject('no parser available for mime type: ' + mimeType);
return Promise.reject('no parser available for mime type: ' + mimeType)
} else {
return rdf.promise.parse(utils.mimeTypeParserMap[mimeType])(data, base, filter, graph);
return utils.mimeTypeParserMap[mimeType](data, callback, base, filter, graph)
}
};
}
utils.mimeTypeSerializerMap = {}
utils.serialize = function (mimeType, graph, callback) {
if (!(mimeType in utils.mimeTypeSerializerMap)) {
return Promise.reject('no serializer available for mime type: ' + mimeType)
} else {
return utils.mimeTypeSerializerMap[mimeType](graph, callback)
}
}
utils.mixin = function (rdf) {
rdf.utils = {};
rdf.utils.filter = utils.filter;
rdf.utils.list = utils.list;
rdf.utils.createSubGraph = utils.createSubGraph.bind(null, rdf);
rdf.utils.createSubGraphByNamedNodeSubject = utils.createSubGraphByNamedNodeSubject.bind(null, rdf);
rdf.utils.splitGraphByNamedNodeSubject = utils.splitGraphByNamedNodeSubject.bind(null, rdf);
rdf.utils.mapNamespaceNode = utils.mapNamespaceNode.bind(null, rdf);
rdf.utils.mapNamespaceTriple = utils.mapNamespaceTriple.bind(null, rdf);
rdf.utils.mapNamespaceGraph = utils.mapNamespaceGraph.bind(null, rdf);
rdf.utils.mimeTypeParserMap = utils.mimeTypeParserMap;
rdf.utils.parse = utils.parse;
};
rdf.utils = {}
rdf.utils.filter = utils.filter
rdf.utils.list = utils.list
rdf.utils.createSubGraph = utils.createSubGraph.bind(null, rdf)
rdf.utils.createSubGraphByNamedNodeSubject = utils.createSubGraphByNamedNodeSubject.bind(null, rdf)
rdf.utils.splitGraphByNamedNodeSubject = utils.splitGraphByNamedNodeSubject.bind(null, rdf)
rdf.utils.mapNamespaceNode = utils.mapNamespaceNode.bind(null, rdf)
rdf.utils.mapNamespaceTriple = utils.mapNamespaceTriple.bind(null, rdf)
rdf.utils.mapNamespaceGraph = utils.mapNamespaceGraph.bind(null, rdf)
rdf.utils.mimeTypeParserMap = utils.mimeTypeParserMap
rdf.utils.mimeTypeSerializerMap = utils.mimeTypeSerializerMap
rdf.utils.parse = utils.parse
rdf.utils.serialize = utils.serialize
}
module.exports = utils.mixin;
module.exports = utils.mixin
{
"name": "rdf-ext",
"version": "0.2.6",
"description": "Implements the RDF-Ext spec plus parsers (JSON-LD, RDF/XML, Turtle), serializers (JSON-LD, N-Triples) and stores (in memory, LDP, SPARQL)",
"version": "0.3.0-rc1",
"description": "Extended Linked Data module",
"main": "rdf-ext.js",
"scripts": {
"prepublish": "node prepublish.js",
"lint": "standard",
"test": "node_modules/.bin/mocha"

@@ -12,34 +12,25 @@ },

"type": "git",
"url": "git://github.com/zazukoians/rdf-ext.git"
"url": "git://github.com/rdf-ext/rdf-ext.git"
},
"keywords": [
"RDF",
"RDF Interfaces",
"JSON-LD",
"Turtle",
"NTriples",
"SPARQL"
"rdf",
"rdf-ext",
"rdf-interfaces"
],
"author": "Thomas Bergwinkl",
"author": "Thomas Bergwinkl <bergi@axolotlfarm.org> (https://www.bergnet.org/people/bergi/card#me)",
"license": "MIT",
"bugs": {
"url": "https://github.com/zazukoians/rdf-ext/issues"
"url": "https://github.com/rdf-ext/rdf-ext/issues"
},
"homepage": "https://github.com/zazukoians/rdf-ext",
"homepage": "https://github.com/rdf-ext/rdf-ext",
"dependencies": {
"jsonld": "0.3.*",
"es6-promise": "2.*",
"n3": "0.4.*",
"rdf-interfaces": "0.1.*",
"xmldom": "0.1.*"
"es6-promise": "^3.0.2",
"rdf-graph-array": "^0.3.0-rc1",
"rdf-store-inmemory": "^0.3.0-rc1"
},
"devDependencies": {
"browserify": "9.*",
"exorcist": "0.1.*",
"mocha": "2.*",
"rdf-test-utils": "0.1.*",
"tbbt-ld": "0.0.2",
"uglify-js": "2.*"
"mocha": "^2.3.3",
"rdf-test-data": "^0.3.0-rc1"
},
"readmeFilename": "README.md"
}
/* global window */
'use strict';
var rdf = require('rdf-graph-array')
var InMemoryStore = require('rdf-store-inmemory')
var mixin = function (rdf, options) {
options = options || {}
var
rdf = require('rdf-interfaces');
require('./lib/environment')(rdf)
rdf.isNode = (typeof process !== 'undefined' && process.versions && process.versions.node);
var mixin = function (options) {
options = options || {};
if (typeof window !== 'undefined') {
window.rdf = rdf;
window.rdf = rdf
}
if (typeof Promise !== 'undefined') {
rdf.Promise = Promise;
} else {
rdf.Promise = null;
}
rdf.defaultRequest = null
rdf.corsProxyRequest = null
rdf.defaultRequest = null;
rdf.corsProxyRequest = null;
rdf.ns = {
type: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
};
require('./lib/utils')(rdf)
require('./lib/utils')(rdf);
if (rdf.isNode) {
require('./lib/utils-node')(rdf);
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
require('./lib/utils-node')(rdf)
} else {
require('./lib/utils-browser')(rdf);
require('./lib/utils-browser')(rdf)
}
if (typeof rdf.Graph === 'undefined') {
rdf.Graph = {};
rdf.createBlankNode = function () {
return new rdf.BlankNode()
}
rdf.Graph.difference = function (a, b) {
var d = rdf.createGraph();
rdf.createNamedNode = function (iri) {
return new rdf.NamedNode(iri)
}
a.forEach(function (at) {
if (!b.some(function (bt) { return at.equals(bt); })) {
d.add(at);
}
});
rdf.createLiteral = function (value, language, datatype) {
return new rdf.Literal(value, language, datatype)
}
return d;
};
rdf.createTriple = function (subject, predicate, object) {
return new rdf.Triple(subject, predicate, object)
}
rdf.Graph.intersection = function (a, b) {
var i = rdf.createGraph();
rdf.createQuad = function (subject, predicate, object, graph) {
return new rdf.Quad(subject, predicate, object, graph)
}
a.forEach(function (at) {
if (b.some(function (bt) { return at.equals(bt); })) {
i.add(at);
}
});
rdf.createGraph = function (triples) {
return new rdf.Graph(triples)
}
return i;
};
// Use InMemoryStore as default store
rdf.createStore = function (options) {
options = options || {}
options.rdf = options.rdf || rdf
rdf.Graph.map = function (graph, callback) {
var result = [];
return new InMemoryStore(options)
}
graph.forEach(function (triple) {
result.push(callback(triple));
});
return result;
};
rdf.Graph.merge = function (a, b) {
var m = rdf.createGraph();
m.addAll(a);
m.addAll(b);
return m;
};
rdf.Graph.toString = function (a) {
var s = '';
a.forEach(function (t) {
s += t.toString() + '\n';
});
return s;
};
var wrappedCreateGraph = rdf.createGraph.bind(rdf);
rdf.createGraphExt = function (triples) {
var graph = wrappedCreateGraph(triples);
graph.difference = rdf.Graph.difference.bind(graph, graph);
graph.intersection = rdf.Graph.intersection.bind(graph, graph);
graph.map = rdf.Graph.map.bind(graph, graph);
graph.toString = rdf.Graph.toString.bind(graph, graph);
if ('replaceMerge' in options && options.replaceMerge) {
graph.merge = rdf.Graph.merge.bind(graph, graph);
}
return graph;
};
Object.defineProperty(rdf, 'createGraph', { value: rdf.createGraphExt });
require('./lib/clownface.js')(rdf);
require('./lib/inmemory-store.js')(rdf);
require('./lib/jsonld-parser.js')(rdf);
require('./lib/jsonld-serializer.js')(rdf);
require('./lib/ldp-store.js')(rdf);
require('./lib/microdata-parser.js')(rdf);
require('./lib/ntriples-serializer.js')(rdf);
require('./lib/promise.js')(rdf);
require('./lib/rdfstore-store.js')(rdf);
require('./lib/rdfxml-parser.js')(rdf);
require('./lib/singlegraph-store.js')(rdf);
require('./lib/sparql-store.js')(rdf);
require('./lib/turtle-parser.js')(rdf);
require('./lib/turtle-serializer.js')(rdf);
return rdf;
};
if (!rdf.isNode) {
mixin();
return rdf
}
mixin(rdf)
module.exports = mixin;
module.exports = rdf
# RDF Interfaces Extension
[![Build Status](https://travis-ci.org/rdf-ext/rdf-ext.svg?branch=master)](https://travis-ci.org/zazukoians/rdf-ext)
[![NPM Version](https://img.shields.io/npm/v/rdf-ext.svg?style=flat)](https://npm.im/rdf-ext)
RDF-Ext provides a JavaScript library for working with RDF & Linked Data. It provides read/write support for the most widely used RDF data formats like:

@@ -71,22 +75,27 @@

var data = '<http://example.org/#s> <http://example.org/#p> <http://example.org/#o>.';
```javascript
var data = '<http://example.org/#s> <http://example.org/#p> <http://example.org/#o>.';
rdf.parseTurtle(data, function (graph) {
console.log(graph.length);
});
rdf.parseTurtle(data, function (graph) {
console.log(graph.length);
});
```
Use a `LdpStore` instance to read the http://dbpedia.org/resource/RDF graph from the turtle resource on DBpedia and
Use a [`LdpStore`](http://npm.im/rdf-store-ldp) instance to read the http://dbpedia.org/resource/RDF graph from the turtle resource on DBpedia and
print the first rdfs:label object value:
var store = new rdf.LdpStore();
```javascript
var LdpStore = require('rdf-store-ldp');
var store = new LdpStore(rdf);
store.match(
'http://dbpedia.org/data/RDF.ttl',
'http://dbpedia.org/resource/RDF',
'http://www.w3.org/2000/01/rdf-schema#label',
null,
function (graph) {
console.log(graph.toArray()[0].object.toString());
}
);
store.match(
'http://dbpedia.org/data/RDF.ttl',
'http://dbpedia.org/resource/RDF',
'http://www.w3.org/2000/01/rdf-schema#label',
null,
function (graph) {
console.log(graph.toArray()[0].object.toString());
}
);
```

@@ -109,2 +118,4 @@ ### RDFJS Primer

Link: [rdf-store-inmemory](http://npm.im/rdf-store-inmemory)
A simple in-memory triple store implementation.

@@ -117,2 +128,4 @@ Cross graph read operations are supported by using `undefined` as graph IRI.

Link: [rdf-store-ldp](http://npm.im/rdf-store-ldp)
Store implementation to access graphs via a RESTful [LDP](http://www.w3.org/TR/ldp/) interface.

@@ -136,2 +149,4 @@ The constructor accepts a single `options` parameters.

Link: [rdf-store-rdfstore-js](http://npm.im/rdf-store-rdfstore-js)
Store based on [rdfstore-js](https://github.com/antoniogarrote/rdfstore-js).

@@ -142,2 +157,4 @@ The constructor requires a rdfstore-js object parameter that will be wrapped.

Link: [rdf-store-singlegraph](http://npm.im/rdf-store-singlegraph)
In memory triple store using a single graph for all named graphs.

@@ -148,4 +165,6 @@ Sometimes usefull for testing.

Store implementation to access graphs via [SPARQL 1.1 Graph Store HTTP Protocol](http://www.w3.org/TR/sparql11-http-rdf-update/) interface. This requires an external triple store.
Link: [rdf-store-sparql](http://npm.im/rdf-store-sparql)
Store implementation to access graphs using the [SPARQL 1.1 Protocol](http://www.w3.org/TR/sparql11-protocol/). This requires an external triple store.
The constructor accepts a single `options` parameters.

@@ -171,2 +190,6 @@

#### See also
- FileStore ([rdf-store-fs](http://npm.im/rdf-store-fs))
- AbstractStore ([rdf-store-abstract](http://npm.im/rdf-store-abstract))
### Parser

@@ -173,0 +196,0 @@

@@ -1,163 +0,11 @@

(function (root, factory) {
if (typeof module !== 'undefined' && module.exports) {
global.Promise = require('es6-promise').Promise;
/* global describe */
var tests = [
require('./environment'),
require('./utils')
]
var
assert = require('assert'),
fs = require('fs'),
path = require('path'),
rdf = require('../rdf-ext')(),
utils = require('rdf-test-utils')(rdf);
var tests = {
clownface: require('./clownface'),
graph: require('./graph'),
ldpStore: require('./ldp-store'),
microdataSuite: require('./microdata-suite'),
parsers: require('./parsers'),
rdfaSuite: require('./rdfa-suite'),
serializers: require('./serializers'),
store: require('./store'),
utils: require('./utils')
};
var readFile = function (filename) {
return new Promise(function (resolve) {
resolve(fs.readFileSync(path.join(__dirname, filename)).toString());
});
};
module.exports = factory(assert, rdf, readFile, utils, tests);
} else {
var readFile = function (filename) {
return new Promise(function (resolve) {
root.rdf.defaultRequest('GET', filename, {}, null, function (status, headers, content) {
resolve(content);
});
});
};
factory(root.assert, root.rdf, readFile, root.rdf.testUtils(root.rdf), root.tests);
}
})(this, function (assert, rdf, readFile, utils, tests) {
var buildCardGraph = function () {
var graph = rdf.createGraph();
var cardNode = rdf.createNamedNode('https://www.example.com/john/card#me');
graph.add(rdf.createTriple(
cardNode,
rdf.createNamedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
rdf.createNamedNode('http://xmlns.com/foaf/0.1/Person')));
graph.add(rdf.createTriple(
cardNode,
rdf.createNamedNode('http://xmlns.com/foaf/0.1/name'),
rdf.createLiteral('John Smith', 'en')));
var keyNode = rdf.createBlankNode();
graph.add(rdf.createTriple(
cardNode,
rdf.createNamedNode('http://www.w3.org/ns/auth/cert#key'),
keyNode));
graph.add(rdf.createTriple(
keyNode,
rdf.createNamedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
rdf.createNamedNode('http://www.w3.org/ns/auth/cert#RSAPublicKey')));
graph.add(rdf.createTriple(
keyNode,
rdf.createNamedNode('http://www.w3.org/ns/auth/cert#exponent'),
rdf.createLiteral('65537', null, rdf.createNamedNode('http://www.w3.org/2001/XMLSchema#integer'))));
graph.add(rdf.createTriple(
keyNode,
rdf.createNamedNode('http://www.w3.org/ns/auth/cert#modulus'),
rdf.createLiteral('abcdef', null, rdf.createNamedNode('http://www.w3.org/2001/XMLSchema#hexBinary'))));
return graph;
};
var ctx = {};
ctx.cardGraph = buildCardGraph();
//TODO:
//ctx.listGraph = buildListGraph();
ctx.nodes = {
blank1: rdf.createBlankNode(),
blank2: rdf.createBlankNode(),
blank3: rdf.createBlankNode(),
blank4: rdf.createBlankNode(),
literal1: rdf.createLiteral('example1'),
literal2: rdf.createLiteral('example2'),
literal3: rdf.createLiteral('example3'),
literal4: rdf.createLiteral('example4'),
named1: rdf.createNamedNode('http://example.org/1'),
named2: rdf.createNamedNode('http://example.org/2'),
named3: rdf.createNamedNode('http://example.org/3'),
named4: rdf.createNamedNode('http://example.org/4')
};
ctx.triples = {
t1: rdf.createTriple(ctx.nodes.named1, ctx.nodes.named3, ctx.nodes.named2),
t2: rdf.createTriple(ctx.nodes.named2, ctx.nodes.named4, ctx.nodes.blank1),
t3: rdf.createTriple(ctx.nodes.blank1, ctx.nodes.named3, ctx.nodes.blank2),
t4: rdf.createTriple(ctx.nodes.blank2, ctx.nodes.named4, ctx.nodes.named1)
};
describe('core', function () {
tests.graph(assert, rdf, readFile, utils, ctx);
tests.store(assert, rdf, readFile, utils, ctx);
});
describe('ext', function() {
before(function (done) {
var parser = new rdf.promise.Parser(new rdf.TurtleParser());
readFile('support/list.nt')
.then(function (list) {
return parser.parse(list, 'https://example.com/list')
.then(function (graph) {
ctx.listGraph = graph;
});
})
.then(function () {
return readFile('support/tbbt.nt')
.then(function (tbbt) {
return parser.parse(tbbt)
})
.then(function (graph) {
ctx.tbbtGraph = graph;
return rdf.utils.splitGraphByNamedNodeSubject(graph)
.then(function (store) {
ctx.tbbtStore = store;
});
});
})
.then(function () {
done();
})
.catch(function (error) {
done(error);
});
});
tests.parsers(assert, rdf, readFile, utils, ctx);
tests.serializers(assert, rdf, readFile, utils, ctx);
tests.ldpStore(assert, rdf, readFile, utils, ctx);
tests.utils(assert, rdf, readFile, utils, ctx);
tests.clownface(assert, rdf, readFile, utils, ctx);
});
describe('test suites', function () {
tests.microdataSuite(assert, rdf, readFile, utils, ctx);
if (rdf.isNode) {
tests.rdfaSuite(assert, rdf, readFile, utils, ctx);
}
});
});
describe('RDF-Ext', function () {
tests.forEach(function (test) {
test()
})
})

@@ -1,187 +0,164 @@

(function (root, factory) {
if (typeof module !== 'undefined' && module.exports) {
module.exports = factory();
} else {
root.tests = root.tests || {};
root.tests.utils = factory();
}
})(this, function () {
return function (assert, rdf, readFile, utils, ctx) {
describe('utils', function () {
describe('filter', function () {
it('namedNode should filter NamedNode nodes', function () {
assert.equal(rdf.utils.filter.namedNode(rdf.createNamedNode('http://example.org')), true);
assert.equal(rdf.utils.filter.namedNode(rdf.createBlankNode()), false);
assert.equal(rdf.utils.filter.namedNode(rdf.createLiteral('test')), false);
});
/* global before, describe, it */
var assert = require('assert')
var rdf = require('../rdf-ext')
var testData = require('rdf-test-data')
it('blankNode should filter BlankNode nodes', function () {
assert.equal(rdf.utils.filter.blankNode(rdf.createNamedNode('http://example.org')), false);
assert.equal(rdf.utils.filter.blankNode(rdf.createBlankNode()), true);
assert.equal(rdf.utils.filter.blankNode(rdf.createLiteral('test')), false);
});
module.exports = function (ctx) {
describe('utils', function () {
var tbbtGraph
it('literal should filter Literal nodes', function () {
assert.equal(rdf.utils.filter.literal(rdf.createNamedNode('http://example.org')), false);
assert.equal(rdf.utils.filter.literal(rdf.createBlankNode()), false);
assert.equal(rdf.utils.filter.literal(rdf.createLiteral('test')), true);
});
before(function (done) {
testData.tbbtGraph().then(function (graph) {
tbbtGraph = graph
it('namedNodeSubject should exclude NamedNodes with different subject', function () {
var blankNodeTriple = rdf.createTriple(
rdf.createBlankNode(),
rdf.createNamedNode('http://example.org'),
rdf.createLiteral('test'));
done()
}).catch(function (error) {
done(error)
})
})
var subject = rdf.createNamedNode('http://example.org/subject');
describe('filter', function () {
it('namedNode should filter NamedNode nodes', function () {
assert.equal(rdf.utils.filter.namedNode(rdf.createNamedNode('http://example.org')), true)
assert.equal(rdf.utils.filter.namedNode(rdf.createBlankNode()), false)
assert.equal(rdf.utils.filter.namedNode(rdf.createLiteral('test')), false)
})
var sameSubjectTriple = rdf.createTriple(
subject,
rdf.createNamedNode('http://example.org'),
rdf.createLiteral('test'));
it('blankNode should filter BlankNode nodes', function () {
assert.equal(rdf.utils.filter.blankNode(rdf.createNamedNode('http://example.org')), false)
assert.equal(rdf.utils.filter.blankNode(rdf.createBlankNode()), true)
assert.equal(rdf.utils.filter.blankNode(rdf.createLiteral('test')), false)
})
var otherSubjectTriple = rdf.createTriple(
rdf.createNamedNode('http://example.org/otherSubject'),
rdf.createNamedNode('http://example.org'),
rdf.createLiteral('test'));
it('literal should filter Literal nodes', function () {
assert.equal(rdf.utils.filter.literal(rdf.createNamedNode('http://example.org')), false)
assert.equal(rdf.utils.filter.literal(rdf.createBlankNode()), false)
assert.equal(rdf.utils.filter.literal(rdf.createLiteral('test')), true)
})
assert.equal(rdf.utils.filter.namedNodeSubject(subject)(blankNodeTriple), true);
assert.equal(rdf.utils.filter.namedNodeSubject(subject)(sameSubjectTriple), true);
assert.equal(rdf.utils.filter.namedNodeSubject(subject)(otherSubjectTriple), false);
});
});
it('namedNodeSubject should exclude NamedNodes with different subject', function () {
var blankNodeTriple = rdf.createTriple(
rdf.createBlankNode(),
rdf.createNamedNode('http://example.org'),
rdf.createLiteral('test'))
describe('list', function () {
it('should list subjects', function () {
var subject = rdf.createNamedNode('http://localhost:8080/data/person/amy-farrah-fowler');
var subjects = rdf.utils.list.subjects(ctx.tbbtGraph.match(subject));
var subject = rdf.createNamedNode('http://example.org/subject')
assert.equal(subjects.length, 1);
assert.equal(subjects[0].toString(), subject.toString());
});
var sameSubjectTriple = rdf.createTriple(
subject,
rdf.createNamedNode('http://example.org'),
rdf.createLiteral('test'))
it('should list subjects with filter', function () {
var subject = rdf.createNamedNode('http://localhost:8080/data/person/amy-farrah-fowler');
var subjects = rdf.utils.list.subjects(ctx.tbbtGraph, function (node) { return node.equals(subject); });
var otherSubjectTriple = rdf.createTriple(
rdf.createNamedNode('http://example.org/otherSubject'),
rdf.createNamedNode('http://example.org'),
rdf.createLiteral('test'))
assert.equal(subjects.length, 1);
assert.equal(subjects[0].toString(), subject.toString());
});
assert.equal(rdf.utils.filter.namedNodeSubject(subject)(blankNodeTriple), true)
assert.equal(rdf.utils.filter.namedNodeSubject(subject)(sameSubjectTriple), true)
assert.equal(rdf.utils.filter.namedNodeSubject(subject)(otherSubjectTriple), false)
})
})
it('should list predicates', function () {
var predicate = rdf.createNamedNode('http://schema.org/knows');
var predicates = rdf.utils.list.predicates(ctx.tbbtGraph.match(null, predicate));
describe('list', function () {
it('should list subjects', function () {
var subject = rdf.createNamedNode('http://localhost:8080/data/person/amy-farrah-fowler')
var subjects = rdf.utils.list.subjects(tbbtGraph.match(subject))
assert.equal(predicates.length, 1);
assert.equal(predicates[0].toString(), predicate.toString());
});
assert.equal(subjects.length, 1)
assert.equal(subjects[0].toString(), subject.toString())
})
it('should list predicates with filter', function () {
var predicate = rdf.createNamedNode('http://schema.org/knows');
var predicates = rdf.utils.list.predicates(ctx.tbbtGraph, function (node) { return node.equals(predicate); });
it('should list subjects with filter', function () {
var subject = rdf.createNamedNode('http://localhost:8080/data/person/amy-farrah-fowler')
var subjects = rdf.utils.list.subjects(tbbtGraph, function (node) { return node.equals(subject) })
assert.equal(predicates.length, 1);
assert.equal(predicates[0].toString(), predicate.toString());
});
assert.equal(subjects.length, 1)
assert.equal(subjects[0].toString(), subject.toString())
})
it('should list objects', function () {
var object = rdf.createLiteral('microbiologist');
var objects = rdf.utils.list.objects(ctx.tbbtGraph.match(null, null, object));
it('should list predicates', function () {
var predicate = rdf.createNamedNode('http://schema.org/knows')
var predicates = rdf.utils.list.predicates(tbbtGraph.match(null, predicate))
assert.equal(objects.length, 1);
assert.equal(objects[0].toString(), object.toString());
});
assert.equal(predicates.length, 1)
assert.equal(predicates[0].toString(), predicate.toString())
})
it('should list objects with filter', function () {
var object = rdf.createLiteral('microbiologist');
var objects = rdf.utils.list.objects(ctx.tbbtGraph, function (node) { return node.equals(object); });
it('should list predicates with filter', function () {
var predicate = rdf.createNamedNode('http://schema.org/knows')
var predicates = rdf.utils.list.predicates(tbbtGraph, function (node) { return node.equals(predicate) })
assert.equal(objects.length, 1);
assert.equal(objects[0].toString(), object.toString());
});
});
assert.equal(predicates.length, 1)
assert.equal(predicates[0].toString(), predicate.toString())
})
describe('subgraph', function () {
it('should create a subgraph', function () {
var tbbtGraphNg = rdf.createGraph();
it('should list objects', function () {
var object = rdf.createLiteral('microbiologist')
var objects = rdf.utils.list.objects(tbbtGraph.match(null, null, object))
// create a copy of tbbt + a not linked triple
tbbtGraphNg.addAll(ctx.tbbtGraph);
tbbtGraphNg.add(rdf.createTriple(
rdf.createNamedNode('http://example.org/subject'),
rdf.createNamedNode('http://example.org/predicate'),
rdf.createLiteral('object')));
assert.equal(objects.length, 1)
assert.equal(objects[0].toString(), object.toString())
})
var subgraph = rdf.utils.createSubGraph(
tbbtGraphNg,
rdf.createNamedNode('http://localhost:8080/data/person/amy-farrah-fowler'));
it('should list objects with filter', function () {
var object = rdf.createLiteral('microbiologist')
var objects = rdf.utils.list.objects(tbbtGraph, function (node) { return node.equals(object) })
assert.equal(subgraph.length, ctx.tbbtGraph.length);
});
assert.equal(objects.length, 1)
assert.equal(objects[0].toString(), object.toString())
})
})
it('should create a subgraph with filter', function () {
var subject = rdf.createNamedNode('http://localhost:8080/data/person/amy-farrah-fowler');
describe('subgraph', function () {
it('should create a subgraph', function () {
var tbbtGraphNg = rdf.createGraph()
var subgraph = rdf.utils.createSubGraph(ctx.tbbtGraph, subject, function (triple) {
return triple.predicate.equals(rdf.createNamedNode('http://schema.org/knows'));
});
// create a copy of tbbt + a not linked triple
tbbtGraphNg.addAll(tbbtGraph)
tbbtGraphNg.add(rdf.createTriple(
rdf.createNamedNode('http://example.org/subject'),
rdf.createNamedNode('http://example.org/predicate'),
rdf.createLiteral('object')))
assert.equal(subgraph.length, 61);
assert.equal(rdf.utils.list.predicates(subgraph).length, 1);
});
var subgraph = rdf.utils.createSubGraph(
tbbtGraphNg,
rdf.createNamedNode('http://localhost:8080/data/person/amy-farrah-fowler'))
it('should create a subgraph by NamedNode subject', function () {
var subgraph = rdf.utils.createSubGraphByNamedNodeSubject(
ctx.tbbtGraph,
rdf.createNamedNode('http://localhost:8080/data/person/amy-farrah-fowler'));
assert.equal(subgraph.length, tbbtGraph.length)
})
assert.equal(subgraph.length, 12);
});
it('should create a subgraph with filter', function () {
var subject = rdf.createNamedNode('http://localhost:8080/data/person/amy-farrah-fowler')
it('should split a graph by NamedNode subject into a store', function (done) {
rdf.utils.splitGraphByNamedNodeSubject(ctx.tbbtGraph)
.then(function (store) {
var graphCount = 0;
var subgraph = rdf.utils.createSubGraph(tbbtGraph, subject, function (triple) {
return triple.predicate.equals(rdf.createNamedNode('http://schema.org/knows'))
})
store.store().forEach(function () {
graphCount++;
});
assert.equal(subgraph.length, 61)
assert.equal(rdf.utils.list.predicates(subgraph).length, 1)
})
assert.equal(graphCount, 9);
})
.then(function () {
done();
})
.catch(function (error) {
done(error);
});
});
});
it('should create a subgraph by NamedNode subject', function () {
var subgraph = rdf.utils.createSubGraphByNamedNodeSubject(
tbbtGraph,
rdf.createNamedNode('http://localhost:8080/data/person/amy-farrah-fowler'))
describe('mimeType', function () {
it('should find a parser by mime type and use it to parse data', function (done) {
rdf.utils.parse('application/n-triples', ctx.tbbtGraph.toString())
.then(function (graph) {
assert.equal(graph.length, 126);
})
.then(function () {
done();
})
.catch(function (error) {
done(error);
});
});
assert.equal(subgraph.length, 12)
})
it('should throw an error if the mime type is not in the map', function () {
rdf.utils.parse('image/jpeg', '')
.then(function (graph) {
done('no error thrown');
})
.catch(function (error) {
done();
});
});
});
});
};
});
it('should split a graph by NamedNode subject into a store', function (done) {
rdf.utils.splitGraphByNamedNodeSubject(tbbtGraph)
.then(function (store) {
assert.equal(Object.keys(store.graphs).length, 9)
})
.then(function () {
done()
})
.catch(function (error) {
done(error)
})
})
})
})
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc