Mercurial > code > home > repos > streamed-graph
view src/rdf_value.ts @ 100:ad08e5e25fc9
revert jsonld a few years to avoid a vite/commonjs build issue
the newer jsonld has this dep graph:
jsonld 5.2.0
└─┬ rdf-canonize 3.0.0
└── setimmediate 1.0.5
and that setimmediate uses strict in a way that breaks the build, etc.
author | drewp@bigasterisk.com |
---|---|
date | Fri, 11 Feb 2022 22:57:23 -0800 |
parents | 47d3b5a5bd5e |
children |
line wrap: on
line source
import { Store, Term, NamedNode } from "n3"; import { RDFS } from "./namespaces"; function _singleValue(g: Store, s: Term, p: Term): Term { const quads = g.getQuads(s, p, null, null); const objs = new Set(quads.map((q) => q.object)); if (objs.size == 0) { throw new Error("no value for " + s.value + " " + p.value); } else if (objs.size == 1) { const obj = objs.values().next().value; return obj as Term; } else { throw new Error("too many different values: " + JSON.stringify(quads)); } } export function stringValue(g: Store, s: Term, p: Term): string { const ret = _singleValue(g, s, p); if (ret.termType != "Literal") { throw new Error(`ret=${ret}`); } return ret.value as string; } export function uriValue(g: Store, s: Term, p: Term): NamedNode { const ret = _singleValue(g, s, p); if (ret.termType != "NamedNode") { throw new Error(`ret=${ret}`); } return ret; } export function labelOrTail(g: Store, uri: NamedNode): string { let ret: string; try { ret = stringValue(g, uri, RDFS("label")); } catch (e) { const words = uri.value.split("/"); ret = words[words.length - 1]; } if (!ret) { ret = uri.value; } return ret; }