@graphy/content.nt.write
Advanced tools
Comparing version 3.2.2 to 4.0.0
155
main.js
@@ -5,7 +5,52 @@ | ||
const factory = require('@graphy/core.data.factory'); | ||
const writable = require('@graphy/core.class.writable'); | ||
const Writable = require('@graphy/core.class.writable'); | ||
const { | ||
c1_to_nt, | ||
clean_iri, | ||
} = factory; | ||
const N_MAX_STRING_BUFFER = 1 << 12; | ||
class NTriples_Writer extends writable { | ||
function verbose_s(yt_subject) { | ||
if('NamedNode' === yt_subject.termType) { | ||
return '<'+clean_iri(yt_subject.value)+'>'; | ||
} | ||
else { | ||
return '_:'+yt_subject.value; | ||
} | ||
} | ||
const verbose_p = yt_predicate => '<'+clean_iri(yt_predicate.value)+'>'; | ||
const P_IRI_XSD_STRING = 'http://www.w3.org/2001/XMLSchema#'; | ||
function verbose_o(yt_object) { | ||
switch(yt_object.termType) { | ||
// named node | ||
case 'NamedNode': return '<'+clean_iri(yt_object.value)+'>'; | ||
// literal | ||
case 'Literal': { | ||
let s_contents = JSON.stringify(yt_object.value); | ||
if(yt_object.language) { | ||
return s_contents+'@'+yt_object.language; | ||
} | ||
else if(P_IRI_XSD_STRING === yt_object.datatype.value) { | ||
return s_contents; | ||
} | ||
else { | ||
return s_contents+'^^<'+clean_iri(yt_object.datatype.value)+'>'; | ||
} | ||
} | ||
// blank node | ||
default: return '_:'+yt_object.value; | ||
} | ||
} | ||
class NTriples_Writer extends Writable { | ||
constructor(gc_writer={}) { | ||
@@ -15,10 +60,7 @@ super(gc_writer); | ||
let { | ||
prefixes: h_prefixes={}, | ||
indent: s_indent='\t', | ||
tokens: gc_tokens=null, | ||
style: gc_style=null, | ||
} = gc_writer; | ||
Object.assign(this, { | ||
indent: s_indent, | ||
state: 2, | ||
_xc_state: 2, | ||
}); | ||
@@ -29,21 +71,27 @@ } | ||
// serialize c3 hash | ||
serialize_c3(hc3_triples) { | ||
// serialize c3 hash | ||
_serialize_c3(hc3_triples) { | ||
let { | ||
prefixes: h_prefixes, | ||
_h_prefixes: h_prefixes, | ||
} = this; | ||
let s_write = ''; | ||
// post component | ||
// post component | ||
let st_post = ' .\n'; | ||
// each subject | ||
// subject exit listener | ||
let f_exit_subject = null; | ||
// each subject | ||
for(let sc1_subject in hc3_triples) { | ||
// directive; serialize it | ||
// directive; serialize it | ||
if('`' === sc1_subject[0]) { | ||
s_write += this.directive(sc1_subject, hc3_triples[sc1_subject]); | ||
let g_apply = this._apply_directive(sc1_subject, hc3_triples[sc1_subject]); | ||
// write data | ||
if(g_apply.write) s_write += g_apply.write; | ||
// save exit listener | ||
if(g_apply.exit) f_exit_subject = g_apply.exit; | ||
continue; | ||
} | ||
// subject-pre component | ||
let st_pre = factory.c1(sc1_subject, h_prefixes).verbose()+' '; | ||
let st_pre = c1_to_nt(sc1_subject, h_prefixes, true)+' '; | ||
// pair indent & terminator | ||
@@ -54,16 +102,22 @@ let s_indent_pairs = ''; | ||
let hc2_pairs = hc3_triples[sc1_subject]; | ||
// predicate exit listener | ||
let f_exit_predicate = null; | ||
// each predicate | ||
for(let sc1_predicate in hc2_pairs) { | ||
// directive; serialize it | ||
// directive; serialize it | ||
if('`' === sc1_predicate[0]) { | ||
s_write += this.directive(sc1_predicate, hc2_pairs[sc1_predicate]); | ||
let g_apply = this._apply_directive(sc1_predicate, hc2_pairs[sc1_predicate]); | ||
// write data | ||
if(g_apply.write) s_write += g_apply.write; | ||
// save exit listener | ||
if(g_apply.exit) f_exit_predicate = g_apply.exit; | ||
continue; | ||
} | ||
// predicate component | ||
let st_predicate = factory.c1(sc1_predicate, h_prefixes).verbose(); | ||
let st_predicate = c1_to_nt(sc1_predicate, h_prefixes, true); | ||
// ref objects | ||
let z_objects = hc2_pairs[sc1_predicate]; | ||
// serialize objects | ||
s_write += this.objects(st_pre+st_predicate+' ', z_objects, st_post); | ||
// // string buffer became too large | ||
s_write += this._encode_objects(st_pre+st_predicate+' ', z_objects, st_post); | ||
// // string buffer became too large | ||
// if(s_write.length >= N_MAX_STRING_BUFFER) { | ||
@@ -73,3 +127,7 @@ // debugger; | ||
} | ||
// call exit predicate listener | ||
if(f_exit_predicate) f_exit_predicate(); | ||
} | ||
// call exit subject listener | ||
if(f_exit_subject) f_exit_subject(); | ||
@@ -82,6 +140,6 @@ return s_write; | ||
// write objects | ||
objects(s_pre, z_objects, s_post, n_nest_level=1) { | ||
_encode_objects(s_pre, z_objects, s_post, n_nest_level=1) { | ||
let { | ||
prefixes: h_prefixes, | ||
coercions: hm_coercions, | ||
_h_prefixes: h_prefixes, | ||
_hm_coercions: hm_coercions, | ||
} = this; | ||
@@ -111,13 +169,13 @@ | ||
for(let z_item of z_objects) { | ||
// item is an array; write RDF collection | ||
// item is an array; write list | ||
if(Array.isArray(z_item)) { | ||
// transcode collection | ||
let hc2_collection = this.transcode_collection(z_item); | ||
// transcode list | ||
let hc2_list = this._transcode_list(z_item); | ||
// serialize transcoded collection | ||
s_write += this.objects(s_pre, hc2_collection, s_post, n_nest_level); | ||
// serialize transcoded list | ||
s_write += this._encode_objects(s_pre, hc2_list, s_post, n_nest_level); | ||
} | ||
// non-array; recurse on item | ||
else { | ||
s_write += this.objects(s_pre, z_item, s_post, n_nest_level); | ||
s_write += this._encode_objects(s_pre, z_item, s_post, n_nest_level); | ||
} | ||
@@ -136,2 +194,5 @@ } | ||
// object exit listener | ||
let f_exit_object = null; | ||
// each pair | ||
@@ -141,3 +202,9 @@ for(let sc1_predicate in z_objects) { | ||
if('`' === sc1_predicate[0]) { | ||
s_write += this.directive(sc1_predicate, z_objects[sc1_predicate]); | ||
let g_apply = this._apply_directive(sc1_predicate, z_objects[sc1_predicate]); | ||
// write data | ||
if(g_apply.write) s_write += g_apply.write; | ||
// save exit listener | ||
if(g_apply.exit) f_exit_object = g_apply.exit; | ||
continue; | ||
@@ -147,8 +214,11 @@ } | ||
// pre-string for nested triples | ||
let s_pre_nest = st_blank+' '+factory.c1(sc1_predicate, h_prefixes).verbose()+' '; | ||
let s_pre_nest = st_blank+' '+c1_to_nt(sc1_predicate, h_prefixes, true)+' '; | ||
// recurse | ||
s_write += this.objects(s_pre_nest, z_objects[sc1_predicate], s_post, n_nest_level+1); | ||
s_write += this._encode_objects(s_pre_nest, z_objects[sc1_predicate], s_post, n_nest_level+1); | ||
} | ||
// call exit object listener | ||
if(f_exit_object) f_exit_object(); | ||
return s_write; | ||
@@ -182,10 +252,8 @@ } | ||
// rdfjs quad | ||
serialize_quad(g_quad) { | ||
let kq_quad = factory.from.quad(g_quad); | ||
_serialize_quad(g_quad) { | ||
// serialize quad | ||
let s_write = (2 !== this.state? '\n': '') | ||
+kq_quad.subject.verbose() | ||
+' '+kq_quad.predicate.verbose() | ||
+' '+kq_quad.object.verbose() | ||
this._s_push += (2 !== this._xc_state? '\n': '') | ||
+verbose_s(g_quad.subject) | ||
+' '+verbose_p(g_quad.predicate) | ||
+' '+verbose_o(g_quad.object) | ||
@@ -195,10 +263,13 @@ +' .\n'; | ||
// update state | ||
this.state = 2; | ||
return s_write; | ||
this._xc_state = 2; | ||
} | ||
} | ||
Object.assign(NTriples_Writer.prototype, { | ||
_serialize_c3r: NTriples_Writer.prototype._serialize_c3, | ||
_serialize_comment: Writable.prototype._serialize_hash_comment, | ||
}); | ||
module.exports = function(gc_writer) { | ||
return new NTriples_Writer(gc_writer); | ||
}; |
{ | ||
"name": "@graphy/content.nt.write", | ||
"version": "3.2.2", | ||
"description": "RDF N-Triples content writer", | ||
"version": "4.0.0", | ||
"description": "RDF N-Triples content writer for dynamic and stylized output", | ||
"keywords": [ | ||
@@ -26,5 +26,4 @@ "linked-data", | ||
"dependencies": { | ||
"@graphy/core.class.writable": "^3.2.2", | ||
"@graphy/core.data.factory": "^3.2.2", | ||
"big-integer": "^1.6.41" | ||
"@graphy/core.class.writable": "^4.0.0", | ||
"@graphy/core.data.factory": "^4.0.0" | ||
}, | ||
@@ -34,2 +33,2 @@ "engines": { | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7529
2
213
+ Added@graphy/core.class.scribable@4.3.7(transitive)
+ Added@graphy/core.class.writable@4.3.7(transitive)
+ Added@graphy/core.data.factory@4.3.7(transitive)
+ Added@graphy/core.iso.stream@4.3.7(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
- Removedbig-integer@^1.6.41
- Removed@graphy/core.class.writable@3.2.2(transitive)
- Removed@graphy/core.data.factory@3.2.2(transitive)
- Removed@graphy/core.iso.stream@3.2.2(transitive)
- Removedbig-integer@1.6.52(transitive)