letype
Version:
Type checker for any data structures
10 lines (9 loc) • 3.98 kB
JavaScript
var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.owns=function(a,e){return Object.prototype.hasOwnProperty.call(a,e)};$jscomp.assign="function"==typeof Object.assign?Object.assign:function(a,e){for(var d=1;d<arguments.length;d++){var f=arguments[d];if(f)for(var g in f)$jscomp.owns(f,g)&&(a[g]=f[g])}return a};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;
$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,e,d){a!=Array.prototype&&a!=Object.prototype&&(a[e]=d.value)};$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global&&null!=global?global:a};$jscomp.global=$jscomp.getGlobal(this);
$jscomp.polyfill=function(a,e,d,f){if(e){d=$jscomp.global;a=a.split(".");for(f=0;f<a.length-1;f++){var g=a[f];g in d||(d[g]={});d=d[g]}a=a[a.length-1];f=d[a];e=e(f);e!=f&&null!=e&&$jscomp.defineProperty(d,a,{configurable:!0,writable:!0,value:e})}};$jscomp.polyfill("Object.assign",function(a){return a||$jscomp.assign},"es6","es3");
(function(a,e){"object"===typeof exports&&"undefined"!==typeof module?e(exports):"function"===typeof define&&define.amd?define(["exports"],e):(a=a||self,e(a.Letype={}))})(this,function(a){function e(c,b,a,k){void 0===a&&(a=[]);void 0===k&&(k=!0);if("function"===typeof b&&b.prototype instanceof f){var h=(new b).parse;if("function"===typeof h)return h(c)}h="function"===typeof b?b():b;var d=typeof h,m=void 0!==c&&null!==c&&c.constructor===b;if(b===Date)return m;if(b&&b.constructor===RegExp&&h instanceof
RegExp)return(b=h.test(c))||k||console.error("Type error: Expected "+(0<a.length?"`"+a.join(".")+"`":"value")+" as `"+c+"` to pass regexp `"+h+"`"),b;if(b===RegExp)return m;if(m)return!0;if("object"===d&&"object"===typeof c){h=!0;if(b&&b instanceof Function&&"Array"===b.name&&b.constructor instanceof Function)return(c&&c.constructor)===(b&&b.constructor);if(b instanceof Array&&1===b.length){if(!(c&&-1<c.length))return!1;for(var g in c)if({}.hasOwnProperty.call(c,g)){var l=e(c[g],b[0],a.concat(g),
k);h&&(h=l)}}else for(l in c=Object.assign({},b,c),c)({}).hasOwnProperty.call(c,l)&&(g=e(c[l],b[l],a.concat(l),k),h&&(h=g));return h}(b=typeof c===d)||(void 0===c?k||console.error("Type error: "+(0<a.length?"`"+a.join(".")+"`":"Value")+" is undefined! Required value of type `"+d+"`"):void 0===h?k||console.error("Type error: "+(0<a.length?"`"+a.join(".")+"`":"Value")+" is defined as `"+c+"`! But it should not be defined at all!"):k||console.error("Type error: `"+c+"` is not of type `"+d+"`"+(0<a.length?
" in `"+a.join(".")+"`":"")));return b}function d(a){for(var b=[],c=arguments.length-1;0<c--;)b[c]=arguments[c+1];if(b&&1===b.length){if(!e(a,b[0],[],!0))throw new TypeError("Type checker found some type mismatches!");}else if(b&&1<b.length){var d=!1;b.forEach(function(b){return!d&&(d=e(a,b,[],!0))});if(!d)throw new TypeError("`"+a+"` is not any of types: "+b.map(function(b){return"`"+b.name+"`"}).join(", "));}return!0}var f=function(){},g=function(a){function b(){a.apply(this,arguments)}a&&(b.__proto__=
a);b.prototype=Object.create(a&&a.prototype);b.prototype.constructor=b;b.prototype.parse=function(){return!0};return b}(f),n=function(a){function b(){a.apply(this,arguments)}a&&(b.__proto__=a);b.prototype=Object.create(a&&a.prototype);b.prototype.constructor=b;b.prototype.parse=function(a){return"undefined"===typeof a};return b}(f);g={Any:g,Or:function(){for(var a=[],b=arguments.length;b--;)a[b]=arguments[b];return function(b){function c(){b.apply(this,arguments)}b&&(c.__proto__=b);c.prototype=Object.create(b&&
b.prototype);c.prototype.constructor=c;c.prototype.parse=function(b){return d.apply(void 0,[b].concat(a))};return c}(f)},Custom:f,Undefined:n};a.assert=d;a.check=function(a){for(var b=[],c=arguments.length-1;0<c--;)b[c]=arguments[c+1];try{d.apply(void 0,[a].concat(b))}catch(k){return!1}return!0};a.types=g;Object.defineProperty(a,"__esModule",{value:!0})});