UNPKG

crocks

Version:

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

70 lines (51 loc) 1.7 kB
/** @license ISC License (c) copyright 2016 original and current authors */ /** @author Ian Hofmann-Hicks (evil) */ var VERSION = 2 var _implements = require('../core/implements') var _inspect = require('../core/inspect') var _equals = require('../core/equals') var type = require('../core/types').type('Max') var _type = require('../core/types').typeFn(type(), VERSION) var fl = require('../core/flNames') var isNil = require('../core/isNil') var isNumber = require('../core/isNumber') var isSameType = require('../core/isSameType') var _empty = function () { return Max(-Infinity); } function Max(n) { var obj; var x = isNil(n) ? _empty().valueOf() : n if(!arguments.length || !isNumber(x)) { throw new TypeError('Max: Numeric value required') } var valueOf = function () { return x; } var empty = _empty var inspect = function () { return ("Max" + (_inspect(valueOf()))); } var equals = function (m) { return isSameType(Max, m) && _equals(x, m.valueOf()); } function concat(method) { return function(m) { if(!isSameType(Max, m)) { throw new TypeError(("Max." + method + ": Max requried")) } return Max(Math.max(x, m.valueOf())) } } return ( obj = { inspect: inspect, toString: inspect, equals: equals, valueOf: valueOf, type: type, empty: empty, concat: concat('concat') }, obj[fl.equals] = equals, obj[fl.empty] = empty, obj[fl.concat] = concat(fl.concat), obj['@@type'] = _type, obj.constructor = Max, obj ) } Max['@@implements'] = _implements( [ 'equals', 'concat', 'empty' ] ) Max.empty = _empty Max.type = type Max[fl.empty] = _empty Max['@@type'] = _type module.exports = Max