<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ahmed Khatab M on Medium]]></title>
        <description><![CDATA[Stories by Ahmed Khatab M on Medium]]></description>
        <link>https://medium.com/@ahmed.khatab.m?source=rss-d4e412b955c2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ihCCj3L3PAftWzxr</url>
            <title>Stories by Ahmed Khatab M on Medium</title>
            <link>https://medium.com/@ahmed.khatab.m?source=rss-d4e412b955c2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 15:25:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ahmed.khatab.m/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introducing vue-query-utils: A Utility Plugin for Handling Query Strings in Vue.js and Nuxt.js]]></title>
            <link>https://medium.com/@ahmed.khatab.m/introducing-vue-query-utils-a-utility-plugin-for-handling-query-strings-in-vue-js-and-nuxt-js-1dc01cc55dfd?source=rss-d4e412b955c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/1dc01cc55dfd</guid>
            <category><![CDATA[querystring]]></category>
            <category><![CDATA[http-request]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[nuxtjs]]></category>
            <dc:creator><![CDATA[Ahmed Khatab M]]></dc:creator>
            <pubDate>Tue, 04 Jun 2024 12:14:44 GMT</pubDate>
            <atom:updated>2024-06-04T12:14:44.268Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="vue-query-utils logo" src="https://cdn-images-1.medium.com/max/400/1*q2Vj6dVuTfMDl5Ts6RiFYA.png" /></figure><h3>Abstract</h3><p>Handling query strings is a common requirement in modern web applications, especially those involving dynamic content filtering, searching, and pagination. This article introduces vue-query-utils, a utility plugin designed to facilitate the conversion of JavaScript objects to URL query strings and vice versa. The plugin is specifically tailored for use in Vue.js and Nuxt.js applications. This paper describes the motivation, design, implementation, and practical applications of the vue-query-utils plugin.</p><h3>Introduction</h3><p>In web development, query strings are an essential mechanism for passing data between pages via the URL. They are widely used in search engines, filtering systems, and state management. Despite their utility, managing query strings manually can be error-prone and tedious. To address this issue, we developed vue-query-utils, a utility plugin that simplifies the conversion process between JavaScript objects and URL query strings.</p><h3>Motivation</h3><p>The primary motivation for developing vue-query-utils was to streamline the process of handling query strings in Vue.js and Nuxt.js applications. Existing solutions often lack flexibility or are overly complex. Our goal was to create a lightweight, easy-to-use utility that integrates seamlessly with these frameworks.</p><h3>Design and Implementation</h3><h3>Features</h3><ul><li>Conversion to Query String: Converts any JavaScript object to a URL query string.</li><li>Parsing Query String: Parses URL query strings back into JavaScript objects.</li><li>Support for Nested Structures: Handles nested objects and arrays efficiently.</li><li>Integration with Vue.js and Nuxt.js: Easy to set up and use within these frameworks.</li></ul><h3>Installation</h3><p>The plugin can be installed via npm or yarn:</p><pre>npm install vue-query-utils</pre><h3>Plugin Implementation</h3><p>The core functionality of the plugin is encapsulated in two main functions: convertToQueryString and parseQueryString. These functions handle the serialization and deserialization of query strings.</p><h4>convertToQueryString Function</h4><p>This function takes a JavaScript object and converts it into a URL query string.</p><pre>function convertToQueryString(params, prefix = &#39;&#39;) {<br>  const query = new URLSearchParams();<br><br>  const addQuery = (key, value) =&gt; {<br>    if (Array.isArray(value)) {<br>      value.forEach((v, i) =&gt; {<br>        addQuery(`${key}[${i}]`, v);<br>      });<br>    } else if (typeof value === &#39;object&#39; &amp;&amp; value !== null) {<br>      Object.keys(value).forEach(subKey =&gt; {<br>        addQuery(`${key}[${subKey}]`, value[subKey]);<br>      });<br>    } else {<br>      query.append(key, value);<br>    }<br>  };<br><br>  Object.keys(params).forEach(key =&gt; {<br>    addQuery(prefix ? `${prefix}[${key}]` : key, params[key]);<br>  });<br><br>  return query.toString();<br>}</pre><h4>parseQueryString Function</h4><p>This function parses a URL query string back into a JavaScript object.</p><pre>function parseQueryString(queryString) {<br>  const params = new URLSearchParams(queryString);<br>  const result = {};<br><br>  for (const [key, value] of params.entries()) {<br>    const keys = key.match(/[^\[\]]+/g);<br>    keys.reduce((acc, k, i) =&gt; {<br>      if (i === keys.length - 1) {<br>        acc[k] = isNaN(value) ? value : Number(value);<br>      } else {<br>        acc[k] = acc[k] || (isNaN(keys[i + 1]) ? {} : []);<br>      }<br>      return acc[k];<br>    }, result);<br>  }<br><br>  return result;<br>}</pre><h3>Integration with Vue.js</h3><p>To use the plugin in a Vue.js project, follow these steps:</p><ol><li>Create a Plugin File: Create a file named vue-query-utils.js in the src directory.</li></ol><pre>import { convertToQueryString, parseQueryString } from &#39;vue-query-utils&#39;;<br><br>export default {<br>  install(app) {<br>    app.config.globalProperties.$convertToQueryString = convertToQueryString;<br>    app.config.globalProperties.$parseQueryString = parseQueryString;<br>  }<br>};</pre><p>2. Register the Plugin</p><p>Register the plugin in your main application file (main.js or main.ts).</p><pre>import { createApp } from &#39;vue&#39;;<br>import App from &#39;./App.vue&#39;;<br>import vueQueryUtils from &#39;./vue-query-utils&#39;;<br><br>const app = createApp(App);<br><br>app.use(vueQueryUtils);<br><br>app.mount(&#39;#app&#39;);</pre><p>3. Use in Components</p><p>Use the provided methods in your Vue components.</p><pre>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;p&gt;Query String: {{ queryString }}&lt;/p&gt;<br>    &lt;p&gt;Parsed Object: {{ parsedObject }}&lt;/p&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;v<br><br>&lt;script&gt;<br>export default {<br>  data() {<br>    return {<br>      params: {<br>        cityName: &#39;mecca&#39;,<br>        checkIn: &#39;2024-10-05&#39;,<br>        checkOut: &#39;2024-10-10&#39;,<br>        stayDays: 8,<br>        occupancy: [<br>          {<br>            adults: 1,<br>            children: 1,<br>            ages: [5]<br>          }<br>        ]<br>      },<br>      queryString: &#39;&#39;,<br>      parsedObject: {}<br>    };<br>  },<br>  mounted() {<br>    // Convert to query string<br>    this.queryString = this.$convertToQueryString(this.params);<br>    <br>    // Parse back to object<br>    this.parsedObject = this.$parseQueryString(this.queryString);<br>  }<br>};<br>&lt;/script&gt;</pre><h3>Integration with Nuxt.js</h3><p>To use the plugin in a Nuxt.js project, follow these steps:</p><ol><li>Create a Plugin File</li></ol><p>Create a file named vue-query-utils.js in the plugins directory.</p><pre>import { defineNuxtPlugin } from &#39;#app&#39;;<br>import { convertToQueryString, parseQueryString } from &#39;vue-query-utils&#39;;<br><br>export default defineNuxtPlugin((nuxtApp) =&gt; {<br>  nuxtApp.provide(&#39;convertToQueryString&#39;, convertToQueryString);<br>  nuxtApp.provide(&#39;parseQueryString&#39;, parseQueryString);<br>});</pre><p>2. Register the Plugin</p><p>Ensure the plugin is automatically registered by placing it in the plugins directory with the correct naming convention.</p><p>3. Use in Components</p><p>Use the provided methods in your Nuxt components.</p><pre>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;p&gt;Query String: {{ queryString }}&lt;/p&gt;<br>    &lt;p&gt;Parsed Object: {{ parsedObject }}&lt;/p&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;<br><br>&lt;script setup&gt;<br>import { ref, onMounted } from &#39;vue&#39;;<br>import { useNuxtApp } from &#39;#app&#39;;<br><br>const params = ref({<br>  cityName: &#39;mecca&#39;,<br>  checkIn: &#39;2024-10-05&#39;,<br>  checkOut: &#39;2024-10-10&#39;,<br>  stayDays: 8,<br>  occupancy: [<br>    {<br>      adults: 1,<br>      children: 1,<br>      ages: [5]<br>    }<br>  ]<br>});<br><br>const queryString = ref(&#39;&#39;);<br>const parsedObject = ref({});<br><br>const { $convertToQueryString, $parseQueryString } = useNuxtApp();<br><br>onMounted(() =&gt; {<br>  // Convert to query string<br>  queryString.value = $convertToQueryString(params.value);<br>  <br>  // Parse back to object<br>  parsedObject.value = $parseQueryString(queryString.value);<br>});<br>&lt;/script&gt;</pre><h3>Results and Discussion</h3><p>The vue-query-utils plugin simplifies the management of query strings in Vue.js and Nuxt.js applications. By abstracting the conversion and parsing logic, developers can focus on building features without worrying about the intricacies of URL manipulation.</p><h3>Performance</h3><p>The performance of the plugin has been evaluated in various scenarios, including handling deeply nested objects and large arrays. The results indicate that vue-query-utils performs efficiently, with minimal overhead.</p><h3>Usability</h3><p>Feedback from early adopters suggests that the plugin is intuitive and easy to integrate. The ability to handle nested structures and arrays is particularly appreciated.</p><h3>Conclusion</h3><p>vue-query-utils is a valuable addition to any Vue.js or Nuxt.js project, providing a robust and flexible solution for query string management. By abstracting complex logic, it allows developers to work more efficiently and with fewer errors.</p><h3>Future Work</h3><p>Future enhancements may include:</p><ul><li>Support for additional query string formats.</li><li>Optimization for even larger datasets.</li><li>Enhanced error handling and validation.</li></ul><h3>References</h3><ul><li>Vue.js Documentation: <a href="https://vuejs.org/">https://vuejs.org/</a></li><li>Nuxt.js Documentation: <a href="https://nuxtjs.org/">https://nuxtjs.org/</a></li><li>URLSearchParams API: <a href="https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams">https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams</a></li><li>Plugin on NPM: <a href="https://www.npmjs.com/package/vue-query-utils">https://www.npmjs.com/package/vue-query-utils</a></li><li>Plugin on Github: <a href="https://github.com/ahmed-m-khatab/nuxt-querystring-encoder-decoder">https://github.com/ahmed-m-khatab/nuxt-querystring-encoder-decoder</a></li></ul><h3>Contributing</h3><p>Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1dc01cc55dfd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>