UNPKG

@lengin/hay

Version:

Run-time checking of javascript types

1 lines 3.39 kB
!function(t){var e={};function r(n){if(e[n])return e[n].exports;var a=e[n]={i:n,l:!1,exports:{}};return t[n].call(a.exports,a,a.exports,r),a.l=!0,a.exports}r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var a in t)r.d(n,a,function(e){return t[e]}.bind(null,a));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,r){t.exports=r(1)},function(t,e,r){"use strict";r.r(e);var n={};r.r(n),r.d(n,"number",function(){return s}),r.d(n,"string",function(){return u}),r.d(n,"bool",function(){return i}),r.d(n,"object",function(){return c}),r.d(n,"array",function(){return f}),r.d(n,"func",function(){return l}),r.d(n,"oneOf",function(){return y}),r.d(n,"arrayOf",function(){return d}),r.d(n,"shapeOf",function(){return g}),r.d(n,"parse",function(){return a});const a=function(t){let e;if(t&&"object"==typeof t&&!Array.isArray(t)){let r={};Object.keys(t).map((e,n)=>{r[e]=a(t[e])}),e={target:r}}else e=Array.isArray(t)?{target:t.map(t=>a(t))}:{target:t};return e},o=(t,e,r)=>(t&&t.push({obj:e,type:r,message:`Expect '${e}' to be 'type:${r}'`}),!1),s=(t,e)=>t&&(p(t.target)||"number"==typeof t.target)||o(e,t,"number"),u=(t,e)=>t&&(p(t.target)||"string"==typeof t.target)||o(e,t,"string"),i=(t,e)=>t&&(p(t.target)||"boolean"==typeof t.target)||o(e,t,"boolean"),c=(t,e)=>t&&(p(t.target)||"object"==typeof t.target&&!f(t))||o(e,t,"object"),f=(t,e)=>t&&(p(t.target)||Array.isArray(t.target))||o(e,t,"array"),l=(t,e)=>t&&(p(t.target)||"[object Function]"==={}.toString.call(t.target))||o(e,t,"function");f.withGeneratorFunc=(t=>e=>(r,n)=>{if(!(e instanceof function*(){}.constructor))throw"Invalid generator function";let a=e();return t(r,n)&&r.target.every(t=>a.next().value(t))})(f);const y=t=>(e,r)=>t.some((t,n)=>t(e,r)),g=t=>(e,r)=>!p(e.target)&&c(e)&&Object.keys(t).map((n,a)=>{return t[n](e.target[n],r)}).every(t=>t)||l({target:t})&&t(e,r)||Array.isArray(e)&&Array.isArray(t)&&e.map((e,r)=>t[r](e)),p=t=>null==t,d=t=>(e,r)=>f(e)&&e.target.every((e,n)=>t(e,r));[s,u,i,c,f,l].forEach(t=>{t.isRequired=(t=>(e,r)=>(e&&!p(e.target,r)||((t,e)=>(t&&t.push({obj:e,message:"Required but received empty."}),!1))(r,e))&&t(e,r))(t)}),r.d(e,"hay",function(){return b}),r.d(e,"HayTypes",function(){return m});const b=new class{constructor(){this.opts={class:"highlighted"}}wrap(t,e){return`<span class='${e.class}'>${t}</span>`}check(t,e){this._messages=[],this._display={};let r=m.parse(t);return this.outlet={messages:this._messages,display:this._display,original:t,parsed:r},Array.isArray(e)?e.every(t=>t(r,this._messages)):e(r,this._messages)}getMessages(){let t=this.outlet.messages.map(t=>t.obj).filter(t=>!!t);return[this.outlet.messages,JSON.stringify(this.outlet.parsed,(e,r)=>t.some(t=>t===r)?this.wrap(r.target,this.opts):r.target," ")]}},m={...n};window.Hay=b,window.HayTypes=m}]);