query-params-helpers

parse - Parameters - Examples - stringify - Parameters - Examples

Usage no npm install needed!

<script type="module">
  import queryParamsHelpers from 'https://cdn.skypack.dev/query-params-helpers';
</script>

README

Table of Contents

parse

Parse params from query string

Parameters

  • queryString String Query string. May include host and hash. If string not include query params thish function will parse hash params
  • options Object (optional, default {output:'tree',conversion:true})
    • options.output String Output format. This parameter must be an array or tree. (optional, default tree)
    • options.conversion Boolean If false, numbers and boolean values will be a strings (optional, default true)

Examples

import { parse } from 'query-params-helpers';

parse('?foo=bar'); 
// { 
//    foo: 'bar' 
// }

parse('?foo[]=1&foo[]=2'); 
// { 
//    foo: [1, 2] 
// }

parse('?foo=1&bar[x]=2&bar[y]=3'); 
// { 
//    foo: 1, 
//    bar: { 
//      x: 2, 
//      y: 3 
//    } 
// }

parse('?foo[0][x]=10&foo[0][y]=20&foo[1][x]=30&foo[1][y]=40'); 
// { 
//   foo: [{ 
//     x: 10, 
//     y: 20 
//   }, { 
//     x: 30, 
//     y: 40 
//   }] 
// }

Returns (Object | Array)

stringify

Convert object to query string

Parameters

  • object Object Source object
  • url Object default url (optional, default '')

Examples

import { stringify } from 'query-params-helpers';

stringify({ 
  foo: 'bar' 
}); 
// '?foo=bar'

stringify({ 
  foo: [1, 2] 
}); 
// '?foo[]=1&foo[]=2'

stringify({ 
  foo: 1, 
  bar: { 
    x: 1, 
    y: 2
  } 
}); 
// '?foo=1&bar[x]=2&bar[y]=3'

stringify({ 
  foo: [{ 
    x: 10, 
    y: 20 
  }, { 
    x: 30, 
    y: 40 
  }] 
}); 
// '?foo[0][x]=10&foo[0][y]=20&foo[1][x]=30&foo[1][y]=40'

Returns String