npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-query-utils

v1.0.5

Published

A utility for converting objects to query strings and vice versa for Vue.js and Nuxt.js

Downloads

205

Readme

Vue Query Utils

Vue Query Utils

npm version

A utility for converting objects to query strings and vice versa, specifically designed for Vue.js and Nuxt.js projects.

Features

  • Convert any JavaScript object to a URL query string.
  • Parse URL query strings back into JavaScript objects.
  • Easy integration with Vue.js and Nuxt.js.
  • Handles nested objects and arrays.

Installation

Install the package using npm or yarn:

npm install vue-query-utils

Usage

In a Vue.js 3 Project

  1. Create a Plugin

Create a file named vue-query-utils.js in the src directory of your Vue project.

src/vue-query-utils.js

 import { convertToQueryString, parseQueryString } from 'vue-query-utils';

 export default {
   install(app) {
     app.config.globalProperties.$convertToQueryString = convertToQueryString;
     app.config.globalProperties.$parseQueryString = parseQueryString;
   }
 };
  1. Register the Plugin

Register the plugin in your main application file, usually main.js or main.ts.

src/main.js

  import { createApp } from 'vue';
  import App from './App.vue';
  import vueQueryUtils from './vue-query-utils';

  const app = createApp(App);

  app.use(vueQueryUtils);

  app.mount('#app');
  1. Use in Components
  <template>
    <div>
      <p>Query String: {{ queryString }}</p>
      <p>Parsed Object: {{ parsedObject }}</p>
    </div>
  </template>

  <script>
  export default {
    data() {
      return {
        params: {
          cityName: 'mecca',
          checkIn: '2024-10-05',
          checkOut: '2024-10-10',
          stayDays: 8,
          occupancy: [
            {
              adults: 1,
              children: 1,
              ages: [5]
            }
          ]
        },
        queryString: '',
        parsedObject: {}
      };
    },
    mounted() {
      // Convert to query string
      this.queryString = this.$convertToQueryString(this.params);
      
      // Parse back to object
      this.parsedObject = this.$parseQueryString(this.queryString);
    }
  };
  </script>

In a Nuxt.js 3 Project

  1. Create a Plugin

    Create a file named vue-query-utils.js in the plugins directory of your Nuxt.js project.

    // plugins/vue-query-utils.js
     import { defineNuxtPlugin } from '#app';
     import { convertToQueryString, parseQueryString } from 'vue-query-utils';
    
     export default defineNuxtPlugin((nuxtApp) => {
         nuxtApp.provide('convertToQueryString', convertToQueryString);
         nuxtApp.provide('parseQueryString', parseQueryString);
     });
  2. Register the Plugin

    In Nuxt 3, plugins are automatically registered if they are placed in the plugins directory with the correct naming conventions. Ensure that your plugin file name ends with .js or .ts.

  3. Use in Components

    Now you can use the $convertToQueryString and $parseQueryString methods in your Vue components.

    <template>
      <div>
        <p>Query String: {{ queryString }}</p>
        <p>Parsed Object: {{ parsedObject }}</p>
      </div>
    </template>
    
    <script setup>
         import { ref, onMounted } from 'vue';
         import { useNuxtApp } from '#app';
    
         const params = ref({
             hotel: 'hotel-name',
             checkIn: '2024-10-05',
             checkOut: '2024-10-10',
             stayDays: 5,
             occupancy: [
                 {
                     adults: 1,
                     children: 1,
                     ages: [5]
                 }
             ]
         });
    
         const queryString = ref('');
         const parsedObject = ref({});
    
         const { $convertToQueryString, $parseQueryString } = useNuxtApp();
    
         onMounted(() => {
             // Convert to query string
             queryString.value = $convertToQueryString(params.value);
                
             // Parse back to object
             parsedObject.value = $parseQueryString(queryString.value);
         });
     </script>

In a Nuxt.js 2 Project

  1. Create a Plugin

    Create a file named vue-query-utils.js in the plugins directory of your Nuxt.js project.

    // plugins/vue-query-utils.js
    import Vue from 'vue';
    import { convertToQueryString, parseQueryString } from 'vue-query-utils';
    
    Vue.prototype.$convertToQueryString = convertToQueryString;
    Vue.prototype.$parseQueryString = parseQueryString;
  2. Register the Plugin

    Add the plugin to your nuxt.config.js file.

    // nuxt.config.js
    export default {
      // Other configurations...
      plugins: [
        '~/plugins/vue-query-utils.js'
      ]
    };
  3. Use in Components

    Now you can use the $convertToQueryString and $parseQueryString methods in your Vue components.

    <template>
      <div>
        <p>Query String: {{ queryString }}</p>
        <p>Parsed Object: {{ parsedObject }}</p>
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          params: {
            hotel: "hotel-name",
            checkIn: "2024-10-05",
            checkOut: "2024-10-10",
            stayDays: 5,
            occupancy: [
              {
                adults: 1,
                children: 1,
                ages: [5]
              }
            ]
          },
          queryString: '',
          parsedObject: {}
        };
      },
      mounted() {
        // Convert to query string
        this.queryString = this.$convertToQueryString(this.params);
           
        // Parse back to object
        this.parsedObject = this.$parseQueryString(this.queryString);
      }
    };
    </script>

API

convertToQueryString(params, prefix = '')

Converts a JavaScript object to a URL query string.

  • params (Object): The object to be converted.
  • prefix (String): An optional prefix for nested objects.

Returns: A URL query string.

parseQueryString(queryString)

Parses a URL query string into a JavaScript object.

  • queryString (String): The URL query string to be parsed.

Returns: A JavaScript object.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.