Skip to content

Instantly share code, notes, and snippets.

@howarddierking
Created July 24, 2019 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save howarddierking/a2e23aca0c731f176e0240e39c5000fe to your computer and use it in GitHub Desktop.
Save howarddierking/a2e23aca0c731f176e0240e39c5000fe to your computer and use it in GitHub Desktop.
'use strict';
const R = require('ramda');
const Rx = require('../ramdaExt');
const concatUnique = (k, l, r) => {
if(R.equals(l, r))
return R.flatten([l]);
return R.flatten([l, r]);
}
const termFor = bindingGroup => {
if(bindingGroup.length === 0)
return {};
const head = R.head(bindingGroup);
const t = R.pipe(
R.toPairs,
R.map(p => {
return [
R.slice(1, Infinity, Rx.first(p)), // remove '?' from SPARQL parameters
[ R.prop('value', Rx.second(p)) ] // TODO: add friendly type name back
];
}),
R.fromPairs)(head);
return R.mergeWithKey(concatUnique, t, termFor(R.tail(bindingGroup)));
};
const termsFrom = R.pipe(
R.groupBy(R.path(['?id', 'value'])),
R.values,
R.map(termFor));
module.exports = {
termsFrom,
termFor
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment