vue-router-query-sync 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -71,12 +71,15 @@ createApp(App)
71
71
 
72
72
  ## 🧩 Basic Usage
73
73
 
74
- - You can use `useQuerySync` several times in one component.
75
- - Sync a value with a query param named `tab`:
74
+ You can use `useQuerySync` several times in one component.
75
+
76
+ #### Sync a value with a query param named `tab`:
77
+
78
+ - Visiting `?tab=favorite` sets `tab.value = 'favorite'`.
79
+ - Changing `tab.value = 'all'` updates the URL to `?tab=all`.
76
80
 
77
81
  ```ts
78
82
  import { useQuerySync } from 'vue-router-query-sync'
79
- import { ref } from 'vue'
80
83
 
81
84
  const tab = ref<'favorite' | 'all'>('all')
82
85
 
@@ -87,10 +90,36 @@ useQuerySync(
87
90
  )
88
91
  ```
89
92
 
90
- Now:
91
- - Visiting `?tab=favorite` sets `tab.value = 'favorite'`.
92
- - Changing `tab.value = 'all'` updates the URL to `?tab=all`.
93
+ ### Multiple values
94
+ #### Sync a value with a query param named `brands`:
93
95
 
96
+ - Visiting `?brands=lg%samsung` sets `brands.value = ['lg', 'samsung']`.
97
+ - Changing `brands.value = ['lg']` updates the URL to `?brands=lg`.
98
+
99
+ ```ts
100
+ import { useQuerySync } from 'vue-router-query-sync'
101
+
102
+ const brands = [
103
+ { value: 'lg', label: 'LG' },
104
+ { value: 'samsung', label: 'Samsung' }
105
+ ]
106
+
107
+ const selectedBrands = ref<string[]>([])
108
+
109
+ useQuerySync(
110
+ 'brands',
111
+ () => {
112
+ return selectedBrands.value.length > 0 ? selectedBrands.value.join('%') : null
113
+ },
114
+ (val) => {
115
+ if (typeof val === 'string' && val) {
116
+ selectedBrands.value = val.includes('%') ? val.split('%') : [val]
117
+ } else {
118
+ selectedBrands.value = []
119
+ }
120
+ }
121
+ )
122
+ ```
94
123
  ---
95
124
 
96
125
  ## 🔧 API
