Skip to content

Instantly share code, notes, and snippets.

@epost
Last active September 10, 2016 14:28
Show Gist options
  • Save epost/c481820ccc65db2abc7ba39cac770f57 to your computer and use it in GitHub Desktop.
Save epost/c481820ccc65db2abc7ba39cac770f57 to your computer and use it in GitHub Desktop.
var R = require('ramda')
var FV = require('fantasy-validations')
var DV = require('data.validation') // folktale
var V = FV
// extend with flatten and chain
V.prototype.flatten = function () {
return this.f ? this : this.s // TODO use fold
}
V.prototype.chain = function (f) {
return this.map(f).flatten()
}
// some tests
exports.fantasyValidations = function (t) {
var s42 = V.of(42)
console.log('s42 =', s42)
var s0 = V.Success(42)
console.log('s0 =', s0)
var f0 = V.Failure('f0')
console.log('f0 =', f0)
var s1 = R.map(x=>x+1)(s0)
console.log('s1 =', s1)
t.done()
}
exports.fantasyValidations_concat = function (t) {
var f0 = V.Failure(['Failure 0'])
var f1 = V.Failure(['Failure 1'])
var fs = R.concat(f0, f1)
console.log('fs =', fs)
t.done()
}
exports.fantasyValidations_ap = function (t) {
var mkFoo = (name) => (level) => ({ name: name, level: level })
var mkFooE = V.Success(mkFoo)
// example data
var nameE = V.Success('Erik')
var levelE = V.Success(9999)
// aggregate successes
var s0 = R.ap(mkFooE, nameE)
console.log('s0 =', s0)
var s1 = R.ap(s0, levelE)
console.log('s1 =', s1)
// aggregate failures
var f0 = R.ap(mkFooE, V.Failure(['Failure: name']))
console.log('f0 =', f0)
var f1 = R.ap(f0, V.Failure(['Failure: level']))
console.log('f1 =', f1)
t.done()
}
exports.fantasyValidations_flatten = function (t) {
var f0 = V.Failure('boo')
console.log('f0 =', f0)
console.log('f0.flatten() =', f0.flatten())
var ss0 = V.of(V.of(42))
console.log('ss0 =', ss0)
console.log('ss0.flatten() =', ss0.flatten())
var sf0 = V.of(V.Failure('boo'))
console.log('sf0 =', sf0)
console.log('sf0.flatten() =', sf0.flatten())
t.done()
}
// join = flatMap = chain
exports.fantasyValidations_chain = function (t) {
var f = (x) => V.of(x+1)
var s0 = V.of(42)
console.log('s0 =', s0)
var res = R.chain(f, s0)
console.log('map f s0 =', R.map(f, s0))
console.log('unnest $ map f s0 =', R.unnest(R.map(f, s0)))
console.log('res =', res)
t.done()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment