Skip to content

Instantly share code, notes, and snippets.

@kaw2k
Created January 17, 2016 05:41
Show Gist options
  • Save kaw2k/88797183216bb30be6f7 to your computer and use it in GitHub Desktop.
Save kaw2k/88797183216bb30be6f7 to your computer and use it in GitHub Desktop.
import R from 'ramda';
function getValidators(ref) {
var propValidators = R.pathOr([], ['props', 'validators'], ref);
var refValidators = R.pathOr([], ['validators'], ref);
return [].concat(propValidators, refValidators);
}
function Tree(self, children) {
return {
children,
self,
map: fn => Tree(fn(self), R.map(fn, children)),
extract: function() {
if (children) return R.map(children, R.invoker(0, 'extract'))
return self
}
};
}
// This tree should work with how our getInputs functions behave. Assuming we
// transform what is needed into this tree, we could greatly simplify things.
var fieldValues = Tree(this, this.refs)
.map(self => self && self.getValue && self.getValue())
.extract()
var fieldErrors = Tree(this, this.refs)
.map(self => getValidators(self).map(fn => fn(self.getValue())))
.extract()
// Now this is all theory land and I know for a fact there are bugs and hand
// waving in the code, but the principles are sound. This Tree is both a
// Functor and (almost) a Comonad. There are still unknowns,
// like how to get the data into this type, but that shouldn't be too hard.
// What inspired this: http://joneshf.github.io/programming/2015/12/31/Comonads-Monoids-and-Trees.html
// For reference: https://github.com/fantasyland/fantasy-land#functor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment