From 63ec730556912f22fa6e9e922ee73e73c9674a0c Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Mon, 24 May 2021 18:32:00 -0400 Subject: [PATCH] Move Vue plugin into separate file (#424) * Export `ZiggyVue` from its own file * Add test builds * Update builds and scripts --- dist/vue.es.js | 1 + dist/vue.js | 1 + dist/vue.m.js | 1 + package.json | 4 +++- src/js/index.js | 8 -------- src/js/vue.js | 9 +++++++++ 6 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 dist/vue.es.js create mode 100644 dist/vue.js create mode 100644 dist/vue.m.js create mode 100644 src/js/vue.js diff --git a/dist/vue.es.js b/dist/vue.es.js new file mode 100644 index 00000000..fdae94dc --- /dev/null +++ b/dist/vue.es.js @@ -0,0 +1 @@ +function t(){return(t=Object.assign||function(t){for(var r=1;r1;){var r=t.pop(),n=r.obj[r.prop];if(e(n)){for(var i=[],o=0;o=48&&f<=57||f>=65&&f<=90||f>=97&&f<=122?o+=i.charAt(u):f<128?o+=n[f]:f<2048?o+=n[192|f>>6]+n[128|63&f]:f<55296||f>=57344?o+=n[224|f>>12]+n[128|f>>6&63]+n[128|63&f]:(f=65536+((1023&f)<<10|1023&i.charCodeAt(u+=1)),o+=n[240|f>>18]+n[128|f>>12&63]+n[128|f>>6&63]+n[128|63&f])}return o},isBuffer:function(t){return!(!t||"object"!=typeof t||!(t.constructor&&t.constructor.isBuffer&&t.constructor.isBuffer(t)))},isRegExp:function(t){return"[object RegExp]"===Object.prototype.toString.call(t)},maybeMap:function(t,r){if(e(t)){for(var n=[],i=0;i-1?t.split(","):t},E=function(t,r,e,n){if(t){var i=e.allowDots?t.replace(/\.([^.[]+)/g,"[$1]"):t,o=/(\[[^[\]]*])/g,u=e.depth>0&&/(\[[^[\]]*])/.exec(i),f=u?i.slice(0,u.index):i,s=[];if(f){if(!e.plainObjects&&m.call(Object.prototype,f)&&!e.allowPrototypes)return;s.push(f)}for(var a=0;e.depth>0&&null!==(u=o.exec(i))&&a=0;--o){var u,f=t[o];if("[]"===f&&e.parseArrays)u=[].concat(i);else{u=e.plainObjects?Object.create(null):{};var s="["===f.charAt(0)&&"]"===f.charAt(f.length-1)?f.slice(1,-1):f,a=parseInt(s,10);e.parseArrays||""!==s?!isNaN(a)&&f!==s&&String(a)===s&&a>=0&&e.parseArrays&&a<=e.arrayLimit?(u=[])[a]=i:u[s]=i:u={0:i}}i=u}return i}(s,r,e,n)}};class S{constructor(t,r,e){var n;this.name=t,this.definition=r,this.bindings=null!=(n=r.bindings)?n:{},this.config=e}get template(){return`${this.config.absolute?this.definition.domain?`${this.config.url.match(/^\w+:\/\//)[0]}${this.definition.domain}${this.config.port?":"+this.config.port:""}`:this.config.url:""}/${this.definition.uri}`.replace(/\/+$/,"")}get parameterSegments(){var t,r;return null!=(t=null===(r=this.template.match(/{[^}?]+\??}/g))||void 0===r?void 0:r.map(t=>({name:t.replace(/{|\??}/g,""),required:!/\?}$/.test(t)})))?t:[]}matchesUrl(t){if(!this.definition.methods.includes("GET"))return!1;const r=this.template.replace(/\/{[^}?]*\?}/g,"(/[^/?]+)?").replace(/{[^}]+}/g,"[^/?]+").replace(/^\w+:\/\//,"");return new RegExp(`^${r}$`).test(t.replace(/\/+$/,"").split("?").shift())}compile(t){return this.parameterSegments.length?this.template.replace(/{([^}?]+)\??}/g,(r,e)=>{var n;if([null,void 0].includes(t[e])&&this.parameterSegments.find(({name:t})=>t===e).required)throw new Error(`Ziggy error: '${e}' parameter is required for route '${this.name}'.`);return encodeURIComponent(null!=(n=t[e])?n:"")}).replace(/\/+$/,""):this.template}}class R extends String{constructor(r,e,n=!0,i){var o;if(super(),this.t=null!=(o=null!=i?i:Ziggy)?o:null===globalThis||void 0===globalThis?void 0:globalThis.Ziggy,this.t=t({},this.t,{absolute:n}),r){if(!this.t.routes[r])throw new Error(`Ziggy error: route '${r}' is not in the route list.`);this.i=new S(r,this.t.routes[r],this.t),this.u=this.s(e)}}toString(){const r=Object.keys(this.u).filter(t=>!this.i.parameterSegments.some(({name:r})=>r===t)).filter(t=>"_query"!==t).reduce((r,e)=>t({},r,{[e]:this.u[e]}),{});return this.i.compile(this.u)+function(t,r){var e,n=t,i=function(t){if(!t)return b;if(null!=t.encoder&&"function"!=typeof t.encoder)throw new TypeError("Encoder has to be a function.");var r=t.charset||b.charset;if(void 0!==t.charset&&"utf-8"!==t.charset&&"iso-8859-1"!==t.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");var e=a.default;if(void 0!==t.format){if(!c.call(a.formatters,t.format))throw new TypeError("Unknown format option provided.");e=t.format}var n=a.formatters[e],i=b.filter;return("function"==typeof t.filter||h(t.filter))&&(i=t.filter),{addQueryPrefix:"boolean"==typeof t.addQueryPrefix?t.addQueryPrefix:b.addQueryPrefix,allowDots:void 0===t.allowDots?b.allowDots:!!t.allowDots,charset:r,charsetSentinel:"boolean"==typeof t.charsetSentinel?t.charsetSentinel:b.charsetSentinel,delimiter:void 0===t.delimiter?b.delimiter:t.delimiter,encode:"boolean"==typeof t.encode?t.encode:b.encode,encoder:"function"==typeof t.encoder?t.encoder:b.encoder,encodeValuesOnly:"boolean"==typeof t.encodeValuesOnly?t.encodeValuesOnly:b.encodeValuesOnly,filter:i,formatter:n,serializeDate:"function"==typeof t.serializeDate?t.serializeDate:b.serializeDate,skipNulls:"boolean"==typeof t.skipNulls?t.skipNulls:b.skipNulls,sort:"function"==typeof t.sort?t.sort:null,strictNullHandling:"boolean"==typeof t.strictNullHandling?t.strictNullHandling:b.strictNullHandling}}(r);"function"==typeof i.filter?n=(0,i.filter)("",n):h(i.filter)&&(e=i.filter);var o=[];if("object"!=typeof n||null===n)return"";var u=l[r&&r.arrayFormat in l?r.arrayFormat:r&&"indices"in r?r.indices?"indices":"repeat":"indices"];e||(e=Object.keys(n)),i.sort&&e.sort(i.sort);for(var f=0;f0?y+d:""}(t({},r,this.u._query),{addQueryPrefix:!0,arrayFormat:"indices",encodeValuesOnly:!0,skipNulls:!0,encoder:(t,r)=>"boolean"==typeof t?Number(t):r(t)})}current(t,r){const e=this.t.absolute?window.location.host+window.location.pathname:window.location.pathname.replace(this.t.url.replace(/^\w*:\/\/[^/]+/,""),"").replace(/^\/+/,"/"),[n,i]=Object.entries(this.t.routes).find(([r,n])=>new S(t,n,this.t).matchesUrl(e))||[void 0,void 0];if(!t)return n;const o=new RegExp(`^${t.replace(".","\\.").replace("*",".*")}$`).test(n);if([null,void 0].includes(r)||!o)return o;const u=new S(n,i,this.t);r=this.s(r,u);const f=this.l(i);return!(!Object.values(r).every(t=>!t)||Object.values(f).length)||Object.entries(r).every(([t,r])=>f[t]==r)}get params(){return this.l(this.t.routes[this.current()])}has(t){return Object.keys(this.t.routes).includes(t)}s(r={},e=this.i){r=["string","number"].includes(typeof r)?[r]:r;const n=e.parameterSegments.filter(({name:t})=>!this.t.defaults[t]);return Array.isArray(r)?r=r.reduce((r,e,i)=>t({},r,n[i]?{[n[i].name]:e}:{[e]:""}),{}):1!==n.length||r[n[0].name]||!r.hasOwnProperty(Object.values(e.bindings)[0])&&!r.hasOwnProperty("id")||(r={[n[0].name]:r}),t({},this.h(e),this.p(r,e.bindings))}h(r){return r.parameterSegments.filter(({name:t})=>this.t.defaults[t]).reduce((r,{name:e},n)=>t({},r,{[e]:this.t.defaults[e]}),{})}p(r,e={}){return Object.entries(r).reduce((r,[n,i])=>{if(!i||"object"!=typeof i||Array.isArray(i)||"_query"===n)return t({},r,{[n]:i});if(!i.hasOwnProperty(e[n])){if(!i.hasOwnProperty("id"))throw new Error(`Ziggy error: object passed as '${n}' parameter is missing route model binding key '${e[n]}'.`);e[n]="id"}return t({},r,{[n]:i[e[n]]})},{})}l(r){var e;let n=window.location.pathname.replace(this.t.url.replace(/^\w*:\/\/[^/]+/,""),"").replace(/^\/+/,"");const i=(r,e="",n)=>{const[i,o]=[r,e].map(t=>t.split(n));return o.reduce((r,e,n)=>/^{[^}?]+\??}$/.test(e)&&i[n]?t({},r,{[e.replace(/^{|\??}$/g,"")]:i[n]}):r,{})};return t({},i(window.location.host,r.domain,"."),i(n,r.uri,"/"),function(t,r){var e=w;if(""===t||null==t)return e.plainObjects?Object.create(null):{};for(var n="string"==typeof t?function(t,r){var e,n={},i=(r.ignoreQueryPrefix?t.replace(/^\?/,""):t).split(r.delimiter,Infinity===r.parameterLimit?void 0:r.parameterLimit),u=-1,f=r.charset;if(r.charsetSentinel)for(e=0;e-1&&(a=j(a)?[a]:a),n[s]=m.call(n,s)?o.combine(n[s],a):a}return n}(t,e):t,i=e.plainObjects?Object.create(null):{},u=Object.keys(n),f=0;ft.mixin({methods:{route:(t,e,n,i=r)=>function(t,r,e,n){const i=new R(t,r,e,n);return t?i.toString():i}(t,e,n,i)}})};export{k as ZiggyVue}; diff --git a/dist/vue.js b/dist/vue.js new file mode 100644 index 00000000..5614d45e --- /dev/null +++ b/dist/vue.js @@ -0,0 +1 @@ +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).ZiggyVue={})}(this,function(t){var e=Object.prototype.hasOwnProperty,r=Array.isArray,n=function(){for(var t=[],e=0;e<256;++e)t.push("%"+((e<16?"0":"")+e.toString(16)).toUpperCase());return t}(),o=function(t,e){for(var r=e&&e.plainObjects?Object.create(null):{},n=0;n1;){var e=t.pop(),n=e.obj[e.prop];if(r(n)){for(var o=[],i=0;i=48&&f<=57||f>=65&&f<=90||f>=97&&f<=122?i+=o.charAt(u):f<128?i+=n[f]:f<2048?i+=n[192|f>>6]+n[128|63&f]:f<55296||f>=57344?i+=n[224|f>>12]+n[128|f>>6&63]+n[128|63&f]:(f=65536+((1023&f)<<10|1023&o.charCodeAt(u+=1)),i+=n[240|f>>18]+n[128|f>>12&63]+n[128|f>>6&63]+n[128|63&f])}return i},isBuffer:function(t){return!(!t||"object"!=typeof t||!(t.constructor&&t.constructor.isBuffer&&t.constructor.isBuffer(t)))},isRegExp:function(t){return"[object RegExp]"===Object.prototype.toString.call(t)},maybeMap:function(t,e){if(r(t)){for(var n=[],o=0;o-1?t.split(","):t},S=function(t,e,r,n){if(t){var o=r.allowDots?t.replace(/\.([^.[]+)/g,"[$1]"):t,i=/(\[[^[\]]*])/g,u=r.depth>0&&/(\[[^[\]]*])/.exec(o),f=u?o.slice(0,u.index):o,s=[];if(f){if(!r.plainObjects&&g.call(Object.prototype,f)&&!r.allowPrototypes)return;s.push(f)}for(var a=0;r.depth>0&&null!==(u=i.exec(o))&&a=0;--i){var u,f=t[i];if("[]"===f&&r.parseArrays)u=[].concat(o);else{u=r.plainObjects?Object.create(null):{};var s="["===f.charAt(0)&&"]"===f.charAt(f.length-1)?f.slice(1,-1):f,a=parseInt(s,10);r.parseArrays||""!==s?!isNaN(a)&&f!==s&&String(a)===s&&a>=0&&r.parseArrays&&a<=r.arrayLimit?(u=[])[a]=o:u[s]=o:u={0:o}}o=u}return o}(s,e,r,n)}},x=function(t,e){var r=function(t){if(!t)return j;if(null!=t.decoder&&"function"!=typeof t.decoder)throw new TypeError("Decoder has to be a function.");if(void 0!==t.charset&&"utf-8"!==t.charset&&"iso-8859-1"!==t.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");return{allowDots:void 0===t.allowDots?j.allowDots:!!t.allowDots,allowPrototypes:"boolean"==typeof t.allowPrototypes?t.allowPrototypes:j.allowPrototypes,arrayLimit:"number"==typeof t.arrayLimit?t.arrayLimit:j.arrayLimit,charset:void 0===t.charset?j.charset:t.charset,charsetSentinel:"boolean"==typeof t.charsetSentinel?t.charsetSentinel:j.charsetSentinel,comma:"boolean"==typeof t.comma?t.comma:j.comma,decoder:"function"==typeof t.decoder?t.decoder:j.decoder,delimiter:"string"==typeof t.delimiter||i.isRegExp(t.delimiter)?t.delimiter:j.delimiter,depth:"number"==typeof t.depth||!1===t.depth?+t.depth:j.depth,ignoreQueryPrefix:!0===t.ignoreQueryPrefix,interpretNumericEntities:"boolean"==typeof t.interpretNumericEntities?t.interpretNumericEntities:j.interpretNumericEntities,parameterLimit:"number"==typeof t.parameterLimit?t.parameterLimit:j.parameterLimit,parseArrays:!1!==t.parseArrays,plainObjects:"boolean"==typeof t.plainObjects?t.plainObjects:j.plainObjects,strictNullHandling:"boolean"==typeof t.strictNullHandling?t.strictNullHandling:j.strictNullHandling}}(e);if(""===t||null==t)return r.plainObjects?Object.create(null):{};for(var n="string"==typeof t?function(t,e){var r,n={},o=(e.ignoreQueryPrefix?t.replace(/^\?/,""):t).split(e.delimiter,Infinity===e.parameterLimit?void 0:e.parameterLimit),u=-1,f=e.charset;if(e.charsetSentinel)for(r=0;r-1&&(a=w(a)?[a]:a),n[s]=g.call(n,s)?i.combine(n[s],a):a}return n}(t,r):t,o=r.plainObjects?Object.create(null):{},u=Object.keys(n),f=0;f({name:t.replace(/{|\??}/g,""),required:!/\?}$/.test(t)})))?t:[]}matchesUrl(t){if(!this.definition.methods.includes("GET"))return!1;const e=this.template.replace(/\/{[^}?]*\?}/g,"(/[^/?]+)?").replace(/{[^}]+}/g,"[^/?]+").replace(/^\w+:\/\//,"");return new RegExp("^"+e+"$").test(t.replace(/\/+$/,"").split("?").shift())}compile(t){return this.parameterSegments.length?this.template.replace(/{([^}?]+)\??}/g,(e,r)=>{var n;if([null,void 0].includes(t[r])&&this.parameterSegments.find(({name:t})=>t===r).required)throw new Error("Ziggy error: '"+r+"' parameter is required for route '"+this.name+"'.");return encodeURIComponent(null!=(n=t[r])?n:"")}).replace(/\/+$/,""):this.template}}class R extends String{constructor(t,e,r=!0,n){var o;if(super(),this.t=null!=(o=null!=n?n:Ziggy)?o:null===globalThis||void 0===globalThis?void 0:globalThis.Ziggy,this.t={...this.t,absolute:r},t){if(!this.t.routes[t])throw new Error("Ziggy error: route '"+t+"' is not in the route list.");this.i=new N(t,this.t.routes[t],this.t),this.u=this.s(e)}}toString(){const t=Object.keys(this.u).filter(t=>!this.i.parameterSegments.some(({name:e})=>e===t)).filter(t=>"_query"!==t).reduce((t,e)=>({...t,[e]:this.u[e]}),{});return this.i.compile(this.u)+function(t,e){var r,n=t,o=function(t){if(!t)return v;if(null!=t.encoder&&"function"!=typeof t.encoder)throw new TypeError("Encoder has to be a function.");var e=t.charset||v.charset;if(void 0!==t.charset&&"utf-8"!==t.charset&&"iso-8859-1"!==t.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");var r=a.default;if(void 0!==t.format){if(!c.call(a.formatters,t.format))throw new TypeError("Unknown format option provided.");r=t.format}var n=a.formatters[r],o=v.filter;return("function"==typeof t.filter||p(t.filter))&&(o=t.filter),{addQueryPrefix:"boolean"==typeof t.addQueryPrefix?t.addQueryPrefix:v.addQueryPrefix,allowDots:void 0===t.allowDots?v.allowDots:!!t.allowDots,charset:e,charsetSentinel:"boolean"==typeof t.charsetSentinel?t.charsetSentinel:v.charsetSentinel,delimiter:void 0===t.delimiter?v.delimiter:t.delimiter,encode:"boolean"==typeof t.encode?t.encode:v.encode,encoder:"function"==typeof t.encoder?t.encoder:v.encoder,encodeValuesOnly:"boolean"==typeof t.encodeValuesOnly?t.encodeValuesOnly:v.encodeValuesOnly,filter:o,formatter:n,serializeDate:"function"==typeof t.serializeDate?t.serializeDate:v.serializeDate,skipNulls:"boolean"==typeof t.skipNulls?t.skipNulls:v.skipNulls,sort:"function"==typeof t.sort?t.sort:null,strictNullHandling:"boolean"==typeof t.strictNullHandling?t.strictNullHandling:v.strictNullHandling}}(e);"function"==typeof o.filter?n=(0,o.filter)("",n):p(o.filter)&&(r=o.filter);var i=[];if("object"!=typeof n||null===n)return"";var u=l[e&&e.arrayFormat in l?e.arrayFormat:e&&"indices"in e?e.indices?"indices":"repeat":"indices"];r||(r=Object.keys(n)),o.sort&&r.sort(o.sort);for(var f=0;f0?h+d:""}({...t,...this.u._query},{addQueryPrefix:!0,arrayFormat:"indices",encodeValuesOnly:!0,skipNulls:!0,encoder:(t,e)=>"boolean"==typeof t?Number(t):e(t)})}current(t,e){const r=this.t.absolute?window.location.host+window.location.pathname:window.location.pathname.replace(this.t.url.replace(/^\w*:\/\/[^/]+/,""),"").replace(/^\/+/,"/"),[n,o]=Object.entries(this.t.routes).find(([e,n])=>new N(t,n,this.t).matchesUrl(r))||[void 0,void 0];if(!t)return n;const i=new RegExp("^"+t.replace(".","\\.").replace("*",".*")+"$").test(n);if([null,void 0].includes(e)||!i)return i;const u=new N(n,o,this.t);e=this.s(e,u);const f=this.l(o);return!(!Object.values(e).every(t=>!t)||Object.values(f).length)||Object.entries(e).every(([t,e])=>f[t]==e)}get params(){return this.l(this.t.routes[this.current()])}has(t){return Object.keys(this.t.routes).includes(t)}s(t={},e=this.i){t=["string","number"].includes(typeof t)?[t]:t;const r=e.parameterSegments.filter(({name:t})=>!this.t.defaults[t]);return Array.isArray(t)?t=t.reduce((t,e,n)=>r[n]?{...t,[r[n].name]:e}:{...t,[e]:""},{}):1!==r.length||t[r[0].name]||!t.hasOwnProperty(Object.values(e.bindings)[0])&&!t.hasOwnProperty("id")||(t={[r[0].name]:t}),{...this.p(e),...this.h(t,e.bindings)}}p(t){return t.parameterSegments.filter(({name:t})=>this.t.defaults[t]).reduce((t,{name:e},r)=>({...t,[e]:this.t.defaults[e]}),{})}h(t,e={}){return Object.entries(t).reduce((t,[r,n])=>{if(!n||"object"!=typeof n||Array.isArray(n)||"_query"===r)return{...t,[r]:n};if(!n.hasOwnProperty(e[r])){if(!n.hasOwnProperty("id"))throw new Error("Ziggy error: object passed as '"+r+"' parameter is missing route model binding key '"+e[r]+"'.");e[r]="id"}return{...t,[r]:n[e[r]]}},{})}l(t){var e;let r=window.location.pathname.replace(this.t.url.replace(/^\w*:\/\/[^/]+/,""),"").replace(/^\/+/,"");const n=(t,e="",r)=>{const[n,o]=[t,e].map(t=>t.split(r));return o.reduce((t,e,r)=>/^{[^}?]+\??}$/.test(e)&&n[r]?{...t,[e.replace(/^{|\??}$/g,"")]:n[r]}:t,{})};return{...n(window.location.host,t.domain,"."),...n(r,t.uri,"/"),...x(null===(e=window.location.search)||void 0===e?void 0:e.replace(/^\?/,""))}}valueOf(){return this.toString()}check(t){return this.has(t)}}t.ZiggyVue={install:(t,e)=>t.mixin({methods:{route:(t,r,n,o=e)=>function(t,e,r,n){const o=new R(t,e,r,n);return t?o.toString():o}(t,r,n,o)}})}}); diff --git a/dist/vue.m.js b/dist/vue.m.js new file mode 100644 index 00000000..0a4a6242 --- /dev/null +++ b/dist/vue.m.js @@ -0,0 +1 @@ +var t=Object.prototype.hasOwnProperty,e=Array.isArray,r=function(){for(var t=[],e=0;e<256;++e)t.push("%"+((e<16?"0":"")+e.toString(16)).toUpperCase());return t}(),n=function(t,e){for(var r=e&&e.plainObjects?Object.create(null):{},n=0;n1;){var r=t.pop(),n=r.obj[r.prop];if(e(n)){for(var o=[],i=0;i=48&&f<=57||f>=65&&f<=90||f>=97&&f<=122?i+=o.charAt(u):f<128?i+=r[f]:f<2048?i+=r[192|f>>6]+r[128|63&f]:f<55296||f>=57344?i+=r[224|f>>12]+r[128|f>>6&63]+r[128|63&f]:(f=65536+((1023&f)<<10|1023&o.charCodeAt(u+=1)),i+=r[240|f>>18]+r[128|f>>12&63]+r[128|f>>6&63]+r[128|63&f])}return i},isBuffer:function(t){return!(!t||"object"!=typeof t||!(t.constructor&&t.constructor.isBuffer&&t.constructor.isBuffer(t)))},isRegExp:function(t){return"[object RegExp]"===Object.prototype.toString.call(t)},maybeMap:function(t,r){if(e(t)){for(var n=[],o=0;o-1?t.split(","):t},E=function(t,e,r,n){if(t){var o=r.allowDots?t.replace(/\.([^.[]+)/g,"[$1]"):t,i=/(\[[^[\]]*])/g,u=r.depth>0&&/(\[[^[\]]*])/.exec(o),f=u?o.slice(0,u.index):o,a=[];if(f){if(!r.plainObjects&&m.call(Object.prototype,f)&&!r.allowPrototypes)return;a.push(f)}for(var s=0;r.depth>0&&null!==(u=i.exec(o))&&s=0;--i){var u,f=t[i];if("[]"===f&&r.parseArrays)u=[].concat(o);else{u=r.plainObjects?Object.create(null):{};var a="["===f.charAt(0)&&"]"===f.charAt(f.length-1)?f.slice(1,-1):f,s=parseInt(a,10);r.parseArrays||""!==a?!isNaN(s)&&f!==a&&String(s)===a&&s>=0&&r.parseArrays&&s<=r.arrayLimit?(u=[])[s]=o:u[a]=o:u={0:o}}o=u}return o}(a,e,r,n)}},S=function(t,e){var r=function(t){if(!t)return w;if(null!=t.decoder&&"function"!=typeof t.decoder)throw new TypeError("Decoder has to be a function.");if(void 0!==t.charset&&"utf-8"!==t.charset&&"iso-8859-1"!==t.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");return{allowDots:void 0===t.allowDots?w.allowDots:!!t.allowDots,allowPrototypes:"boolean"==typeof t.allowPrototypes?t.allowPrototypes:w.allowPrototypes,arrayLimit:"number"==typeof t.arrayLimit?t.arrayLimit:w.arrayLimit,charset:void 0===t.charset?w.charset:t.charset,charsetSentinel:"boolean"==typeof t.charsetSentinel?t.charsetSentinel:w.charsetSentinel,comma:"boolean"==typeof t.comma?t.comma:w.comma,decoder:"function"==typeof t.decoder?t.decoder:w.decoder,delimiter:"string"==typeof t.delimiter||o.isRegExp(t.delimiter)?t.delimiter:w.delimiter,depth:"number"==typeof t.depth||!1===t.depth?+t.depth:w.depth,ignoreQueryPrefix:!0===t.ignoreQueryPrefix,interpretNumericEntities:"boolean"==typeof t.interpretNumericEntities?t.interpretNumericEntities:w.interpretNumericEntities,parameterLimit:"number"==typeof t.parameterLimit?t.parameterLimit:w.parameterLimit,parseArrays:!1!==t.parseArrays,plainObjects:"boolean"==typeof t.plainObjects?t.plainObjects:w.plainObjects,strictNullHandling:"boolean"==typeof t.strictNullHandling?t.strictNullHandling:w.strictNullHandling}}(e);if(""===t||null==t)return r.plainObjects?Object.create(null):{};for(var n="string"==typeof t?function(t,e){var r,n={},i=(e.ignoreQueryPrefix?t.replace(/^\?/,""):t).split(e.delimiter,Infinity===e.parameterLimit?void 0:e.parameterLimit),u=-1,f=e.charset;if(e.charsetSentinel)for(r=0;r-1&&(s=g(s)?[s]:s),n[a]=m.call(n,a)?o.combine(n[a],s):s}return n}(t,r):t,i=r.plainObjects?Object.create(null):{},u=Object.keys(n),f=0;f({name:t.replace(/{|\??}/g,""),required:!/\?}$/.test(t)})))?t:[]}matchesUrl(t){if(!this.definition.methods.includes("GET"))return!1;const e=this.template.replace(/\/{[^}?]*\?}/g,"(/[^/?]+)?").replace(/{[^}]+}/g,"[^/?]+").replace(/^\w+:\/\//,"");return new RegExp("^"+e+"$").test(t.replace(/\/+$/,"").split("?").shift())}compile(t){return this.parameterSegments.length?this.template.replace(/{([^}?]+)\??}/g,(e,r)=>{var n;if([null,void 0].includes(t[r])&&this.parameterSegments.find(({name:t})=>t===r).required)throw new Error("Ziggy error: '"+r+"' parameter is required for route '"+this.name+"'.");return encodeURIComponent(null!=(n=t[r])?n:"")}).replace(/\/+$/,""):this.template}}class R extends String{constructor(t,e,r=!0,n){var o;if(super(),this.t=null!=(o=null!=n?n:Ziggy)?o:null===globalThis||void 0===globalThis?void 0:globalThis.Ziggy,this.t={...this.t,absolute:r},t){if(!this.t.routes[t])throw new Error("Ziggy error: route '"+t+"' is not in the route list.");this.i=new N(t,this.t.routes[t],this.t),this.u=this.s(e)}}toString(){const t=Object.keys(this.u).filter(t=>!this.i.parameterSegments.some(({name:e})=>e===t)).filter(t=>"_query"!==t).reduce((t,e)=>({...t,[e]:this.u[e]}),{});return this.i.compile(this.u)+function(t,e){var r,n=t,o=function(t){if(!t)return b;if(null!=t.encoder&&"function"!=typeof t.encoder)throw new TypeError("Encoder has to be a function.");var e=t.charset||b.charset;if(void 0!==t.charset&&"utf-8"!==t.charset&&"iso-8859-1"!==t.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");var r=a.default;if(void 0!==t.format){if(!s.call(a.formatters,t.format))throw new TypeError("Unknown format option provided.");r=t.format}var n=a.formatters[r],o=b.filter;return("function"==typeof t.filter||l(t.filter))&&(o=t.filter),{addQueryPrefix:"boolean"==typeof t.addQueryPrefix?t.addQueryPrefix:b.addQueryPrefix,allowDots:void 0===t.allowDots?b.allowDots:!!t.allowDots,charset:e,charsetSentinel:"boolean"==typeof t.charsetSentinel?t.charsetSentinel:b.charsetSentinel,delimiter:void 0===t.delimiter?b.delimiter:t.delimiter,encode:"boolean"==typeof t.encode?t.encode:b.encode,encoder:"function"==typeof t.encoder?t.encoder:b.encoder,encodeValuesOnly:"boolean"==typeof t.encodeValuesOnly?t.encodeValuesOnly:b.encodeValuesOnly,filter:o,formatter:n,serializeDate:"function"==typeof t.serializeDate?t.serializeDate:b.serializeDate,skipNulls:"boolean"==typeof t.skipNulls?t.skipNulls:b.skipNulls,sort:"function"==typeof t.sort?t.sort:null,strictNullHandling:"boolean"==typeof t.strictNullHandling?t.strictNullHandling:b.strictNullHandling}}(e);"function"==typeof o.filter?n=(0,o.filter)("",n):l(o.filter)&&(r=o.filter);var i=[];if("object"!=typeof n||null===n)return"";var u=c[e&&e.arrayFormat in c?e.arrayFormat:e&&"indices"in e?e.indices?"indices":"repeat":"indices"];r||(r=Object.keys(n)),o.sort&&r.sort(o.sort);for(var f=0;f0?d+h:""}({...t,...this.u._query},{addQueryPrefix:!0,arrayFormat:"indices",encodeValuesOnly:!0,skipNulls:!0,encoder:(t,e)=>"boolean"==typeof t?Number(t):e(t)})}current(t,e){const r=this.t.absolute?window.location.host+window.location.pathname:window.location.pathname.replace(this.t.url.replace(/^\w*:\/\/[^/]+/,""),"").replace(/^\/+/,"/"),[n,o]=Object.entries(this.t.routes).find(([e,n])=>new N(t,n,this.t).matchesUrl(r))||[void 0,void 0];if(!t)return n;const i=new RegExp("^"+t.replace(".","\\.").replace("*",".*")+"$").test(n);if([null,void 0].includes(e)||!i)return i;const u=new N(n,o,this.t);e=this.s(e,u);const f=this.l(o);return!(!Object.values(e).every(t=>!t)||Object.values(f).length)||Object.entries(e).every(([t,e])=>f[t]==e)}get params(){return this.l(this.t.routes[this.current()])}has(t){return Object.keys(this.t.routes).includes(t)}s(t={},e=this.i){t=["string","number"].includes(typeof t)?[t]:t;const r=e.parameterSegments.filter(({name:t})=>!this.t.defaults[t]);return Array.isArray(t)?t=t.reduce((t,e,n)=>r[n]?{...t,[r[n].name]:e}:{...t,[e]:""},{}):1!==r.length||t[r[0].name]||!t.hasOwnProperty(Object.values(e.bindings)[0])&&!t.hasOwnProperty("id")||(t={[r[0].name]:t}),{...this.p(e),...this.h(t,e.bindings)}}p(t){return t.parameterSegments.filter(({name:t})=>this.t.defaults[t]).reduce((t,{name:e},r)=>({...t,[e]:this.t.defaults[e]}),{})}h(t,e={}){return Object.entries(t).reduce((t,[r,n])=>{if(!n||"object"!=typeof n||Array.isArray(n)||"_query"===r)return{...t,[r]:n};if(!n.hasOwnProperty(e[r])){if(!n.hasOwnProperty("id"))throw new Error("Ziggy error: object passed as '"+r+"' parameter is missing route model binding key '"+e[r]+"'.");e[r]="id"}return{...t,[r]:n[e[r]]}},{})}l(t){var e;let r=window.location.pathname.replace(this.t.url.replace(/^\w*:\/\/[^/]+/,""),"").replace(/^\/+/,"");const n=(t,e="",r)=>{const[n,o]=[t,e].map(t=>t.split(r));return o.reduce((t,e,r)=>/^{[^}?]+\??}$/.test(e)&&n[r]?{...t,[e.replace(/^{|\??}$/g,"")]:n[r]}:t,{})};return{...n(window.location.host,t.domain,"."),...n(r,t.uri,"/"),...S(null===(e=window.location.search)||void 0===e?void 0:e.replace(/^\?/,""))}}valueOf(){return this.toString()}check(t){return this.has(t)}}const T={install:(t,e)=>t.mixin({methods:{route:(t,r,n,o=e)=>function(t,e,r,n){const o=new R(t,e,r,n);return t?o.toString():o}(t,r,n,o)}})};export{T as ZiggyVue}; diff --git a/package.json b/package.json index 6b991e21..670f6e70 100644 --- a/package.json +++ b/package.json @@ -41,10 +41,12 @@ }, "scripts": { "build": "microbundle --name route --format modern,es,umd --external none --no-sourcemap", + "build:vue": "microbundle --entry src/js/vue.js --output dist/vue.js --name ZiggyVue --format modern,es,umd --external none --no-sourcemap", "watch": "npm run build watch", "build:npm": "microbundle --name route --format modern,es,umd --no-sourcemap", + "build:npm:vue": "microbundle --entry src/js/vue.js --output dist/vue.js --name ZiggyVue --format modern,es,umd --no-sourcemap", "test": "jest", - "prepublishOnly": "npm run build:npm" + "prepublishOnly": "npm run build:npm && npm run build:npm:vue" }, "mangle": { "regex": "^_(?!query)" diff --git a/src/js/index.js b/src/js/index.js index 324e4c61..2b9dd5d3 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -5,11 +5,3 @@ export default function route(name, params, absolute, config) { return name ? router.toString() : router; } - -export const ZiggyVue = { - install: (v, options) => v.mixin({ - methods: { - route: (name, params, absolute, config = options) => route(name, params, absolute, config), - }, - }), -}; diff --git a/src/js/vue.js b/src/js/vue.js new file mode 100644 index 00000000..6bc1a384 --- /dev/null +++ b/src/js/vue.js @@ -0,0 +1,9 @@ +import route from './index.js'; + +export const ZiggyVue = { + install: (v, options) => v.mixin({ + methods: { + route: (name, params, absolute, config = options) => route(name, params, absolute, config), + }, + }), +};