Socket
Socket
Sign inDemoInstall

query-params-parser

Package Overview
Dependencies
0
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    query-params-parser

A simple library to parse query params into proper types


Version published
Weekly downloads
57
decreased by-3.39%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

#query-params-parser

In a Nutshell

This library is supposed to fix the problem of always returning strings from the query parameters, to prevent problems or bad conversions like pageNumber to be a string, or "readOnly" to carry the "true" instead of the boolean value true.

It is only focused on fixing the query/search params, the one you get from window.location.search or with the pattern ?param1=value1&param2=100

Installation

npm i query-params-parser

Documentation

.js / .ts

import parseParams from 'query-params-parser';

class ReferenceClass {
  prop1 = '';
  prop2 = 0;
  prop3 = false;
  prop4 = [];
}

/* 
This class will work for the following uri:
home/page?prop1=value%20as%20string&prop2=100&prop3=no&prop4=item1,item2,item3

and will return the following object:
*/
{
    "prop1": "value as string",
    "prop2": 100,
    "prop3": false,
    "prop4": [
        "item1",
        "item2",
        "item3"
    ]
}

console.log('result by class', parseParams(new ReferenceClass()))

// or the following with object declaration

console.log('result by object', parseParams({prop1: '', prop2: 0}))

Running helpers

import { stringToBoolean, parseByType} from 'query-params-parser';

const byType = parseByType('item1, item2', [], ', '); 
// byType will be ['item1', 'item2'];


const fromString = stringToBoolean('yes');
// fromString will be a boolean true;

As javascript is a weak typed language, we cannot use the type definition to parse properties, so it is expecting a initialized object like a class with values defined to have typescript checking for its types.

parseParams (default export) parameters

Property NameDescriptionRequired / Default
baseObjectobject with initialized values, like { prop1: "test", prop2: 100 }yes / none
paramsinitialized class URLSearchParamsno / new URLSearchParams(window.location.search)
arrSeparatorwhen spliting the array from the search param it expects a split string .split(arrSeparator)no / ','

parseByType parameters

Property NameDescriptionRequired / Default
variablevariable carrying the not parsed value typed as a stringyes / none
propertyexpects a primitive value like 'this', 10, false or ['t', 'e']yes / none
arrSeparatorwhen spliting the array from the search param it expects a split string .split(arrSeparator)yes / none

stringToBoolean parameters

Property NameDescriptionRequired / Default
stringValuestring with the value to be parsedyes / none

examples of string values, they are not case sensitive

sectionexamples
values for true"true", "yes" and "1"
values for false"false", "no", "0", null, undefined, default

Keywords

FAQs

Last updated on 12 Jan 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc