All files / apps/web/src/utils iri.js

100% Statements 14/14
100% Branches 6/6
100% Functions 2/2
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38          3x 4x 4x 4x       3x                               3x 6x 6x 43x   2x 2x 1x    
/**
 * Formatage des IRI RDF pour l'affichage (partagé par le viewer et la vue 3D).
 */
 
/** Dernier segment d'un IRI (`…#fragment` ou `…/segment`). */
export const shortIri = (iri) => {
  const s = String(iri);
  const i = Math.max(s.lastIndexOf('#'), s.lastIndexOf('/'));
  return i >= 0 ? s.slice(i + 1) : s;
};
 
// Préfixes des vocabulaires courants → affichage « ontology:predicate ».
const NAMESPACES = [
  ['http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdf'],
  ['http://www.w3.org/2000/01/rdf-schema#', 'rdfs'],
  ['http://www.w3.org/2002/07/owl#', 'owl'],
  ['http://www.w3.org/2004/02/skos/core#', 'skos'],
  ['https://schema.org/', 'schema'],
  ['http://schema.org/', 'schema'],
  ['http://xmlns.com/foaf/0.1/', 'foaf'],
  ['http://purl.org/dc/terms/', 'dcterms'],
  ['http://purl.org/dc/elements/1.1/', 'dc'],
  ['http://www.w3.org/ns/shacl#', 'shacl'],
  ['http://www.w3.org/2001/XMLSchema#', 'xsd'],
  ['http://www.w3.org/ns/prov#', 'prov'],
];
 
/** Rend un IRI sous forme préfixée `ns:fragment` (repli : dernier segment de namespace). */
export const prefixedIri = (iri) => {
  const s = String(iri);
  for (const [base, prefix] of NAMESPACES) {
    if (s.startsWith(base)) return `${prefix}:${s.slice(base.length)}`;
  }
  const i = Math.max(s.lastIndexOf('#'), s.lastIndexOf('/'));
  if (i < 0) return s;
  return `${shortIri(s.slice(0, i))}:${s.slice(i + 1)}`;
};