UNPKG

crocks

Version:

A collection of well known Algebraic Datatypes for your utter enjoyment.

39 lines (28 loc) 975 B
/** @license ISC License (c) copyright 2017 original and current authors */ /** @author Ian Hofmann-Hicks (evil) */ var Pair = require('../core/types').proxy('Pair') var isFoldable = require('../core/isFoldable') var isSameType = require('../core/isSameType') var isString = require('../core/isString') function foldPairs(acc, pair) { var obj; if(!isSameType(Pair, pair)) { throw new TypeError('fromPairs: Foldable of Pairs required for argument') } var key = pair.fst() var value = pair.snd() if(!isString(key)) { throw new TypeError('fromPairs: String required for fst of every Pair') } return value !== undefined ? Object.assign(acc, ( obj = {}, obj[key] = value, obj )) : acc } // fromPairs : Foldable f => f (Pair String a) -> Object function fromPairs(xs) { if(!isFoldable(xs)) { throw new TypeError('fromPairs: Foldable of Pairs required for argument') } return xs.reduce(foldPairs, {}) } module.exports = fromPairs