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 37
/
matches.min.js
9 lines (9 loc) · 11.5 KB
/
matches.min.js
1
2
3
4
5
6
7
8
9
// matches.js
// ----------
// Powerful pattern matching for Javascript
//
// version : 0.5.1
// author : Nathan Faubion <nathan@n-son.com>
// repo : https://github.com/natefaubion/matches.js
// license : MIT
(function(e,t){"use strict";function n(e){var t=new r(e),n=i(t);return n}function r(e){this.input=e,this.buffer=e,this.pos=0}function i(e){var t=s(e.skipWs());return e.skipWs().buffer&&Nt(e),t}function s(e){return ot(u(e))}function o(e){return k(f,e)}function u(e){return k(a,e,L())}function a(e){return l(e)||f(e)}function f(e){return c(e)||h(e)||p(e)||d(e)||v(e)||m(e)||g(e)||y(e)||b(e)||w(e)||T(e)}function l(e){var t=e.takeAPeek("...");if(t)return ut(f(e)||at())}function c(e){if(e.takeAPeek("_"))return at()}function h(e){if(e.takeAPeek(R))return ft()}function p(e){if(e.takeAPeek(q))return lt()}function d(e){var t=e.takeAPeek(U);if(t)return ct(t[0])}function v(e){var t=B(e);if(t)return ht(t)}function m(e){var t=A(e);if(t)return pt(t)}function g(e){var t=e.takeAPeek(X);if(t){var n=t[0],r=w(e);return r?wt(n,r):(r=C("(",")",u,dt,e),r?wt(n,r):wt(n))}}function y(e){function n(e){return Et(t[1],e)}var t=e.takeAPeek(V);if(t)return C("(",")",f,n,e)||Et(t[1])}function b(e){return C("[","]",u,dt,e)}function w(e){return C("{","}",E,vt,e)}function E(e){return k(S,e)}function S(e){var t=x(e);if(t){if(e.skipWs().takeAPeek(":")){var n=f(e.skipWs());if(n)return bt(t,n)}return yt(t)}}function x(e){var t=A(e);if(t)return t;var n=e.takeAPeek(W);if(n)return n[0]}function T(e){var t=e.takeAPeek(z);if(t){if(e.takeAPeek("@")){var n=N(e);if(n)return mt(t[0],n)}return gt(t[0])}}function N(e){return g(e)||b(e)||w(e)}function C(e,t,n,r,i){if(i.takeAPeek(e)){var s=n(i.skipWs());if(i.skipWs().takeAPeek(t))return r(s);Nt(i,"Expected "+t)}}function k(e,t,n){var r=[],i;for(;;){i=e(t);if(!(i&&!n||i&&n(i,t)))break;r.push(i);if(!t.skipWs().takeAPeek(","))break;t.skipWs()}return r}function L(){var e=0;return function(t,n){return t.type==="rest"&&++e>1&&(n.put(t.pattern),Nt(n,"Multiple ...'s not allowed")),!0}}function A(e){return O('"',$,e)||O("'",J,e)}function O(e,t,n){if(n.takeAPeek(e)){var r=M(t,n);if(n.takeAPeek(e))return r;Nt(n,"Expected "+e)}}function M(e,t){var n="";for(;;){var r=t.takeAPeek(e);if(r)n+=r;else{if(!t.peek("\\"))break;r=_(t);if(!r)break;n+=r}}return n}function _(e){return D(e)||P(Z,e)||P(Y,e)||H(e)}function D(e){if(e.takeAPeek(G))return"\0"}function P(e,t){var n=t.takeAPeek(e);if(n)return String.fromCharCode(parseInt("0x"+n[1]))}function H(e){var t=e.takeAPeek(Q);if(t)return t[1].replace("b","\b").replace("f","\f").replace("n","\n").replace("r","\r").replace("t"," ").replace("v","");t=e.takeAPeek(K);if(t)return t[1]}function B(e){var t="";e.takeAPeek("-")&&(t+="-");var n=I(e);return n&&(t+=n),n=j(e),n&&(t+=n),t&&t!=="-"&&(n=F(e),n&&(t+=n)),t==="-"&&Nt(e,"Expected number"),t||null}function j(e){if(e.takeAPeek(".")){var t=e.takeAPeek(rt);if(t)return"."+t;Nt(e,"Expected digit")}}function F(e){var t=e.takeAPeek(et);if(t){var n=e.takeAPeek(rt);if(n)return t[0]+n}}function I(e){return e.takeAPeek(nt)||e.takeAPeek(it)}function st(e,t,n,r){var i={pattern:t,type:e};return n!==undefined&&(i.value=n),r!==undefined&&(i.children=r),i}function ot(e){return st("argumentList",St(e).join(","),undefined,e)}function ut(e){return st("rest","..."+e.pattern,undefined,[e])}function at(){return st("wildcard","_")}function ft(){return st("null","null")}function lt(){return st("undefined","undefined")}function ct(e){return st("boolean",e,e==="true")}function ht(e){return st("number",e,parseFloat(e))}function pt(e){return st("string",xt(e),e)}function dt(e){return st("array","["+St(e).join(",")+"]",undefined,e)}function vt(e){return st("object","{"+St(e).join(",")+"}",undefined,e)}function mt(e,t){return st("binder",e+"@"+t.pattern,e,[t])}function gt(e){return st("identifier",e,e)}function yt(e){return e=xt(e),st("key",e,e)}function bt(e,t){return e=xt(e),st("keyValue",e+":"+t.pattern,e,[t])}function wt(e,t){var n=e;return t&&(n+=t.type==="array"?"("+t.pattern.substring(1,t.pattern.length-1)+")":t.pattern),st("class",n,e,t?[t]:undefined)}function Et(e,t){return t?st("extractor","$"+e+"("+t.pattern+")",e,[t]):st("extractor","$"+e,e)}function St(e){return e.map(function(e){return e.pattern})}function xt(e){return'"'+e.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\x08/g,"\\b").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\f/g,"\\f").replace(/\r/g,"\\r").replace(/[\x00-\x07\x0B\x0E-\x1F\x80-\uFFFF]/g,Tt)+'"'}function Tt(e){var t=e.charCodeAt(0),n,r;t<=255?(n="x",r=2):(n="u",r=4);var i=t.toString(16);return"\\"+n+Array(r-i.length).join("0")+i}function Nt(e,t){throw t||(t="Unexpected character"),t+=" at column "+(e.pos+1),new SyntaxError(t+"\n"+e.input+"\n"+Array(e.input.length-e.buffer.length+1).join(" ")+"^")}function Ct(e){return typeof e=="string"&&(e=n(e)),new Function(["args","runt"],["var ret = [];",Lt(e),"return ret;"].join("\n"))}function Lt(e){return Pt("args",e)}function At(e,t){return kt[t.type](e,t)}function Ot(){return""}function Mt(e,t){return"if ("+e+" !== "+t.pattern+") return false;"}function _t(e){return"ret[ret.length] = "+e+";"}function Dt(e,t){var n=[_t(e),At(e,t.children[0])];return n.join("\n")}function Pt(e,t){return Rt(t)?Bt(e,t):Ht(e,t)}function Ht(e,t){var n=t.children.length,r=["if (!("+e+" instanceof Array) || "+e+".length !== "+n+") return false;"],i=0,s=t.children.length,o;for(;i<s;i++)o=e+"_"+i,r.push("var "+o+" = "+e+"["+i+"];",At(o,t.children[i]));return r.join("\n")}function Bt(e,t){var n=t.children.length,r=n-1,i=e+"_pos",s=e+"_rest",o=["if (!("+e+" instanceof Array) ||"+e+".length < "+r+") return false;","var "+i+" = 0;","var "+s+";"],u=0,a=t.children.length,f,l,c;for(;u<a;u++){l=t.children[u],f=e+"_"+u;if(l.type!=="rest"){o.push("var "+f+" = "+e+"["+i+"++];",At(f,l));continue}u===r?o.push(s+" = "+e+".slice("+u+");"):u===0?o.push(i+" = "+e+".length - "+r+";",s+" = "+e+".slice(0, "+i+");"):o.push(i+" = "+e+".length - "+(r-u)+";",s+" = "+e+".slice("+u+", "+i+");"),c=l.children[0].type,c==="identifier"?o.push(_t(s)):c!=="wildcard"&&o.push(qt(s,l))}return o.join("\n")}function jt(e,t){var n=["if (!("+e+" instanceof Object)) return false;"],r=0,i=t.children.length,s,o;for(;r<i;r++)o=t.children[r],s=e+"_"+r,n.push("if (!("+o.value+" in "+e+")) return false;"),o.type==="key"?n.push(_t(e+"["+o.value+"]")):n.push("var "+s+" = "+e+"["+o.value+"];",At(s,o.children[0]));return n.join("\n")}function Ft(e,t){var n=["if (!runt.matchesTypeName("+e+', "'+t.value+'")) return false;'];if(t.children){var r=t.children[0].type==="array",i=r?"unapply":"unapplyObj",s=r?Pt:jt,o=e+"_vals";n.push("if (!"+e+".constructor || "+"!"+e+".constructor."+i+") return false;","var "+o+" = "+e+".constructor."+i+"("+e+");",s(o,t.children[0]))}return n.join("\n")}function It(e,t){var n=e+"_ext",r=e+"_val",i=["var "+n+' = runt.callExtractor("'+t.value+'", '+e+");","if (!"+n+" || !("+n+" instanceof runt.Pass)) return false;"];return t.children&&(i.push("var "+r+" = "+n+".val;"),i.push(At(r,t.children[0]))),i.join("\n")}function qt(e,t){var n=[],r=[],i=0,s=Ut(t);for(;i<s;i++)r.push("[]");var o=e+"_ret",u=e+"_loop",a=e+"_i",f=e+"_len",l=e+"_retargs";return n.push("var "+o+" = ["+r.join(",")+"];","var "+u+" = function (val) {"," var ret = [];",zt(2,At("val",t.children[0]))," return ret;","};","var "+a+" = 0, "+f+" = "+e+".length, "+l+";","for (; "+a+" < "+f+"; "+a+"++) {"," "+l+" = "+u+"("+e+"["+a+"]);"," if (!"+l+") return false;",function(){var e=[];for(i=0;i<s;i++)e.push(" "+o+"["+i+"].push("+l+"["+i+"]);");return e.join("\n")}(),"}","ret = Array.prototype.concat.call(ret, "+o+");"),n.join("\n")}function Rt(e){for(var t=0,n;n=e.children[t];t++)if(n.type==="rest")return!0;return!1}function Ut(e){if(!e.children)return 0;var t=0,n=0,r=e.children.length,i;for(;n<r;n++)i=e.children[n].type,i==="identifier"||i==="binder"||i==="key"?t+=1:t+=Ut(e.children[n]);return t}function zt(e,t){return t.replace(/^/gm,Array(e+1).join(" "))}function Vt(e,t,n){this.patternFn=e,this.successFn=t,this.next=n}function Qt(e){var t,r;return Jt.hasOwnProperty(e)||(t=n(e),Kt.hasOwnProperty(t.pattern)||(r=Ct(t),r.pattern=t.pattern,Kt[t.pattern]=r),Jt[e]=Kt[t.pattern]),Jt[e]}function Gt(){var e=typeof arguments[0],t=typeof arguments[1],n=typeof arguments[2],r,i,s,o,u,a,f,l;if(e!="function"||t!="undefined"&&t!="object"){if(e!="object"||t!="undefined"&&t!="object")return e=="function"&&t=="function"?(a=arguments[2]?arguments[2].clone():null,Yt(arguments[0],arguments[1],a)):(o=arguments[0],u=arguments[1],a=arguments[2]?arguments[2].clone():null,s=Qt(o),Yt(s,u,a));i=arguments[0],a=arguments[1]?arguments[1].clone():null;for(o in i)r=Gt(o,i[o],a),a=r.__matchChain;return r}r=arguments[0],a=arguments[1];if(!r.__matchChain)throw new Error("Not a matcher function");return a?(a=a.clone(),a.last().next=r.__matchChain.clone()):a=r.__matchChain.clone(),l=a.pop(),Yt(l.patternFn,l.successFn,a)}function Yt(e,t,n){var r=new Vt(e,t);n?n.last().next=r:n=r;var i=function(){var e=[],t=0,r=arguments.length;for(;t<r;t++)e[t]=arguments[t];return n.match(e,this)};return i.alt=function(){var e=$t.call(arguments);return e.push(n),Gt.apply(null,e)},i.__matchChain=n,i}function Zt(){var e=$t.call(arguments,0,-1),t=arguments[arguments.length-1],n=this===exports?null:this;if(typeof t=="function"){if(!t.__matchChain)throw new Error("Not a matcher function");return t.apply(n,e)}return Gt(t).apply(n,e)}function en(){var e=$t.call(arguments,1),t=this===exports?null:this,n=Qt(arguments[0]);return n.call(t,e,Wt)||null}function tn(){var e=en.apply(this,arguments);return e===null?void 0:e[0]}r.prototype.take=function(e){var t=this.buffer.substr(0,e);return this.buffer=this.buffer.substring(e),this.pos+=t.length,t},r.prototype.peek=function(e){return e instanceof RegExp?this.buffer.match(e):this.buffer.substr(0,e.length)===e?e:null},r.prototype.takeAPeek=function(e){var t=this.peek(e);if(t)return this.take((e instanceof RegExp?t[0]:t).length),t},r.prototype.skipWs=function(){while(this.takeAPeek(" "))continue;return this},r.prototype.put=function(e){this.buffer=e+this.buffer;var t=this.pos-e.length;return this.pos=t<0?0:t,this};var q=/^(undefined)\b/,R=/^(null)\b/,U=/^(true|false)\b/,z=/^[a-z][_$a-zA-Z0-9]*/,W=/^[_$a-zA-Z][_$a-zA-Z0-9]*/,X=/^[A-Z][_$a-zA-Z0-9]*/,V=/^\$([_$a-zA-Z][_$a-zA-Z0-9]*)/,$=/^(?!["\\])./,J=/^(?!['\\])./,K=/^\\(.)/,Q=/^\\(['"\\bfnrtv])/,G=/^\\0(?![0-9])/,Y=/^\\u([0-9a-fA-F]{4})/,Z=/^\\x([0-9a-fA-F]{2})/,et=/^[eE](\+|-)?/,tt=/^[0-9a-fA-F]/,nt=/^[1-9][0-9]+/,rt=/^[0-9]+/,it=/^[0-9]/,kt={wildcard:Ot,"null":Mt,"undefined":Mt,"boolean":Mt,number:Mt,string:Mt,identifier:_t,binder:Dt,array:Pt,object:jt,"class":Ft,extractor:It},Wt={},Xt={};Wt.matchesTypeName=function(e,t){var n=Object.prototype.toString.call(e);if(n.substring(8,n.length-1)===t)return!0;if(e.constructor)if(e.constructor.className===t||e.constructor.name===t)return!0;return!1},Wt.callExtractor=function(e,t){if(!Xt.hasOwnProperty(e))throw new Error("Extractor does not exist: "+e);return Xt[e](t,Wt.Pass)},Wt.Pass=function(e){if(!(this instanceof Wt.Pass))return new Wt.Pass(e);this.val=e},Vt.prototype.match=function(e,t){var n=this.patternFn(e,Wt);if(n)return this.successFn.apply(t,n);if(this.next)return this.next.match(e,t);throw new TypeError("All patterns exhausted")},Vt.prototype.clone=function(){var e=new Vt(this.patternFn,this.successFn);return this.next&&(e.next=this.next.clone()),e},Vt.prototype.last=function(){var e=this;while(e.next)e=e.next;return e},Vt.prototype.pop=function(){var e=this,t;while(e.next)t=e,e=e.next;return t&&(t.next=null),e};var $t=Array.prototype.slice,Jt={},Kt={};e.matches=t.exports={pattern:Gt,caseOf:Zt,extract:en,extractOne:tn,extractors:Xt,parse:n,compile:Ct,compilers:kt,runtime:Wt,Matcher:Vt}})(typeof window!="undefined"?window:{},typeof module!="undefined"?module:{});