-
Notifications
You must be signed in to change notification settings - Fork 1
/
object-pattern.min.js
1 lines (1 loc) · 6.35 KB
/
object-pattern.min.js
1
"use strict";!function(t,n){if("function"==typeof define)define(n);else if("undefined"!=typeof module&&module.exports)module.exports=n();else{var e=n(),r=window,i=r[t];e.noConflict=function(){return r[t]=i,e},r[t]=e}}("OP",function(){var t=function(){},n=function(n){return n instanceof o?"("+n.toString()+")":n instanceof t?n.toString():n instanceof h?n.toString():isNaN(n)?"'"+n+"'":n.toString()},e=function(t){return t instanceof o||t instanceof c?t.toJSON():t instanceof s?"*":t instanceof u?"<"+t.type+">":t},r=function(t){this.value=t};r.prototype=new t,r.prototype.match=function(n){var e;if(this.value instanceof t){for(e in n)if(this.value.match(n[e]))return!0}else for(e in n)if(n[e]===this.value)return!0;return!1},r.prototype.toString=function(){return"*:"+n(this.value)};var i=function(t,n){this.name=t,this.value=n};i.prototype=new t,i.prototype.match=function(n){return this.value instanceof t?n[this.name]&&this.value.match(n[this.name]):n[this.name]&&n[this.name]===this.value},i.prototype.toString=function(){return this.name+":"+n(this.value)};var a=function(t){this.matchable=t};a.prototype=new t,a.prototype.match=function(t){return!this.matchable.match(t)},a.prototype.toString=function(){return"!"+n(this.matchable)};var o=function(){this.properties=[];for(var t=0,n=arguments.length;n>t;t++)this.properties.push(arguments[t])};o.prototype=new t,o.prototype.match=function(t){for(var n=0,e=this.properties.length;e>n;n++)if(!this.properties[n].match(t))return!1;return!0},o.prototype.toString=function(){return this.properties.map(function(t){return t.toString()}).join(",")},o.prototype.toJSON=function(){var t={};return this.properties.forEach(function(n){if(n instanceof i)return t[n.name]=e(n.value);if(n instanceof r)return t["*"]=e(n.value);if(n instanceof a){if(n.matchable instanceof i)return t["!"+n.matchable.name]=e(n.matchable.value);if(n.matchable instanceof r)return t["!*"]=e(n.matchable.value)}}),t};var s=function(){};s.prototype=new t,s.prototype.match=function(t){return void 0!==t},s.prototype.toString=function(){return"*"};var u=function(t){this.type=t};u.prototype=new t,u.prototype.match=function(t){switch(this.type){case"array":return"["===JSON.stringify(t).substring(0,1);case"boolean":return t===!0||t===!1;case"number":return JSON.stringify(t)===""+t;case"object":return"{"===(JSON.stringify(t)||"").substring(0,1);case"string":return JSON.stringify(t)==='"'+t+'"';default:return t instanceof this.type}},u.prototype.toString=function(){return"<"+this.type+">"};var c=function(){this.matchables=[];for(var t=0;t<arguments.length;t++)this.matchables.push(arguments[t])};c.prototype=new t,c.prototype.match=function(n){if(!(n instanceof Array))return!1;if(0===this.matchables.length&&n.length>0)return!1;if(0===this.matchables.length&&0===n.length)return!0;for(var e=n,r={},i=0;i<this.matchables.length;i++)if(this.matchables[i]instanceof h){if(r=this.matchables[i].match(e),r.matched===!1)return!1;e=r.unmatched}else{if(0===e.length)return!1;if(this.matchables[i]instanceof t){if(!this.matchables[i].match(e[0]))return!1}else if(e[0]!==this.matchables[i])return!1;r.matched=!0,e=e.slice(1)}return r.matched&&0===e.length},c.prototype.toString=function(){return"/"+this.matchables.map(function(t){return n(t)}).join("/")},c.prototype.toJSON=function(){var t=[];return this.matchables.forEach(function(n){n instanceof f?(t.push(e("**")),t.push(e(n.termination))):t.push(e(n))}),t};var h=function(){},f=function(t){this.termination=t};f.prototype=new h,f.prototype.match=function(n){if(!this.termination)return{matched:!0,unmatched:[]};for(var e=0;e<n.length;e++)if(this.termination instanceof t){if(this.termination.match(n[e]))return{matched:!0,unmatched:n.slice(e+1)}}else if(this.termination===n[e])return{matched:!0,unmatched:n.slice(e+1)};return{matched:!1,unmatched:[]}},f.prototype.toString=function(){return"**/"+n(this.termination)};var p=function(t){return t instanceof Object?l(t):""===t?void 0:"/"===t.substring(0,1)?p.array(t):"*"===t?new s:"["===t.substring(0,1)&&"]"===t.substring(t.length-1,t.length)?p.array(t.substring(1,t.length-1)):"<"===t.substring(0,1)&&">"===t.substring(t.length-1,t.length)?new u(t.substring(1,t.length-1)):"("===t.substring(0,1)&&")"===t.substring(t.length-1,t.length)?p.object(t.substring(1,t.length-1)):'"'===t.substring(0,1)&&'"'===t.substring(t.length-1,t.length)||"'"===t.substring(0,1)&&"'"===t.substring(t.length-1,t.length)?t.substring(1,t.length-1).split("\\\\").map(function(t){return t.replace("\\","")}).join("\\"):"true"===t?!0:"false"===t?!1:isNaN(t)?t.indexOf(":")>-1?p.object(t):t:parseFloat(t)};p.array=function(t){var n=new c,e=p.nestedChecker(["/"]),r="",i=[];return t.split("").forEach(function(t,n){e(t)&&"**"!==r?(i.push(r),r=""):r+=t}),i.push(r),n.matchables=i.filter(function(t){return""!==t}).map(function(t,n,e){return"**/"===t.substring(0,3)?new f(p(t.substring(3))):"**"===t?new f:p(t)}),n},p.nestedChecker=function(t){var n=[],e=[["(",")"],["[","]"]],r=["'",'"'];return t=t||[],function(i){return e.forEach(function(t){i===t[0]&&n.push(t[0]),i===t[1]&&n[n.length-1]===t[0]&&n.pop()}),r.forEach(function(t){i===t&&(n[n.length-1]===t?n.pop():n.push(t))}),n.length>0?!1:t.filter(function(t){return i===t}).length>0}},p.object=function(t){var n="",e=p.nestedChecker([","]),r=new o,i=!1;return t.split("").forEach(function(t,a){e(t)&&!i?(r.properties.push(p.property(n)),n=""):n+=t,i=!1,"\\"===t&&(i=!0)}),r.properties.push(p.property(n)),r},p.property=function(t){var n=t.substring(0,t.indexOf(":")),e=p(t.substring(t.indexOf(":")+1));return"*"===n?new r(e):"!"===n.substring(0,1)?new a("*"===n.substring(1)?new r(e):new i(n.substring(1),e)):new i(n,e)};var l=function(t){var n;return t instanceof Array?l.value(t):(n=new o,n.properties=Object.keys(t).map(function(n){var e=l.value(t[n]);return"*"===n?new r(e):"!"===n.substring(0,1)?new a("*"===n.substring(1)?new r(e):new i(n.substring(1),e)):new i(n,e)}),n)};return l.value=function(t){return t instanceof Array?l.array(t):t instanceof Object?l(t):"*"===t?new s:t.substring&&"<"===t.substring(0,1)&&">"===t.substring(t.length-1)?new u(t.substring(1,t.length-1)):t},l.array=function(t){var n=new c;return n.matchables=t.map(function(t,n,e){return"**"===t?e[n+1]?new f(l.value(e[n+1])):new f:l.value(t)}).filter(function(t,n,e){return e[n-1]&&e[n-1]instanceof f?!1:!0}),n},{Matchable:t,WildcardProperty:r,ExactProperty:i,Negator:a,ObjectPattern:o,WildcardValue:s,TypedValue:u,ArrayPattern:c,ArrayMatchable:h,ArrayEllipsis:f,parse:p}});