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

n32s

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

n32s - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

Changes

239

js/index.js
#!/usr/bin/env node
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

@@ -63,6 +40,3 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }

Object.defineProperty(exports, "__esModule", { value: true });
var N3 = __importStar(require("n3"));
var parser_1 = require("./parser");
var XSD = 'http://www.w3.org/2001/XMLSchema#';
var RDFS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
if (process.argv.length != 3) {

@@ -82,4 +56,4 @@ console.log("usage: ".concat(process.argv[1], " n3-file"));

store = _a.sent();
graph = makeGraph(store, N3.DataFactory.defaultGraph());
n3s = writeGraph(graph);
graph = (0, parser_1.makeGraph)(store);
n3s = (0, parser_1.writeGraph)(graph);
console.log(n3s);

@@ -91,210 +65,1 @@ return [2 /*return*/];

}
function pref(type, value) {
return type + value;
}
function writeGraph(graph) {
var result = [];
graph.value.forEach(function (pso) {
var value = writePSO(pso);
result.push(value);
});
return result.join("\n");
}
;
function writePSO(pso) {
var subject = writeTerm(pso.subject);
var predicate = writeTerm(pso.predicate);
var object = writeTerm(pso.object);
return "".concat(predicate, "(").concat(subject, ",").concat(object, ").");
}
function writeTerm(term) {
if (term.type === 'NamedNode') {
return "'<".concat(term.value, ">'");
}
else if (term.type === 'Literal') {
if (term.datatype === pref(XSD, 'string')) {
return "'".concat(term.value, "'");
}
else if (term.datatype === pref(XSD, 'integer')) {
return term.value;
}
else if (term.datatype === pref(XSD, 'boolean')) {
return term.value;
}
else {
return "literal('".concat(term.value, ",'{").concat(term.datatype, "})");
}
}
else if (term.type === 'BlankNode') {
return "'".concat(term.value, "'");
}
else if (term.type === 'List') {
var result_1 = [];
term.value.forEach(function (li) {
result_1.push(writeTerm(li));
});
return '[' + result_1.join(",") + ']';
}
else if (term.type === 'Graph') {
var result_2 = [];
term.value.forEach(function (gi) {
var value = writePSO(gi);
result_2.push(value.replace(/\.$/, ''));
});
return '(' + result_2.join(",") + ')';
}
return 'x';
}
function makeGraph(store, graph) {
var result = {
type: 'Graph',
value: [],
datatype: null
};
// First process the named nodes...
store.forEach(function (quad) {
if (quad.subject.termType === 'NamedNode'
&& !isGraphLike(quad, graph)) {
var subject = parseTerm(quad.subject, store);
var predicate = parseTerm(quad.predicate, store);
var object = parseTerm(quad.object, store);
result.value.push({
type: 'PSO',
subject: subject,
predicate: predicate,
object: object
});
}
}, null, null, null, graph);
// Next process the explicit bnodes...
store.forEach(function (quad) {
if (quad.subject.termType === 'BlankNode'
&& !isListLike(quad)
&& !isGraphLike(quad, graph)) {
var subject = parseTerm(quad.subject, store);
var predicate = parseTerm(quad.predicate, store);
var object = parseTerm(quad.object, store);
result.value.push({
type: 'PSO',
subject: subject,
predicate: predicate,
object: object
});
}
}, null, null, null, graph);
return result;
}
function parseTerm(term, store) {
if (term.termType === 'NamedNode') {
if (term.value === pref(RDFS, 'nil')) {
return { type: 'List', value: [] };
}
else {
return { type: 'NamedNode', value: term.value };
}
}
else if (term.termType === 'Literal') {
return {
type: 'Literal',
value: term.value,
datatype: term.datatypeString
};
}
else if (term.termType === 'BlankNode') {
if (isList(term, store)) {
return makeList(term, store);
}
else if (isGraph(term, store)) {
return makeGraph(store, term);
}
else {
var genid = makeGenId(term);
return {
type: 'BlankNode',
value: genid
};
}
}
else {
return {
type: 'BlankNode',
value: 'unknown'
};
}
}
function makeGenId(term) {
var value = term.value.replace(/^.*(_|\.)/, '');
return "_:".concat(value);
}
function isGraphLike(quad, graph) {
if (quad.graph.id === graph.id) {
return false;
}
else {
return true;
}
}
function isListLike(quad) {
if (quad.predicate.value === pref(RDFS, 'first') ||
quad.predicate.value === pref(RDFS, 'rest')) {
return true;
}
else {
return false;
}
}
function isList(term, store) {
var first = store.getQuads(term, pref(RDFS, 'first'), null, null);
var rest = store.getQuads(term, pref(RDFS, 'rest'), null, null);
if (first.length == 1 || rest.length == 1) {
return true;
}
else {
return false;
}
}
function isGraph(term, store) {
var graph = store.getQuads(null, null, null, term);
if (graph.length == 0) {
return false;
}
else {
return true;
}
}
function makeList(term, store) {
var termList = [];
var searchTerm = term;
var brake = false;
do {
var first = store.getQuads(searchTerm, pref(RDFS, 'first'), null, null);
var rest = store.getQuads(searchTerm, pref(RDFS, 'rest'), null, null);
if (first.length == 0) {
if (rest.length == 0 || rest.length != 1) {
brake = true;
}
else {
brake = true;
}
}
else if (first.length != 1 || rest.length != 1) {
brake = true;
}
else if (first[0].object.value === pref(RDFS, 'nil')) {
brake = true;
}
else {
var termValue = parseTerm(first[0].object, store);
termList.push(termValue);
if (rest[0].object.value === pref(RDFS, 'nil')) {
brake = true;
}
else {
searchTerm = rest[0].object;
}
}
first.forEach(function (quad) { store.removeQuad(quad); });
rest.forEach(function (quad) { store.removeQuad(quad); });
} while (!brake);
return { type: 'List', value: termList };
}
{
"name": "n32s",
"version": "0.0.1",
"version": "0.0.2",
"description": "Notation3 to N3S precompiler",

@@ -5,0 +5,0 @@ "main": "js/index.js",

#!/usr/bin/env node
import * as N3 from 'n3';
import { parseN3File } from './parser';
import { parseN3File, makeGraph, writeGraph } from './parser';
const XSD = 'http://www.w3.org/2001/XMLSchema#';
const RDFS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
if (process.argv.length != 3) {

@@ -20,280 +17,5 @@ console.log(`usage: ${process.argv[1]} n3-file`);

const store = await parseN3File(path);
const graph = makeGraph(store, N3.DataFactory.defaultGraph());
const graph = makeGraph(store);
const n3s = writeGraph(graph);
console.log(n3s);
}
type IPSO = {
type: 'PSO',
subject: ITerm ,
predicate: ITerm ,
object: ITerm ,
};
type ITerm = INamedNode | IBlankNode | ILiteral | IGraph | IList;
type INamedNode = {
type: 'NamedNode';
value: string;
datatype: null;
};
type IBlankNode = {
type: 'BlankNode';
value: string;
datatype: null;
};
type ILiteral = {
type: 'Literal';
value: string;
datatype: string;
};
type IList = {
type: 'List';
value: ITerm[]
datatype: null;
};
type IGraph = {
type: 'Graph';
value: IPSO[];
datatype: null;
};
function pref(type: string, value: string) : string {
return type + value;
}
function writeGraph(graph: IGraph) : string {
const result : string[] = [];
graph.value.forEach( (pso) => {
const value = writePSO(pso);
result.push(value);
});
return result.join("\n");
};
function writePSO(pso: IPSO) : string {
const subject = writeTerm(pso.subject);
const predicate = writeTerm(pso.predicate);
const object = writeTerm(pso.object);
return `${predicate}(${subject},${object}).`;
}
function writeTerm(term: ITerm) : string {
if (term.type === 'NamedNode') {
return `'<${term.value}>'`;
}
else if (term.type === 'Literal') {
if (term.datatype === pref(XSD,'string')) {
return `'${term.value}'`;
}
else if (term.datatype === pref(XSD,'integer')) {
return term.value;
}
else if (term.datatype === pref(XSD,'boolean')) {
return term.value;
}
else {
return `literal('${term.value},'{${term.datatype}})`;
}
}
else if (term.type === 'BlankNode') {
return `'${term.value}'`;
}
else if (term.type === 'List') {
const result : string[] = [];
term.value.forEach( (li) => {
result.push(writeTerm(li));
});
return '[' + result.join(",") + ']';
}
else if (term.type === 'Graph') {
const result : string[] = [];
term.value.forEach( (gi) => {
const value = writePSO(gi);
result.push(value.replace(/\.$/,''));
});
return '(' + result.join(",") + ')';
}
return 'x';
}
function makeGraph(store: N3.Store, graph: N3.Term) : IGraph {
const result : IGraph = {
type: 'Graph',
value: [] as IPSO[],
datatype: null
};
// First process the named nodes...
store.forEach((quad) => {
if (quad.subject.termType === 'NamedNode'
&& !isGraphLike(quad,graph)) {
let subject = parseTerm(quad.subject, store);
let predicate = parseTerm(quad.predicate, store);
let object = parseTerm(quad.object, store);
result.value.push({
type: 'PSO',
subject: subject,
predicate: predicate,
object: object
} as IPSO);
}
}, null, null, null, graph);
// Next process the explicit bnodes...
store.forEach((quad) => {
if (quad.subject.termType === 'BlankNode'
&& !isListLike(quad)
&& !isGraphLike(quad,graph)) {
let subject = parseTerm(quad.subject, store);
let predicate = parseTerm(quad.predicate, store);
let object = parseTerm(quad.object, store);
result.value.push({
type: 'PSO',
subject: subject,
predicate: predicate,
object: object
} as IPSO);
}
}, null, null, null, graph);
return result;
}
function parseTerm(term: N3.Term, store: N3.Store) : ITerm {
if (term.termType === 'NamedNode') {
if (term.value === pref(RDFS,'nil')) {
return { type: 'List' , value: [] as ITerm[] } as IList;
}
else {
return { type: 'NamedNode' , value: term.value} as INamedNode;
}
}
else if (term.termType === 'Literal') {
return {
type: 'Literal',
value: term.value ,
datatype: term.datatypeString
} as ILiteral;
}
else if (term.termType === 'BlankNode') {
if (isList(term,store)) {
return makeList(term,store);
}
else if (isGraph(term,store)) {
return makeGraph(store,term);
}
else {
const genid = makeGenId(term);
return {
type: 'BlankNode',
value: genid
} as IBlankNode;
}
}
else {
return {
type: 'BlankNode',
value: 'unknown'
} as IBlankNode;
}
}
function makeGenId(term: N3.Term) : string {
const value = term.value.replace(/^.*(_|\.)/,'') ;
return `_:${value}`;
}
function isGraphLike(quad: N3.Quad, graph: N3.Term) : boolean {
if (quad.graph.id === graph.id) {
return false;
}
else {
return true;
}
}
function isListLike(quad: N3.Quad) : boolean {
if (quad.predicate.value === pref(RDFS,'first') ||
quad.predicate.value === pref(RDFS,'rest')) {
return true;
}
else {
return false;
}
}
function isList(term: N3.Term, store: N3.Store) : boolean {
const first = store.getQuads(term,pref(RDFS,'first'),null,null);
const rest = store.getQuads(term,pref(RDFS,'rest'),null,null);
if (first.length == 1 || rest.length == 1) {
return true;
}
else {
return false;
}
}
function isGraph(term: N3.Term, store: N3.Store) : boolean {
const graph = store.getQuads(null, null, null, term);
if (graph.length == 0) {
return false;
}
else {
return true;
}
}
function makeList(term: N3.Term, store: N3.Store) : IList {
let termList : ITerm[] = [];
let searchTerm = term;
let brake = false;
do {
const first = store.getQuads(searchTerm,pref(RDFS,'first'),null,null);
const rest = store.getQuads(searchTerm,pref(RDFS,'rest'),null,null);
if (first.length == 0) {
if (rest.length == 0 || rest.length != 1) {
brake = true;
}
else {
brake = true;
}
}
else if (first.length != 1 || rest.length != 1) {
brake = true;
}
else if (first[0].object.value === pref(RDFS,'nil')) {
brake = true;
}
else {
const termValue = parseTerm(first[0].object, store);
termList.push(termValue);
if (rest[0].object.value === pref(RDFS,'nil')) {
brake = true;
}
else {
searchTerm = rest[0].object;
}
}
first.forEach( (quad) => { store.removeQuad(quad) });
rest.forEach( (quad) => { store.removeQuad(quad) });
} while (!brake);
return { type: 'List', value: termList } as IList;
}

@@ -5,11 +5,53 @@ import * as N3 from 'n3';

const XSD = 'http://www.w3.org/2001/XMLSchema#';
const RDFS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
const logger = getLogger();
export {
parseN3File,
parseN3,
serializeN3Store
export type IPSO = {
type: 'PSO',
subject: ITerm ,
predicate: ITerm ,
object: ITerm ,
};
async function parseN3File(file: string) : Promise<N3.Store> {
export type ITerm = INamedNode | IBlankNode | ILiteral | IVariable | IGraph | IList;
export type INamedNode = {
type: 'NamedNode';
value: string;
datatype: null;
};
export type IBlankNode = {
type: 'BlankNode';
value: string;
datatype: null;
};
export type ILiteral = {
type: 'Literal';
value: string;
datatype: string;
};
export type IVariable = {
type: 'Variable';
value: string;
datatype: null;
};
export type IList = {
type: 'List';
value: ITerm[]
datatype: null;
};
export type IGraph = {
type: 'Graph';
value: IPSO[];
datatype: null;
};
export async function parseN3File(file: string) : Promise<N3.Store> {
logger.debug(`parsing: ${file}`);

@@ -20,4 +62,4 @@ const n3 = fs.readFileSync(file, { encoding: "utf8"});

async function parseN3(n3: string) : Promise<N3.Store> {
const parser = new N3.Parser({ format: 'Notation3' });
export async function parseN3(n3: string) : Promise<N3.Store> {
const parser = new N3.Parser({ format: 'text/n3' });

@@ -44,3 +86,3 @@ const store = new N3.Store();

function serializeN3Store(store: N3.Store) : void {
export function serializeN3Store(store: N3.Store) : void {
store.forEach( (quad) => {

@@ -54,2 +96,249 @@ const subject = quad.subject.value;

}, null, null, null, null);
}
function pref(type: string, value: string) : string {
return type + value;
}
export function writeGraph(graph: IGraph) : string {
const result : string[] = [];
graph.value.forEach( (pso) => {
const value = writePSO(pso);
result.push(value);
});
return result.join("\n");
};
function writePSO(pso: IPSO) : string {
const subject = writeTerm(pso.subject);
const predicate = writeTerm(pso.predicate);
const object = writeTerm(pso.object);
return `${predicate}(${subject},${object}).`;
}
function writeTerm(term: ITerm) : string {
if (term.type === 'NamedNode') {
return `'<${term.value}>'`;
}
else if (term.type === 'Literal') {
if (term.datatype === pref(XSD,'string')) {
return `'${term.value}'`;
}
else if (term.datatype === pref(XSD,'integer')) {
return term.value;
}
else if (term.datatype === pref(XSD,'boolean')) {
return term.value;
}
else {
return `literal('${term.value},'${term.datatype})`;
}
}
else if (term.type === 'BlankNode') {
return `'${term.value}'`;
}
else if (term.type === 'Variable') {
return `${term.value}`;
}
else if (term.type === 'List') {
const result : string[] = [];
term.value.forEach( (li) => {
result.push(writeTerm(li));
});
return '[' + result.join(",") + ']';
}
else if (term.type === 'Graph') {
const result : string[] = [];
term.value.forEach( (gi) => {
const value = writePSO(gi);
result.push(value.replace(/\.$/,''));
});
return '(' + result.join(",") + ')';
}
return 'x';
}
export function makeGraph(store: N3.Store, graph: N3.Term = N3.DataFactory.defaultGraph()) : IGraph {
const result : IGraph = {
type: 'Graph',
value: [] as IPSO[],
datatype: null
};
// First process the named nodes...
store.forEach((quad) => {
const termType = '' + quad.subject.termType;
if ((termType === 'NamedNode' || termType === 'Literal' || termType === 'Variable')
&& !isGraphLike(quad,graph)) {
let subject = parseTerm(quad.subject, store);
let predicate = parseTerm(quad.predicate, store);
let object = parseTerm(quad.object, store);
result.value.push({
type: 'PSO',
subject: subject,
predicate: predicate,
object: object
} as IPSO);
}
}, null, null, null, graph);
// Next process the explicit bnodes...
store.forEach((quad) => {
const termType = '' + quad.subject.termType;
if (termType === 'BlankNode'
&& !isListLike(quad)
&& !isGraphLike(quad,graph)) {
let subject = parseTerm(quad.subject, store);
let predicate = parseTerm(quad.predicate, store);
let object = parseTerm(quad.object, store);
result.value.push({
type: 'PSO',
subject: subject,
predicate: predicate,
object: object
} as IPSO);
}
}, null, null, null, graph);
return result;
}
function parseTerm(term: N3.Term, store: N3.Store) : ITerm {
if (term.termType === 'NamedNode') {
if (term.value === pref(RDFS,'nil')) {
return { type: 'List' , value: [] as ITerm[] } as IList;
}
else {
return { type: 'NamedNode' , value: term.value} as INamedNode;
}
}
else if (term.termType === 'Literal') {
return {
type: 'Literal',
value: term.value ,
datatype: term.datatypeString
} as ILiteral;
}
else if (term.termType === 'BlankNode') {
if (isList(term,store)) {
return makeList(term,store);
}
else if (isGraph(term,store)) {
return makeGraph(store,term);
}
else {
const genid = makeGenId(term);
return {
type: 'BlankNode',
value: genid
} as IBlankNode;
}
}
else if (term.termType === 'Variable') {
return {
type: 'Variable',
value: term.value
} as IVariable;
}
else {
return {
type: 'BlankNode',
value: 'unknown'
} as IBlankNode;
}
}
function makeGenId(term: N3.Term) : string {
const value = term.value.replace(/^.*(_|\.)/,'') ;
return `_:${value}`;
}
function isGraphLike(quad: N3.Quad, graph: N3.Term) : boolean {
if (quad.graph.id === graph.id) {
return false;
}
else {
return true;
}
}
function isListLike(quad: N3.Quad) : boolean {
if (quad.predicate.value === pref(RDFS,'first') ||
quad.predicate.value === pref(RDFS,'rest')) {
return true;
}
else {
return false;
}
}
function isList(term: N3.Term, store: N3.Store) : boolean {
const first = store.getQuads(term,pref(RDFS,'first'),null,null);
const rest = store.getQuads(term,pref(RDFS,'rest'),null,null);
if (first.length == 1 || rest.length == 1) {
return true;
}
else {
return false;
}
}
function isGraph(term: N3.Term, store: N3.Store) : boolean {
const graph = store.getQuads(null, null, null, term);
if (graph.length == 0) {
return false;
}
else {
return true;
}
}
function makeList(term: N3.Term, store: N3.Store) : IList {
let termList : ITerm[] = [];
let searchTerm = term;
let brake = false;
do {
const first = store.getQuads(searchTerm,pref(RDFS,'first'),null,null);
const rest = store.getQuads(searchTerm,pref(RDFS,'rest'),null,null);
if (first.length == 0) {
if (rest.length == 0 || rest.length != 1) {
brake = true;
}
else {
brake = true;
}
}
else if (first.length != 1 || rest.length != 1) {
brake = true;
}
else if (first[0].object.value === pref(RDFS,'nil')) {
brake = true;
}
else {
const termValue = parseTerm(first[0].object, store);
termList.push(termValue);
if (rest[0].object.value === pref(RDFS,'nil')) {
brake = true;
}
else {
searchTerm = rest[0].object;
}
}
first.forEach( (quad) => { store.removeQuad(quad) });
rest.forEach( (quad) => { store.removeQuad(quad) });
} while (!brake);
return { type: 'List', value: termList } as IList;
}

Sorry, the diff of this file is not supported yet

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