Skip to content

Instantly share code, notes, and snippets.

@a-s-o
Last active October 3, 2015 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-s-o/ae046d8074c6d48fd8d2 to your computer and use it in GitHub Desktop.
Save a-s-o/ae046d8074c6d48fd8d2 to your computer and use it in GitHub Desktop.
requirebin sketch
const t = require('tcomb');
const Person = t.struct({ name: t.Str, money: t.Num }, 'Person');
Person.prototype.pay = function (amount) {
return Person.update(this, {
money: { $set: this.money + t.Num(amount) }
});
};
try {
const me = new Person({ name: 'me', money: 0 });
t.assert(me.pay(1).money === 1, 'ensure property is updated');
t.assert(me !== me.pay(1), 'return different instance if properties changed');
t.assert(me === me.pay(0), 'return same instance if properties did not change');
} catch (ex) {
document.getElementById('output').innerHTML = ex;
}
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var process=module.exports={};process.nextTick=function(){var canSetImmediate=typeof window!=="undefined"&&window.setImmediate;var canMutationObserver=typeof window!=="undefined"&&window.MutationObserver;var canPost=typeof window!=="undefined"&&window.postMessage&&window.addEventListener;if(canSetImmediate){return function(f){return window.setImmediate(f)}}var queue=[];if(canMutationObserver){var hiddenDiv=document.createElement("div");var observer=new MutationObserver(function(){var queueList=queue.slice();queue.length=0;queueList.forEach(function(fn){fn()})});observer.observe(hiddenDiv,{attributes:true});return function nextTick(fn){if(!queue.length){hiddenDiv.setAttribute("yes","no")}queue.push(fn)}}if(canPost){window.addEventListener("message",function(ev){var source=ev.source;if((source===window||source===null)&&ev.data==="process-tick"){ev.stopPropagation();if(queue.length>0){var fn=queue.shift();fn()}}},true);return function nextTick(fn){queue.push(fn);window.postMessage("process-tick","*")}}return function nextTick(fn){setTimeout(fn,0)}}();process.title="browser";process.browser=true;process.env={};process.argv=[];function noop(){}process.on=noop;process.addListener=noop;process.once=noop;process.off=noop;process.removeListener=noop;process.removeAllListeners=noop;process.emit=noop;process.binding=function(name){throw new Error("process.binding is not supported")};process.cwd=function(){return"/"};process.chdir=function(dir){throw new Error("process.chdir is not supported")}},{}],tcomb:[function(require,module,exports){(function(process){"use strict";exports.stringify=function stringify(x){try{return JSON.stringify(x,null,2)}catch(e){return String(x)}};function isNil(x){return x===null||x===void 0}function isBoolean(x){return x===true||x===false}function isString(x){return typeof x==="string"}function isNumber(x){return typeof x==="number"&&isFinite(x)&&!isNaN(x)}function isFunction(x){return typeof x==="function"}function isArray(x){return x instanceof Array}function isObject(x){return!isNil(x)&&typeof x==="object"&&!isArray(x)}function isType(x){return isFunction(x)&&isObject(x.meta)}function isStruct(x){return isType(x)&&x.meta.kind==="struct"}function isMaybe(x){return isType(x)&&x.meta.kind==="maybe"}function isUnion(x){return isType(x)&&x.meta.kind==="union"}function isTypeName(name){return isNil(name)||isString(name)}function is(x,type){if(isType(type)){return type.is(x)}return x instanceof type}function isIdentity(type){if(isType(type)){return type.meta.identity}return true}function create(type,value,path){if(isType(type)){return isStruct(type)?new type(value,path):type(value,path)}if(process.env.NODE_ENV!=="production"){path=path||[getFunctionName(type)];assert(value instanceof type,function(){return"Invalid value "+exports.stringify(value)+" supplied to "+path.join("/")})}return value}function getFunctionName(f){return f.displayName||f.name||"<function"+f.length+">"}function getTypeName(constructor){if(isType(constructor)){return constructor.displayName}return getFunctionName(constructor)}exports.fail=function fail(message){throw new TypeError("[tcomb] "+message)};function assert(guard,message){if(guard!==true){if(isFunction(message)){message=message()}else if(isNil(message)){message='Assert failed (turn on "Pause on exceptions" in your Source panel)'}exports.fail(message)}}function mixin(target,source,overwrite){if(isNil(source)){return target}for(var k in source){if(source.hasOwnProperty(k)){if(overwrite!==true){if(process.env.NODE_ENV!=="production"){assert(!target.hasOwnProperty(k),function(){return'Invalid call to mixin(target, source, [overwrite]): cannot overwrite property "'+k+'" of target object'})}}target[k]=source[k]}}return target}function forbidNewOperator(x,type){assert(!(x instanceof type),function(){return"Cannot use the new operator to instantiate the type "+getTypeName(type)})}function getShallowCopy(x){if(isArray(x)){return x.concat()}if(isObject(x)){return mixin({},x)}return x}function update(instance,spec){if(process.env.NODE_ENV!=="production"){assert(isObject(spec),function(){return"Invalid argument spec "+exports.stringify(spec)+" supplied to function update(instance, spec): expected an object containing commands"})}var value=getShallowCopy(instance);for(var k in spec){if(spec.hasOwnProperty(k)){if(update.commands.hasOwnProperty(k)){return update.commands[k](spec[k],value)}else{value[k]=update(value[k],spec[k])}}}return value}function $apply(f,value){if(process.env.NODE_ENV!=="production"){assert(isFunction(f),"Invalid argument f supplied to immutability helper { $apply: f } (expected a function)")}return f(value)}function $push(elements,arr){if(process.env.NODE_ENV!=="production"){assert(isArray(elements),"Invalid argument elements supplied to immutability helper { $push: elements } (expected an array)");assert(isArray(arr),"Invalid value supplied to immutability helper $push (expected an array)")}return arr.concat(elements)}function $remove(keys,obj){if(process.env.NODE_ENV!=="production"){assert(isArray(keys),"Invalid argument keys supplied to immutability helper { $remove: keys } (expected an array)");assert(isObject(obj),"Invalid value supplied to immutability helper $remove (expected an object)")}for(var i=0,len=keys.length;i<len;i++){delete obj[keys[i]]}return obj}function $set(value){return value}function $splice(splices,arr){if(process.env.NODE_ENV!=="production"){assert(list(Arr).is(splices),"Invalid argument splices supplied to immutability helper { $splice: splices } (expected an array of arrays)");assert(isArray(arr),"Invalid value supplied to immutability helper $splice (expected an array)")}return splices.reduce(function(acc,splice){acc.splice.apply(acc,splice);return acc},arr)}function $swap(config,arr){if(process.env.NODE_ENV!=="production"){assert(isObject(config),"Invalid argument config supplied to immutability helper { $swap: config } (expected an object)");assert(isNumber(config.from),"Invalid argument config.from supplied to immutability helper { $swap: config } (expected a number)");assert(isNumber(config.to),"Invalid argument config.to supplied to immutability helper { $swap: config } (expected a number)");assert(isArray(arr),"Invalid value supplied to immutability helper $swap (expected an array)")}var element=arr[config.to];arr[config.to]=arr[config.from];arr[config.from]=element;return arr}function $unshift(elements,arr){if(process.env.NODE_ENV!=="production"){assert(isArray(elements),"Invalid argument elements supplied to immutability helper {$unshift: elements} (expected an array)");assert(isArray(arr),"Invalid value supplied to immutability helper $unshift (expected an array)")}return elements.concat(arr)}function $merge(obj,value){return mixin(mixin({},value),obj,true)}update.commands={$apply:$apply,$push:$push,$remove:$remove,$set:$set,$splice:$splice,$swap:$swap,$unshift:$unshift,$merge:$merge};function irreducible(name,predicate){if(process.env.NODE_ENV!=="production"){assert(isString(name),function(){return"Invalid argument name "+exports.stringify(name)+" supplied to irreducible(name, predicate) (expected a string)"});assert(isFunction(predicate),"Invalid argument predicate "+exports.stringify(predicate)+" supplied to irreducible(name, predicate) (expected a function)")}function Irreducible(value,path){if(process.env.NODE_ENV!=="production"){forbidNewOperator(this,Irreducible);path=path||[name];assert(predicate(value),function(){return"Invalid value "+exports.stringify(value)+" supplied to "+path.join("/")})}return value}Irreducible.meta={kind:"irreducible",name:name,identity:true};Irreducible.displayName=name;Irreducible.is=predicate;return Irreducible}var Any=irreducible("Any",function(){return true});var Nil=irreducible("Nil",isNil);var Str=irreducible("String",isString);var Num=irreducible("Number",isNumber);var Bool=irreducible("Boolean",isBoolean);var Arr=irreducible("Array",isArray);var Obj=irreducible("Object",isObject);var Func=irreducible("Function",isFunction);var Err=irreducible("Error",function(x){return x instanceof Error});var Re=irreducible("RegExp",function(x){return x instanceof RegExp});var Dat=irreducible("Date",function(x){return x instanceof Date});function struct(props,name){if(process.env.NODE_ENV!=="production"){assert(dict(Str,Func).is(props),function(){return"Invalid argument props "+exports.stringify(props)+" supplied to struct(props, [name]) combinator (expected a dictionary String -> Type)"});assert(isTypeName(name),function(){return"Invalid argument name "+exports.stringify(name)+" supplied to struct(props, [name]) combinator (expected a string)"})}var displayName=name||struct.getDefaultName(props);function Struct(value,path){if(Struct.is(value)){return value}if(process.env.NODE_ENV!=="production"){path=path||[displayName];assert(isObject(value),function(){return"Invalid value "+exports.stringify(value)+" supplied to "+path.join("/")+" (expected an object)"})}if(!(this instanceof Struct)){return new Struct(value)}for(var k in props){if(props.hasOwnProperty(k)){var expected=props[k];var actual=value[k];this[k]=create(expected,actual,process.env.NODE_ENV!=="production"?path.concat(k+": "+getTypeName(expected)):null)}}if(process.env.NODE_ENV!=="production"){Object.freeze(this)}}Struct.meta={kind:"struct",props:props,name:name,identity:false};Struct.displayName=displayName;Struct.is=function(x){return x instanceof Struct};Struct.update=function(instance,spec){return new Struct(exports.update(instance,spec))};Struct.extend=function(structs,name){var props={};var prototype={};[Struct].concat(structs).forEach(function(struct,i){if(isObject(struct)){mixin(props,struct)}else{if(process.env.NODE_ENV!=="production"){assert(isStruct(struct),function(){return"Invalid argument structs["+i+"] "+exports.stringify(struct)+" supplied to "+displayName+".extend(structs, name)"})}mixin(props,struct.meta.props);mixin(prototype,struct.prototype)}});var ret=struct(props,name);mixin(ret.prototype,prototype);return ret};return Struct}struct.getDefaultName=function(props){return"{"+Object.keys(props).map(function(prop){return prop+": "+getTypeName(props[prop])}).join(", ")+"}"};function union(types,name){if(process.env.NODE_ENV!=="production"){assert(isArray(types)&&types.every(isFunction)&&types.length>=2,function(){return"Invalid argument types "+exports.stringify(types)+" supplied to union(types, [name]) combinator (expected an array of at least 2 types)"});assert(isTypeName(name),function(){return"Invalid argument name "+exports.stringify(name)+" supplied to union(types, [name]) combinator (expected a string)"})}var displayName=name||union.getDefaultName(types);var identity=types.every(isIdentity);function Union(value,path){if(process.env.NODE_ENV==="production"){if(identity){return value}}var type=Union.dispatch(value);if(process.env.NODE_ENV!=="production"){forbidNewOperator(this,Union);path=path||[displayName];assert(isType(type),function(){return"Invalid value "+exports.stringify(value)+" supplied to "+path.join("/")+" (no constructor found)"});assert(types.some(function(t){return t===type}),function(){return"Invalid constructor "+getTypeName(type)+" returned by "+path.join("/")+".dispatch(x) function"});path[path.length-1]+="("+getTypeName(type)+")"}return create(type,value,path)}Union.meta={kind:"union",types:types,name:name,identity:identity};Union.displayName=displayName;Union.is=function(x){return types.some(function(type){return is(x,type)})};Union.dispatch=function(x){for(var i=0,len=types.length;i<len;i++){var type=types[i];if(isUnion(type)){var t=type.dispatch(x);if(!isNil(t)){return t}}else if(is(x,type)){return type}}};Union.update=function(instance,spec){return Union(exports.update(instance,spec))};return Union}union.getDefaultName=function(types){return types.map(getTypeName).join(" | ")};function intersection(types,name){if(process.env.NODE_ENV!=="production"){assert(isArray(types)&&types.every(isFunction)&&types.length>=2,function(){return"Invalid argument types "+exports.stringify(types)+" supplied to intersection(types, [name]) combinator (expected an array of at least 2 types)"});assert(isTypeName(name),function(){return"Invalid argument name "+exports.stringify(name)+" supplied to intersection(types, [name]) combinator (expected a string)"})}var displayName=name||intersection.getDefaultName(types);function Intersection(value,path){if(process.env.NODE_ENV!=="production"){forbidNewOperator(this,Intersection);path=path||[displayName];assert(Intersection.is(value),function(){return"Invalid value "+exports.stringify(value)+" supplied to "+path.join("/")})}return value}Intersection.meta={kind:"intersection",types:types,name:name,identity:true};Intersection.displayName=displayName;Intersection.is=function(x){return types.every(function(type){return is(x,type)})};Intersection.update=function(instance,spec){return Intersection(exports.update(instance,spec))};return Intersection}intersection.getDefaultName=function(types){return types.map(getTypeName).join(" & ")};function maybe(type,name){if(isMaybe(type)||type===Any||type===Nil){return type}if(process.env.NODE_ENV!=="production"){assert(isFunction(type),function(){return"Invalid argument type "+exports.stringify(type)+" supplied to maybe(type, [name]) combinator (expected a type)"});assert(isTypeName(name),function(){return"Invalid argument name "+exports.stringify(name)+" supplied to maybe(type, [name]) combinator (expected a string)"})}var displayName=name||maybe.getDefaultName(type);function Maybe(value,path){if(process.env.NODE_ENV!=="production"){forbidNewOperator(this,Maybe)}return isNil(value)?null:create(type,value,path)}Maybe.meta={kind:"maybe",type:type,name:name,identity:isIdentity(type)};Maybe.displayName=displayName;Maybe.is=function(x){return isNil(x)||is(x,type)};return Maybe}maybe.getDefaultName=function(type){return"?"+getTypeName(type)};function enums(map,name){if(process.env.NODE_ENV!=="production"){assert(isObject(map),function(){return"Invalid argument map "+exports.stringify(map)+" supplied to enums(map, [name]) combinator (expected a dictionary of String -> String | Number)"});assert(isTypeName(name),function(){return"Invalid argument name "+exports.stringify(name)+" supplied to enums(map, [name]) combinator (expected a string)"})}var displayName=name||enums.getDefaultName(map);function Enums(value,path){if(process.env.NODE_ENV!=="production"){forbidNewOperator(this,Enums);path=path||[displayName];assert(Enums.is(value),function(){return"Invalid value "+exports.stringify(value)+" supplied to "+path.join("/")+" (expected one of "+exports.stringify(Object.keys(map))+")"})}return value}Enums.meta={kind:"enums",map:map,name:name,identity:true};Enums.displayName=displayName;Enums.is=function(x){return map.hasOwnProperty(x)};return Enums}enums.getDefaultName=function(map){return Object.keys(map).map(function(k){return exports.stringify(k)}).join(" | ")};enums.of=function(keys,name){keys=isString(keys)?keys.split(" "):keys;var value={};keys.forEach(function(k){value[k]=k});return enums(value,name)};function tuple(types,name){if(process.env.NODE_ENV!=="production"){assert(isArray(types)&&types.every(isFunction),function(){return"Invalid argument types "+exports.stringify(types)+" supplied to tuple(types, [name]) combinator (expected an array of types)"});assert(isTypeName(name),function(){return"Invalid argument name "+exports.stringify(name)+" supplied to tuple(types, [name]) combinator (expected a string)"})}var displayName=name||tuple.getDefaultName(types);var identity=types.every(isIdentity);function Tuple(value,path){if(process.env.NODE_ENV==="production"){if(identity){return value}}if(process.env.NODE_ENV!=="production"){path=path||[displayName];assert(isArray(value)&&value.length===types.length,function(){return"Invalid value "+exports.stringify(value)+" supplied to "+path.join("/")+" (expected an array of length "+types.length+")"})}var idempotent=true;var ret=[];for(var i=0,len=types.length;i<len;i++){var expected=types[i];var actual=value[i];var instance=create(expected,actual,process.env.NODE_ENV!=="production"?path.concat(i+": "+getTypeName(expected)):null);idempotent=idempotent&&actual===instance;ret.push(instance)}if(idempotent){ret=value}if(process.env.NODE_ENV!=="production"){Object.freeze(ret)}return ret}Tuple.meta={kind:"tuple",types:types,name:name,identity:identity};Tuple.displayName=displayName;Tuple.is=function(x){return isArray(x)&&x.length===types.length&&types.every(function(type,i){return is(x[i],type)})};Tuple.update=function(instance,spec){return Tuple(exports.update(instance,spec))};return Tuple}tuple.getDefaultName=function(types){return"["+types.map(getTypeName).join(", ")+"]"};function refinement(type,predicate,name){if(process.env.NODE_ENV!=="production"){assert(isFunction(type),function(){return"Invalid argument type "+exports.stringify(type)+" supplied to refinement(type, predicate, [name]) combinator (expected a type)"});assert(isFunction(predicate),function(){return"Invalid argument predicate supplied to refinement(type, predicate, [name]) combinator (expected a function)"});assert(isTypeName(name),function(){return"Invalid argument name "+exports.stringify(name)+" supplied to refinement(type, predicate, [name]) combinator (expected a string)"})}var displayName=name||refinement.getDefaultName(type,predicate);var identity=isIdentity(type);function Refinement(value,path){if(process.env.NODE_ENV!=="production"){forbidNewOperator(this,Refinement);path=path||[displayName]}var x=create(type,value,path);if(process.env.NODE_ENV!=="production"){assert(predicate(x),function(){return"Invalid value "+exports.stringify(value)+" supplied to "+path.join("/")})}return x}Refinement.meta={kind:"subtype",type:type,predicate:predicate,name:name,identity:identity};Refinement.displayName=displayName;Refinement.is=function(x){return is(x,type)&&predicate(x)};Refinement.update=function(instance,spec){return Refinement(exports.update(instance,spec))};return Refinement}refinement.getDefaultName=function(type,predicate){return"{"+getTypeName(type)+" | "+getFunctionName(predicate)+"}"};function list(type,name){if(process.env.NODE_ENV!=="production"){assert(isFunction(type),function(){return"Invalid argument type "+exports.stringify(type)+" supplied to list(type, [name]) combinator (expected a type)"});assert(isTypeName(name),function(){return"Invalid argument name "+exports.stringify(name)+" supplied to list(type, [name]) combinator (expected a string)"})}var displayName=name||list.getDefaultName(type);var typeNameCache=getTypeName(type);var identity=isIdentity(type);function List(value,path){if(process.env.NODE_ENV==="production"){if(identity){return value}}if(process.env.NODE_ENV!=="production"){path=path||[displayName];assert(isArray(value),function(){return"Invalid value "+exports.stringify(value)+" supplied to "+path.join("/")+" (expected an array of "+typeNameCache+")"})}var idempotent=true;var ret=[];for(var i=0,len=value.length;i<len;i++){var actual=value[i];var instance=create(type,actual,process.env.NODE_ENV!=="production"?path.concat(i+": "+typeNameCache):null);idempotent=idempotent&&actual===instance;ret.push(instance)}if(idempotent){ret=value}if(process.env.NODE_ENV!=="production"){Object.freeze(ret)}return ret}List.meta={kind:"list",type:type,name:name,identity:identity};List.displayName=displayName;List.is=function(x){return isArray(x)&&x.every(function(e){return is(e,type)})};List.update=function(instance,spec){return List(exports.update(instance,spec))};return List}list.getDefaultName=function(type){return"Array<"+getTypeName(type)+">"};function dict(domain,codomain,name){if(process.env.NODE_ENV!=="production"){assert(isFunction(domain),function(){return"Invalid argument domain "+exports.stringify(domain)+" supplied to dict(domain, codomain, [name]) combinator (expected a type)"});assert(isFunction(codomain),function(){return"Invalid argument codomain "+exports.stringify(codomain)+" supplied to dict(domain, codomain, [name]) combinator (expected a type)"});assert(isTypeName(name),function(){return"Invalid argument name "+exports.stringify(name)+" supplied to dict(domain, codomain, [name]) combinator (expected a string)"})}var displayName=name||dict.getDefaultName(domain,codomain);var domainNameCache=getTypeName(domain);var codomainNameCache=getTypeName(codomain);var identity=isIdentity(domain)&&isIdentity(codomain);function Dict(value,path){if(process.env.NODE_ENV==="production"){if(identity){return value}}if(process.env.NODE_ENV!=="production"){path=path||[displayName];assert(isObject(value),function(){return"Invalid value "+exports.stringify(value)+" supplied to "+path.join("/")})}var idempotent=true;var ret={};for(var k in value){if(value.hasOwnProperty(k)){k=create(domain,k,process.env.NODE_ENV!=="production"?path.concat(domainNameCache):null);var actual=value[k];var instance=create(codomain,actual,process.env.NODE_ENV!=="production"?path.concat(k+": "+codomainNameCache):null);idempotent=idempotent&&actual===instance;ret[k]=instance}}if(idempotent){ret=value}if(process.env.NODE_ENV!=="production"){Object.freeze(ret)}return ret}Dict.meta={kind:"dict",domain:domain,codomain:codomain,name:name,identity:identity};Dict.displayName=displayName;Dict.is=function(x){if(!isObject(x)){return false}for(var k in x){if(x.hasOwnProperty(k)){if(!is(k,domain)||!is(x[k],codomain)){return false}}}return true};Dict.update=function(instance,spec){return Dict(exports.update(instance,spec))};return Dict}dict.getDefaultName=function(domain,codomain){return"{[key: "+getTypeName(domain)+"]: "+getTypeName(codomain)+"}"};function isInstrumented(f){return isFunction(f)&&isObject(f.instrumentation)}function func(domain,codomain,name){domain=isArray(domain)?domain:[domain];if(process.env.NODE_ENV!=="production"){assert(list(Func).is(domain),function(){return"Invalid argument domain "+exports.stringify(domain)+" supplied to func(domain, codomain, [name]) combinator (expected an array of types)"});assert(isFunction(codomain),function(){return"Invalid argument codomain "+exports.stringify(codomain)+" supplied to func(domain, codomain, [name]) combinator (expected a type)"});assert(isTypeName(name),function(){return"Invalid argument name "+exports.stringify(name)+" supplied to func(domain, codomain, [name]) combinator (expected a string)"})}var displayName=name||func.getDefaultName(domain,codomain);function FuncType(value,curried){if(!isInstrumented(value)){return FuncType.of(value,curried)}if(process.env.NODE_ENV!=="production"){assert(FuncType.is(value),function(){return"Invalid value "+exports.stringify(value)+" supplied to "+displayName})}return value}FuncType.meta={kind:"func",domain:domain,codomain:codomain,name:name};FuncType.displayName=displayName;FuncType.is=function(x){return isInstrumented(x)&&x.instrumentation.domain.length===domain.length&&x.instrumentation.domain.every(function(type,i){return type===domain[i]})&&x.instrumentation.codomain===codomain};FuncType.of=function(f,curried){if(process.env.NODE_ENV!=="production"){assert(isFunction(f),function(){return"Invalid argument f supplied to func.of "+displayName+" (expected a function)"});assert(isNil(curried)||isBoolean(curried),function(){return"Invalid argument curried "+exports.stringify(curried)+" supplied to func.of "+displayName+" (expected a boolean)"})}if(FuncType.is(f)){return f}function fn(){var args=Array.prototype.slice.call(arguments);var len=curried?args.length:domain.length;var argsType=tuple(domain.slice(0,len));args=argsType(args);if(len===domain.length){return create(codomain,f.apply(this,args))}else{var g=Function.prototype.bind.apply(f,[this].concat(args));var newdomain=func(domain.slice(len),codomain);return newdomain.of(g,curried)}}fn.instrumentation={domain:domain,codomain:codomain,f:f};fn.displayName=getFunctionName(f);return fn};return FuncType}func.getDefaultName=function(domain,codomain){return"("+domain.map(getTypeName).join(", ")+") => "+getTypeName(codomain)};function match(x){var type,guard,f,count;for(var i=1,len=arguments.length;i<len;){type=arguments[i];guard=arguments[i+1];f=arguments[i+2];if(isFunction(f)&&!isType(f)){i=i+3}else{f=guard;guard=Any.is;i=i+2}if(process.env.NODE_ENV!=="production"){count=(count||0)+1;assert(isType(type),function(){return"Invalid type in clause #"+count});assert(isFunction(guard),function(){return"Invalid guard in clause #"+count});assert(isFunction(f),function(){return"Invalid block in clause #"+count})}if(type.is(x)&&guard(x)){return f(x)}}exports.fail("Match error")}mixin(exports,{is:is,isType:isType,getTypeName:getTypeName,mixin:mixin,update:update,assert:assert,Any:Any,Nil:Nil,Str:Str,String:Str,Num:Num,Number:Num,Bool:Bool,Boolean:Bool,Arr:Arr,Array:Arr,Obj:Obj,Object:Obj,Func:Func,Function:Func,Err:Err,Error:Err,Re:Re,RegExp:Re,Dat:Dat,Date:Dat,irreducible:irreducible,struct:struct,enums:enums,union:union,maybe:maybe,tuple:tuple,subtype:refinement,refinement:refinement,list:list,dict:dict,func:func,intersection:intersection,match:match})}).call(this,require("_process"))},{_process:1}]},{},[]);const t=require("tcomb");const Person=t.struct({name:t.Str,money:t.Num},"Person");Person.prototype.pay=function(amount){return Person.update(this,{money:{$set:this.money+t.Num(amount)}})};try{const me=new Person({name:"me",money:0});t.assert(me.pay(1).money===1,"ensure property is updated");t.assert(me!==me.pay(1),"return different instance if properties changed");t.assert(me===me.pay(0),"return same instance if properties did not change")}catch(ex){document.getElementById("output").innerHTML=ex}
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"tcomb": "2.5.0"
}
}
<pre id="output" style="background: #E58900; color: #FFFFFF; padding: 2rem; margin: 0;">
// No errors
</pre>
<!-- contents of this file will be placed inside the <head> -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment