-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rstream-query): add obj->triple converter, update readme & example
- Loading branch information
1 parent
d03520d
commit 6f95bcb
Showing
5 changed files
with
134 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { isArray } from "@thi.ng/checks/is-array"; | ||
import { isPlainObject } from "@thi.ng/checks/is-plain-object"; | ||
import { concat } from "@thi.ng/transducers/iter/concat"; | ||
import { pairs } from "@thi.ng/transducers/iter/pairs"; | ||
import { iterator } from "@thi.ng/transducers/iterator"; | ||
import { mapcat } from "@thi.ng/transducers/xform/mapcat"; | ||
|
||
let NEXT_ID = 0; | ||
|
||
const mapBNode = (s: any, p: any, o: any) => { | ||
const id = `__b${NEXT_ID++}__`; | ||
return concat([[s, p, id]], asTriples(o, id)); | ||
}; | ||
|
||
const mapSubject = (subject: any) => | ||
([p, o]) => { | ||
if (isArray(o)) { | ||
return iterator( | ||
mapcat((o) => | ||
isPlainObject(o) ? | ||
mapBNode(subject, p, o) : | ||
[[subject, p, o]]), | ||
o); | ||
} else if (isPlainObject(o)) { | ||
return mapBNode(subject, p, o); | ||
} | ||
return [[subject, p, o]]; | ||
}; | ||
|
||
/** | ||
* Converts given object into an iterable of triples, with the following | ||
* conversion rules: | ||
* | ||
* - Toplevel object keys are used as subjects and MUST each have a | ||
* plain object as value, where its keys are used as predicates and | ||
* values as objects (in the SPO sense). | ||
* - Plain objects in SPO object position are translated into unique IDs | ||
* in order to allow the nested map to become a subject itself. In RDF | ||
* terms, this is equivalent to BNodes. | ||
* - Arrays in SPO object position cause multiple triples with same | ||
* subject & predicate to be emitted. If any of the items in the array | ||
* is a plain object, it will be treated as BNode and transformed as | ||
* described in the previous rule | ||
* | ||
* ``` | ||
* src = { | ||
* "@thi.ng/rstream-query": { | ||
* type: "project", | ||
* author: "toxi", | ||
* tag: ["ES6", "TypeScript", "graph"] | ||
* }, | ||
* toxi: { | ||
* type: "person", | ||
* hasAccount: [ | ||
* {type: "twitter", id: "toxi"}, | ||
* {type: "github", id: "postspectacular"} | ||
* ] | ||
* } | ||
* }; | ||
* | ||
* [...asTriples(src)] | ||
* // [ [ '@thi.ng/rstream-query', 'type', 'project' ], | ||
* // [ '@thi.ng/rstream-query', 'author', 'toxi' ], | ||
* // [ '@thi.ng/rstream-query', 'tag', 'ES6' ], | ||
* // [ '@thi.ng/rstream-query', 'tag', 'TypeScript' ], | ||
* // [ '@thi.ng/rstream-query', 'tag', 'graph' ], | ||
* // [ 'toxi', 'type', 'person' ], | ||
* // [ 'toxi', 'hasAccount', '__b0__' ], | ||
* // [ '__b0__', 'type', 'twitter' ], | ||
* // [ '__b0__', 'id', 'toxi' ], | ||
* // [ 'toxi', 'hasAccount', '__b1__' ], | ||
* // [ '__b1__', 'type', 'github' ], | ||
* // [ '__b1__', 'id', 'postspectacular' ] ] | ||
* ``` | ||
* | ||
* @param obj | ||
* @param subject internal use only, do not specify! | ||
*/ | ||
export const asTriples = (obj: any, subject?: any) => | ||
iterator( | ||
mapcat( | ||
subject === undefined ? | ||
([s, v]: any) => iterator(mapcat(mapSubject(s)), <any>pairs(v)) : | ||
mapSubject(subject) | ||
), | ||
pairs(obj)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from "./api"; | ||
export * from "./convert"; | ||
export * from "./pattern"; | ||
export * from "./qvar"; | ||
export * from "./store"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters