all files / semantic-graphql/src/graphql/ getGraphqlDescription.js

96.15% Statements 25/26
85.71% Branches 18/21
100% Functions 2/2
100% Lines 21/21
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 39 40   80×   40×   40×       40×       20×   20× 20×   20×   20× 20×   20×     20×   20×      
const { isLiteral, getLiteralValue, getLiteralLanguage } = require('n3').Util;
const { rdfsLabel, rdfsComment } = require('../constants');
const getIriLocalName = require('../utils/getIriLocalName');
const memorize = require('../graph/memorize');
 
const englishLocale = 'en';
const isLiteralWithLocale = locale => l => isLiteral(l) && getLiteralLanguage(l) === locale;
 
function findValueForLocale(literals, locale) {
  Iif (!literals) return null;
 
  const literal = literals.find(isLiteralWithLocale(locale))
    || literals.find(isLiteralWithLocale(englishLocale))
    || literals[0];
 
  return isLiteral(literal) ? getLiteralValue(literal) : null;
}
 
function getGraphqlDescription(g, iri) {
  // The default locale is "en", can be set with config.locale
  const { locale = englishLocale } = g.config;
 
  const label = findValueForLocale(g[iri][rdfsLabel], locale);
  const comment = findValueForLocale(g[iri][rdfsComment], locale);
 
  let description = label && getIriLocalName(iri) !== label ? label : '';
 
  Eif (comment) {
    if (description) description += ' - ';
 
    description += comment;
  }
 
  if (description && !description.endsWith('.')) description += '.';
 
  return description;
}
 
module.exports = memorize(getGraphqlDescription, 'graphqlDescription');