@graphy/core.class.writable
Advanced tools
Comparing version 3.2.2 to 4.0.0
249
main.js
@@ -5,3 +5,3 @@ | ||
const factory = require('@graphy/core.data.factory'); | ||
const stream = require('@graphy/core.iso.stream'); | ||
const Scribable = require('@graphy/core.class.scribable'); | ||
@@ -15,13 +15,8 @@ const R_DIRECTIVE_CONTENTS = /^`\[[^\]]+\](.*)$/; | ||
class Writable extends stream.Transform { | ||
class Writable extends Scribable { | ||
constructor(gc_writable={}) { | ||
super({ | ||
writableObjectMode: true, | ||
readableObjectMode: false, | ||
}); | ||
super(gc_writable); | ||
let { | ||
prefixes: h_prefixes={}, | ||
collections: gc_collections=null, | ||
lists: gc_lists=null, | ||
} = gc_writable; | ||
@@ -43,4 +38,4 @@ | ||
// collections | ||
let g_collections = { | ||
// lists | ||
let g_lists = { | ||
first: '>http://www.w3.org/1999/02/22-rdf-syntax-ns#first', | ||
@@ -52,3 +47,3 @@ rest: '>http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', | ||
// custom transcoder | ||
if(gc_collections) { | ||
if(gc_lists) { | ||
let { | ||
@@ -58,59 +53,22 @@ first: sc1_first=null, | ||
nil: sc1_nil=null, | ||
} = gc_collections; | ||
} = gc_lists; | ||
if(sc1_first) g_collections.first = sc1_first; | ||
if(sc1_rest) g_collections.rest = sc1_rest; | ||
if(sc1_nil) g_collections.nil = sc1_nil; | ||
if(sc1_first) g_lists.first = sc1_first; | ||
if(sc1_rest) g_lists.rest = sc1_rest; | ||
if(sc1_nil) g_lists.nil = sc1_nil; | ||
} | ||
Object.assign(this, { | ||
isGraphyWritable: true, | ||
state: 0, | ||
prefixes: h_prefixes || {}, | ||
coercions: hm_coercions, | ||
collections: g_collections, | ||
_xc_state: 0, | ||
_hm_coercions: hm_coercions, | ||
_g_lists: g_lists, | ||
}); | ||
// one new sources | ||
this.on('pipe', (ds_src) => { | ||
// listen for prefix events | ||
ds_src.on('prefix', (s_prefix_id, p_iri) => { | ||
this.write({ | ||
type: 'prefixes', | ||
value: { | ||
[s_prefix_id]: p_iri, | ||
}, | ||
}); | ||
}); | ||
// listen for comment events | ||
ds_src.on('comment', (s_comment) => { | ||
this.write({ | ||
type: 'c3', | ||
value: { | ||
[factory.comment()]: s_comment, | ||
}, | ||
}); | ||
}); | ||
}); | ||
// bind event listeners | ||
if(gc_writable.close) this.once('close', gc_writable.close); | ||
if(gc_writable.drain) this.on('drain', gc_writable.drain); | ||
if(gc_writable.error) this.on('error', gc_writable.error); | ||
if(gc_writable.finish) this.once('finish', gc_writable.finish); | ||
if(gc_writable.data) this.on('data', gc_writable.data); | ||
if(gc_writable.end) this.once('end', gc_writable.end); | ||
if(gc_writable.warning) this.on('warning', gc_writable.warning); | ||
} | ||
// overrideable no-ops | ||
serialize_prefixes() {} // eslint-disable-line class-methods-use-this | ||
// serialize comment | ||
serialize_comment(s_comment) { | ||
_serialize_comment(s_comment, g_directive) { | ||
let s_write = ''; | ||
// non-data state | ||
if(2 !== this.state) { | ||
if(2 !== this._xc_state) { | ||
// break line | ||
@@ -120,38 +78,48 @@ s_write += '\n'; | ||
// update state | ||
this.state = 2; | ||
this._xc_state = 2; | ||
} | ||
// each line of comment | ||
for(let s_line of s_comment.split(/\n/g)) { | ||
s_write += `# ${s_line}\n`; | ||
// comment width | ||
if(g_directive && g_directive.width) { | ||
let n_width = g_directive.width; | ||
let a_lines = []; | ||
while(s_comment.length > n_width) { | ||
let s_line = s_comment.slice(0, n_width+1); | ||
let m_line = /^(.*[^\s])\s+/.exec(s_line); | ||
if(m_line) { | ||
let s_push = m_line[1]; | ||
a_lines.push(s_push); | ||
s_comment = s_comment.slice(s_push.length).replace(/^\s+/, ''); | ||
} | ||
else { | ||
a_lines.push(s_comment.slice(0, n_width)); | ||
s_comment = s_comment.slice(n_width); | ||
} | ||
} | ||
s_comment = a_lines.join('\n'); | ||
} | ||
return s_write; | ||
return s_write+(super._serialize_comment(s_comment) || ''); | ||
} | ||
// serialize newlines | ||
serialize_newlines(n_newlines) { // eslint-disable-line class-methods-use-this | ||
// no need to check/change state | ||
return '\n'.repeat(n_newlines); | ||
} | ||
// transcode collection into concise-pairs hash | ||
transcode_collection(a_collection) { | ||
let g_collections = this.collections; | ||
// empty collection | ||
if(!a_collection.length) { | ||
return g_collections.nil; | ||
// return { | ||
// [g_collections.first]: g_collections.nil, | ||
// }; | ||
// transcode list into concise-pairs hash | ||
_transcode_list(a_list, g_lists=this._g_lists) { | ||
// empty list | ||
if(!a_list.length) { | ||
return g_lists.nil; | ||
} | ||
// non-empty collection | ||
// non-empty list | ||
else { | ||
let z_item = a_collection[0]; | ||
let z_item = a_list[0]; | ||
let w_first = z_item; | ||
// item is nested collection; transcode | ||
// item is nested list; transcode | ||
if(Array.isArray(z_item)) { | ||
w_first = this.transcode_collection(z_item); // eslint-disable-line no-invalid-this | ||
w_first = this._transcode_list(z_item, g_lists); // eslint-disable-line no-invalid-this | ||
} | ||
@@ -161,8 +129,8 @@ | ||
// first item | ||
[g_collections.first]: w_first, | ||
[g_lists.first]: w_first, | ||
// rest of items | ||
[g_collections.rest]: 1 === a_collection.length | ||
? g_collections.nil | ||
: this.transcode_collection(a_collection.slice(1)), // eslint-disable-line no-invalid-this | ||
[g_lists.rest]: 1 === a_list.length | ||
? g_lists.nil | ||
: this._transcode_list(a_list.slice(1), g_lists), // eslint-disable-line no-invalid-this | ||
}; | ||
@@ -172,46 +140,8 @@ } | ||
// implement stream.Transform | ||
_transform(g_event, s_encoding, fke_transform) { | ||
// try to serialize input value | ||
try { | ||
// this.push(this.data_event(g_event), 'utf8'); | ||
fke_transform(null, this.data_event(g_event)); | ||
} | ||
// serialization error | ||
catch(e_serialize) { | ||
fke_transform(e_serialize); | ||
} | ||
} | ||
// route writable data events | ||
data_event(g_event) { | ||
let { | ||
type: s_type, | ||
value: z_value, | ||
} = g_event; | ||
// method id | ||
let s_method = 'serialize_'+s_type; | ||
// event type exists | ||
if('function' === typeof this[s_method]) { | ||
return this[s_method](z_value); | ||
} | ||
// no such event type | ||
else { | ||
throw new Error(`no such writable data event type for RDF stream: '${s_type}'`); | ||
} | ||
} | ||
// serialize 'array' writable data event | ||
serialize_array(a_events) { | ||
return a_events.map(g_event => this.data_event(g_event)).join(''); | ||
} | ||
// serialize a writable data event directive | ||
directive(sct_directive, w_value) { | ||
_apply_directive(sc1_directive, w_value) { | ||
// directive contents | ||
let m_directive = R_DIRECTIVE_CONTENTS.exec(sct_directive); | ||
let m_directive = R_DIRECTIVE_CONTENTS.exec(sc1_directive); | ||
if(!m_directive) { | ||
throw new Error(`Invalid writable data event directive string: "${sct_directive}"`); | ||
throw new Error(`Invalid writable data event directive string: "${sc1_directive}"`); | ||
} | ||
@@ -236,4 +166,6 @@ | ||
// serializer supports commenting; serialize comment | ||
if(this.serialize_comment) { | ||
return this.serialize_comment(w_value+'', g_directive); | ||
if(this._serialize_comment) { | ||
return { | ||
write: this._serialize_comment(w_value+'', g_directive), | ||
}; | ||
} | ||
@@ -246,4 +178,6 @@ break; | ||
// serializer supports newlines; serialize newlines | ||
if(this.serialize_newlines) { | ||
return this.serialize_newlines(w_value); | ||
if(this._serialize_newlines) { | ||
return { | ||
write: this._serialize_newlines(w_value), | ||
}; | ||
} | ||
@@ -253,2 +187,47 @@ break; | ||
// config | ||
case 'config': { | ||
switch(g_directive.value) { | ||
// list config | ||
case 'lists': { | ||
// ref stack of list serializers | ||
let a_list_serializers = this._a_list_serializers; | ||
// push current method to stack | ||
a_list_serializers.push(this._serialize_list_object); | ||
// inherit unspecified keys from parent | ||
let g_list_default = this._g_lists; | ||
// build list config | ||
let g_lists = { | ||
first: w_value.first || g_list_default.first, | ||
rest: w_value.rest || g_list_default.rest, | ||
nil: w_value.nil || g_list_default.nil, | ||
}; | ||
// redefine list object serialization | ||
this._serialize_list_object = function(a_list, n_nest_level) { | ||
// transcode list object | ||
let hc2_transcoded = this._transcode_list(a_list, g_lists); | ||
// serialize object | ||
return this._encode_objects(hc2_transcoded, n_nest_level); | ||
}; | ||
// return local directive instructions | ||
return { | ||
exit: () => { | ||
this._serialize_list_object = a_list_serializers.pop(); | ||
}, | ||
}; | ||
} | ||
// no such key | ||
default: { | ||
throw new Error(`No such config key '${g_directive.value}'`); | ||
} | ||
} | ||
} | ||
// other | ||
@@ -261,8 +240,8 @@ default: { | ||
// nothing | ||
return ''; | ||
return {}; | ||
} | ||
// if not overriden by subclass, serialize quads in default graph | ||
serialize_c4(hc4_quads) { | ||
let h_prefixes = this.prefixes; | ||
_serialize_c4(hc4_quads) { | ||
let h_prefixes = this._h_prefixes; | ||
let a_unions = []; | ||
@@ -277,3 +256,3 @@ let s_write = ''; | ||
// add all quads from graph | ||
s_write += this.serialize_c3(hc4_quads[sv1_graph]); | ||
s_write += this._serialize_c3(hc4_quads[sv1_graph]); | ||
} | ||
@@ -280,0 +259,0 @@ |
{ | ||
"name": "@graphy/core.class.writable", | ||
"version": "3.2.2", | ||
"description": "Produce quads using nestable concise term string objects", | ||
"version": "4.0.0", | ||
"description": "Serialize RDF conveniently and with style", | ||
"keywords": [ | ||
@@ -26,4 +26,4 @@ "linked-data", | ||
"dependencies": { | ||
"@graphy/core.data.factory": "^3.2.2", | ||
"@graphy/core.iso.stream": "^3.2.2" | ||
"@graphy/core.data.factory": "^4.0.0", | ||
"@graphy/core.iso.stream": "^4.0.0" | ||
}, | ||
@@ -33,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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
7099
1872
211
1
+ 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)
- Removed@graphy/core.data.factory@3.2.2(transitive)
- Removed@graphy/core.iso.stream@3.2.2(transitive)