This repository has been archived by the owner on Nov 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
adt.min.js
8 lines (8 loc) · 6.26 KB
/
adt.min.js
1
2
3
4
5
6
7
8
// adt.js
// ------
// Algebraic data types and immutable structures in Javascript
//
// version : 0.7.2
// author : Nathan Faubion <nathan@n-son.com>
// license : MIT
(function(e){"use strict";function t(e,t){if(t instanceof e.__adtClass__)return!0;throw new TypeError("Unexpected type")}function n(e){return e.constructor.__order__}function r(e,t){var n=[],r=t||0,i=e.length;for(;r<i;r++)n.push(e[r]);return n}function i(e,t){var n=function(){};n.prototype=e.prototype;var r=new n,i=e.apply(r,t);return r}function s(e,t){var n=function(){};return n.prototype=e.prototype,t.prototype=new n,t.prototype.constructor=t,t}function o(e,t){return function(){return e.apply(this,t.concat(r(arguments)))}}function u(e){var t=r(arguments,1),n=0,i=t.length,s;for(;n<i;n++)for(s in t[n])t[n].hasOwnProperty(s)&&(e[s]=t[n][s]);return e}function a(t,n){if(t instanceof Function){if(n instanceof t||t===Number&&typeof n=="number"||t===String&&typeof n=="string"||t===Boolean&&typeof n=="boolean")return!0}else if(t instanceof e.__Base__&&t.equals(n)||t===n)return!0;return!1}function f(e,t){var n=0,r=e.length;for(;n<r;n++)if(a(e[n],t))return!0;return!1}function l(e){return e&&typeof e=="object"&&e.toJSON?e.toJSON():e}function h(e){return(e||"")+c++}function p(){}e.__Base__=function(){},e.data=function(){var t=typeof arguments[0];if(t==="string"){var n=arguments;return e.data(function(e){var t=0,r=n.length;for(;t<r;t++)e(n[t])})}if(t==="object"){var r=arguments[0];return e.data(function(e){for(var t in r)r.hasOwnProperty(t)&&e(t,r[t])})}var i=arguments[0]||p,n=[],o=s(e.__Base__,function(){if(this instanceof o||o.apply===Function.prototype.apply)throw new Error("Bad invocation");return o.apply(this,arguments)});o.type=function(t,r){typeof t!="string"&&(r=t,t=h("Anonymous"));var i=f([String,Boolean,Number,Date,null,void 0],r);i?r=e.single(r):typeof r!="function"&&(r=a(Array,r)?e.record.apply(null,r):e.record(r)),o.prototype["is"+t]=!1;var s=r(o,t);return s=typeof s=="function"?u(s.bind(),s):s,o[t]=s,n.push(t),s};var r=i.call(o,o.type,o);if(typeof r=="object"&&!(r instanceof e.__Base__))for(var l in r)r.hasOwnProperty(l)&&o.type(l,r[l]);return o.seal=function(){var e=0,t,r;for(;t=n[e];e++)this[t].seal&&this[t].seal();return delete o.type,delete o.seal,o},o.__names__=n,o.prototype.__adtClass__=o,o},e.single=function(t){var n=function(){};return n.__value__=t===void 0?null:t,function(t,r){return s(t,n),u(n.prototype,e.single.__methods__),n.className=r,n.prototype["is"+r]=!0,new n}},e.single.__methods__={toString:function(){return this.constructor.className},toJSON:function(){return this.constructor.__value__},clone:function(){return this},equals:function(e){return this===e},hasInstance:function(e){return this===e}},e.record=function(){var t=typeof arguments[0];if(t==="string"){var n=arguments;return e.record(function(t){var r=0,i=n.length;for(;r<i;r++)t(n[r],e.any)})}if(t==="object"){var a=arguments[0];return e.record(function(e){for(var t in a)a.hasOwnProperty(t)&&e(t,a[t])})}var f=arguments[0]||p,n=[],l={},c=function(){var e=arguments,t=n.length;if(!(this instanceof c))return e.length<t?o(c,r(e)):i(c,e);if(e.length!==t)throw new Error("Unexpected number of arguments for "+c.className+": "+"got "+e.length+", but need "+t+".");var s=0,u;for(;u=n[s];s++)this[u]=l[u](e[s],u,c)};return function(t,r){s(t,c),u(c,e.record.__classMethods__),u(c.prototype,e.record.__methods__),c.className=r,c.prototype["is"+r]=!0,c.field=function(t,r){r||(r=e.any);if(typeof r!="function")throw new TypeError("Constraints must be functions");return n.push(t),l[t]=r,c};var i=f.call(c,c.field,c);if(typeof i=="object"&&i!==c)for(var r in i)i.hasOwnProperty(r)&&c.field(r,i[r]);return c.__names__=n,c.__constraints__=l,c}},e.record.__methods__={toString:function(){var e=this.constructor,t=e.unapply(this);return e.className+(t.length?"("+t.join(", ")+")":"")},toJSON:function(){return this.constructor.unapplyObject(this,l)},clone:function(){var t=this.constructor,n=t.__names__,r=[],i=0,s,o;for(;s=n[i];i++)o=this[s],r[i]=o instanceof e.__Base__?o.clone():e.nativeClone(o);return t.apply(null,r)},equals:function(t){var n=this.constructor;if(this===t)return!0;if(t instanceof n){var r=n.__names__,i=0,s=r.length,o,u,a;for(;i<s;i++){a=r[i],o=this[a],u=t[a];if(o instanceof e.__Base__){if(!o.equals(u))return!1}else if(!e.nativeEquals(o,u))return!1}return!0}return!1},get:function(e){var t=this.constructor,n=t.__names__,r=t.__constraints__;if(typeof e=="number"){if(e<0||e>n.length-1)throw new Error("Field index out of range: "+e);e=n[e]}else if(!r.hasOwnProperty(e))throw new Error("Field name does not exist: "+e);return this[e]},set:function(e){var t=this.constructor,n=t.__names__,r=[],i=0,s;for(;s=n[i];i++)r[i]=s in e?e[s]:this[s];return t.apply(null,r)}},e.record.__classMethods__={create:function(e){var t=[],n=this.__names__,r=0,i=n.length,s;for(;s=n[r];r++){if(!(s in e))throw new Error("Missing `"+s+"` in arguments to "+this.className);t[r]=e[s]}return this.apply(null,t)},hasInstance:function(e){return e instanceof this},unapply:function(e,t){if(this.hasInstance(e)){var n=this.__names__,r=[],i=0,s;for(;s=n[i];i++)r[i]=t?t(e[s],s):e[s];return r}},unapplyObject:function(e,t){if(this.hasInstance(e)){var n=this.__names__,r={},i=0,s;for(;s=n[i];i++)r[s]=t?t(e[s],s):e[s];return r}},seal:function(){return delete this.field,delete this.seal,this}},e.enumeration=function(){function r(e){return e.constructor&&(e=e.constructor),e.__order__=n++,e}var t=e.data.apply(null,arguments),n=0;for(var i=0,s;s=t.__names__[i];i++)r(t[s]);var o=t.type;return t.type=function(){return r(o.apply(t,arguments))},u(t.prototype,e.enumeration.__methods__),t},e["enum"]=e.enumeration,e.enumeration.__methods__={lt:function(e){return t(this,e)&&n(this)<n(e)},lte:function(e){return t(this,e)&&n(this)<=n(e)},gt:function(e){return t(this,e)&&n(this)>n(e)},gte:function(e){return t(this,e)&&n(this)>=n(e)},eq:function(e){return t(this,e)&&n(this)===n(e)},neq:function(e){return t(this,e)&&n(this)!==n(e)}},e.nativeClone=function(e){return e},e.nativeEquals=function(e,t){return e===t},e.newtype=function(){var t=r(arguments),n=e.data();return n.type.apply(n,t)},e.any=function(e){return e},e.only=function(){var e=arguments;return function(t,n,r){if(f(e,t))return t;var i="Unexpected type";throw n&&r&&(i+=" for `"+n+"` of "+r.className),new TypeError(i)}};var c=0})(typeof exports!="undefined"?exports:this.adt={});