UNPKG

@thi.ng/rstream-query

Version:

@thi.ng/rstream based triple store & reactive query engine

29 lines (28 loc) 880 B
import { isArray } from "@thi.ng/checks/is-array"; import { isPlainObject } from "@thi.ng/checks/is-plain-object"; import { concat } from "@thi.ng/transducers/concat"; import { mapcat } from "@thi.ng/transducers/mapcat"; import { pairs } from "@thi.ng/transducers/pairs"; let __nextID = 0; const __mapBNode = (s, p, o) => { const id = `__b${__nextID++}__`; return concat([[s, p, id]], asTriples(o, id)); }; const __mapSubject = (subject) => ([p, o]) => { if (isArray(o)) { return mapcat( (o2) => isPlainObject(o2) ? __mapBNode(subject, p, o2) : [[subject, p, o2]], o ); } else if (isPlainObject(o)) { return __mapBNode(subject, p, o); } return [[subject, p, o]]; }; const asTriples = (obj, subject) => mapcat( subject === void 0 ? ([s, v]) => mapcat(__mapSubject(s), pairs(v)) : __mapSubject(subject), pairs(obj) ); export { asTriples };