Skip to content

Instantly share code, notes, and snippets.

@nateabele
Created June 24, 2015 02:21
Show Gist options
  • Select an option

  • Save nateabele/e3010459c4c18b31f8f7 to your computer and use it in GitHub Desktop.

Select an option

Save nateabele/e3010459c4c18b31f8f7 to your computer and use it in GitHub Desktop.
Domp
import R from 'ramda';
let { is, complement, filter, map, mapObj, mapObjIndexed, prop, pipe, identity, mergeAll, toLower } = R;
class Domp {
constructor() {
this.propMap = {
name: pipe(prop('nodeName'), toLower),
type: prop('nodeType'),
text: prop('nodeValue'),
attributes: pipe(prop('attributes'), filter(complement(is(Function))), map(node => ({ [node.name]: node.value })), mergeAll),
children: pipe(prop('children'), map(this.toObject.bind(this)))
};
}
toObject(node) {
return mapObj(fn => fn(node))(this.propMap);
}
toJSON(node) {
return JSON.stringify(this.toObject(node));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment