UNPKG

validation-query

Version:

**Validation Query** Validation Query is a utility library providing a range of functions for managing query strings in web applications. It offers features for validating, decoding, and parsing query string parameters, as well as inspecting the current e

81 lines (57 loc) 2.5 kB
**Validation Query** Validation Query is a utility library providing a range of functions for managing query strings in web applications. It offers features for validating, decoding, and parsing query string parameters, as well as inspecting the current environment and extracting the search query from the URL. **Installation** You can install the package using npm or yarn: ```bash npm install validation-query ``` or ```bash yarn add validation-query ``` **Usage** Import the desired functions from the package: ```javascript import { validateParams, toQueryString, parseBoolean, isClient, getSearchQuery } from 'validation-query'; ``` **Functions** 1. **validateParams** Validates query string parameters by removing any keys with falsy values. ```javascript import { validateParams } from 'validation-query'; const validParams = validateParams({ foo: 'bar', abc: '', def: null }); // { foo: 'bar' } ``` 2. **toQueryString** Decodes query string values by removing any keys with falsy values and decoding the resulting query string. ```javascript import { toQueryString } from 'validation-query'; const decodedQueryString = toQueryString({ foo: 'bar', abc: 'xyz' }); // 'foo=bar&abc=xyz' ``` 3. **parseBoolean** Parses boolean values from query strings. It returns true for any non-falsy string value, and false for falsy string values (case-insensitive "f", "false", "n", "no", and "0"). ```javascript import { parseBoolean } from 'validation-query'; const isTrue = parseBoolean('true'); // true const isFalse = parseBoolean('false'); // false ``` 4. **isClient** Checks if the current environment is a client-side environment (browser). ```javascript import { isClient } from 'validation-query'; if (isClient()) { // Client-side code } else { // Server-side code } ``` 5. **getSearchQuery** Retrieves the search query from the current URL and parses it using the query-string library. It accepts options for configuring the parsing behavior. ```javascript import { getSearchQuery } from 'validation-query'; const URL = "?foo=bar&abc=xyz&isSort=true&categoryId=1,2,3" const searchQuery = getSearchQuery({ parseNumbers: true , parseBooleans: true , arrayFormat:"comma" }); // {foo: "bar", abc:"xyz", isSort:true, categoryId: [1,2,3]} ``` **Support** If you encounter any issues or have questions, please feel free to reach out to us at garmabi.mohammad73@gmail.com.