@@ -1 +1 @@
1
- {"version":3,"file":"useQuerySync.d.ts","sourceRoot":"","sources":["../../src/composables/useQuerySync.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAI1C;;;;;;;;;;;;;GAaG;AAEH,wBAAgB,YAAY,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EACpD,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,CAAC,GAAG,IAAI,EACnB,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,EACrB,OAAO,GAAE,gBAAqB,QAyD/B"}
1
+ {"version":3,"file":"useQuerySync.d.ts","sourceRoot":"","sources":["../../src/composables/useQuerySync.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAgB1C;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EACpD,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,CAAC,GAAG,IAAI,EACnB,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,EACrB,OAAO,GAAE,gBAAqB,QAiE/B"}
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- (function(e,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],n):(e=typeof globalThis<"u"?globalThis:e||self,n(e.VueRouterQuerySync={},e.Vue))})(this,(function(e,n){"use strict";let p=null;function m(t){p=t}function R(){if(!p)throw new Error("[vue-router-query-sync] Router instance not set. Call setRouter(router) before using composables.");return p}let c={};const f=new Set;let h=!1;function i(t,o){const a=R();o===null?t in c||f.add(t):(c[t]=o,f.delete(t)),h||(h=!0,queueMicrotask(()=>{const d={...a.currentRoute.value.query};f.forEach(l=>{delete d[l]});const v={...d,...c};a.replace({query:v}),c={},f.clear(),h=!1}))}function g(t,o,a,d={}){const l=R().currentRoute,{deps:S=[],context:b}=d,r=b?`${b}_${t}`:t,q=(u,y=!1)=>{if(typeof u=="string"){const s=isNaN(Number(u))?u:Number(u);if(y||s!==o()){a(s);const Q=o();Q!==s&&Q!==null&&i(r,Q)}}else{const s=o();s!==null&&i(r,s)}};n.onBeforeMount(()=>{S.length>0?n.watch(S,()=>q(l.value.query[r])):q(l.value.query[r])}),n.watch(o,u=>{const y=l.value.query[r];u===null?y!==void 0&&i(r,null):y!==String(u)&&i(r,u)}),n.watch(()=>l.value.query[r],u=>{q(u)}),n.onBeforeUnmount(()=>{i(r,null)})}const w={install(t,o){m(o.router)}};e.default=w,e.getRouter=R,e.setRouter=m,e.useQuerySync=g,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
1
+ (function(u,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],n):(u=typeof globalThis<"u"?globalThis:u||self,n(u.VueRouterQuerySync={},u.Vue))})(this,(function(u,n){"use strict";let p=null;function m(e){p=e}function Q(){if(!p)throw new Error("[vue-router-query-sync] Router instance not set. Call setRouter(router) before using composables.");return p}let c={};const f=new Set;let R=!1;function l(e,t){const a=Q();t===null?e in c||f.add(e):(c[e]=t,f.delete(e)),R||(R=!0,queueMicrotask(()=>{const d={...a.currentRoute.value.query};f.forEach(i=>{delete d[i]});const q={...d,...c};a.replace({query:q}),c={},f.clear(),R=!1}))}function g(e){if(e==="")return e;const t=Number(e);return isNaN(t)?e:t}function T(e,t,a,d={}){const i=Q().currentRoute,{deps:v=[],context:b}=d,r=b?`${b}_${e}`:e,S=(o,y=!1)=>{if(typeof o=="string"){const s=g(o);if(y||s!==t()){a(s);const h=t();h!==s&&h!==null&&l(r,h)}}else if(o===void 0){const s=t();s!==null&&l(r,s)}},M=o=>{const y=i.value.query[r];if(o===null)y!==void 0&&l(r,null);else{const s=String(o);y!==s&&l(r,o)}};n.onBeforeMount(()=>{v.length>0?n.watch(v,()=>S(i.value.query[r])):S(i.value.query[r])}),n.watch(t,M),n.watch(()=>i.value.query[r],o=>S(o)),n.onBeforeUnmount(()=>{l(r,null)})}const w={install(e,t){m(t.router)}};u.default=w,u.getRouter=Q,u.setRouter=m,u.useQuerySync=T,Object.defineProperties(u,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
package/dist/index.mjs CHANGED
@@ -1,68 +1,77 @@
1
- import { onBeforeMount as h, watch as y, onBeforeUnmount as N } from "vue";
1
+ import { onBeforeMount as b, watch as y, onBeforeUnmount as g } from "vue";
2
2
  let v = null;
3
3
  function x(e) {
4
4
  v = e;
5
5
  }
6
- function b() {
6
+ function S() {
7
7
  if (!v)
8
8
  throw new Error(
9
9
  "[vue-router-query-sync] Router instance not set. Call setRouter(router) before using composables."
10
10
  );
11
11
  return v;
12
12
  }
13
- let i = {};
13
+ let f = {};
14
14
  const a = /* @__PURE__ */ new Set();
15
- let q = !1;
16
- function l(e, u) {
17
- const s = b();
18
- u === null ? e in i || a.add(e) : (i[e] = u, a.delete(e)), q || (q = !0, queueMicrotask(() => {
15
+ let Q = !1;
16
+ function l(e, t) {
17
+ const s = S();
18
+ t === null ? e in f || a.add(e) : (f[e] = t, a.delete(e)), Q || (Q = !0, queueMicrotask(() => {
19
19
  const c = { ...s.currentRoute.value.query };
20
20
  a.forEach((o) => {
21
21
  delete c[o];
22
22
  });
23
23
  const R = {
24
24
  ...c,
25
- ...i
25
+ ...f
26
26
  };
27
- s.replace({ query: R }), i = {}, a.clear(), q = !1;
27
+ s.replace({ query: R }), f = {}, a.clear(), Q = !1;
28
28
  }));
29
29
  }
30
- function g(e, u, s, c = {}) {
31
- const o = b().currentRoute, { deps: m = [], context: Q } = c, r = Q ? `${Q}_${e}` : e, d = (t, f = !1) => {
32
- if (typeof t == "string") {
33
- const n = isNaN(Number(t)) ? t : Number(t);
34
- if (f || n !== u()) {
30
+ function N(e) {
31
+ if (e === "")
32
+ return e;
33
+ const t = Number(e);
34
+ return isNaN(t) ? e : t;
35
+ }
36
+ function B(e, t, s, c = {}) {
37
+ const o = S().currentRoute, { deps: m = [], context: q } = c, r = q ? `${q}_${e}` : e, d = (u, i = !1) => {
38
+ if (typeof u == "string") {
39
+ const n = N(u);
40
+ if (i || n !== t()) {
35
41
  s(n);
36
- const p = u();
42
+ const p = t();
37
43
  p !== n && p !== null && l(r, p);
38
44
  }
39
- } else {
40
- const n = u();
45
+ } else if (u === void 0) {
46
+ const n = t();
41
47
  n !== null && l(r, n);
42
48
  }
49
+ }, h = (u) => {
50
+ const i = o.value.query[r];
51
+ if (u === null)
52
+ i !== void 0 && l(r, null);
53
+ else {
54
+ const n = String(u);
55
+ i !== n && l(r, u);
56
+ }
43
57
  };
44
- h(() => {
58
+ b(() => {
45
59
  m.length > 0 ? y(m, () => d(o.value.query[r])) : d(o.value.query[r]);
46
- }), y(u, (t) => {
47
- const f = o.value.query[r];
48
- t === null ? f !== void 0 && l(r, null) : f !== String(t) && l(r, t);
49
- }), y(
60
+ }), y(t, h), y(
50
61
  () => o.value.query[r],
51
- (t) => {
52
- d(t);
53
- }
54
- ), N(() => {
62
+ (u) => d(u)
63
+ ), g(() => {
55
64
  l(r, null);
56
65
  });
57
66
  }
58
- const w = {
59
- install(e, u) {
60
- x(u.router);
67
+ const E = {
68
+ install(e, t) {
69
+ x(t.router);
61
70
  }
62
71
  };
63
72
  export {
64
- w as default,
65
- b as getRouter,
73
+ E as default,
74
+ S as getRouter,
66
75
  x as setRouter,
67
- g as useQuerySync
76
+ B as useQuerySync
68
77
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-router-query-sync",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Vue composable for syncing router query params with store",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",