Code Monkey home page Code Monkey logo

query-string's Introduction

query-string Travis-ci Status GitHub license typescript | javascript | node.js Npm Version NPM Download

A simple and lightweight QueryString library by TypeScript for node.js or browsers.

Installation

Installation is done using the npm install command:

npm install -S @billjs/query-string

Overview

API

parse

Parse the string to an object. Support the parsing of multiple keys with the same name, and parse them into arrays. The parsed keys and values are decoded by decodeURIComponent function. If you specified the fn argument, that means you can to use it for customize value.

  • str (string) source string
  • [sep] (string | null optional) group separator, default &
  • [eq] (string | null optional) key-value separator, default =
  • [fn] (ParseFunction) a function, it can be used to customize return values.
  • return (object)

Parse a normal query-string.

const data = parse('a=1&b=s');
console.log(data); // ==> { a: '1', b: 's' }

Parse the search from URL.

const data = parse('http://foo.com?a=1&b=s');
console.log(data); // ==> { a: '1', b: 's' }

Parse it when if key or value is encoded.

const data = parse('test%3D=test%20%26*%20test');
console.log(data); // ==> { 'test=': 'test &* test' }

Parse it when if sep and eq are specified.

const data = parse('a#1|b#s|b#s2', '|', '#');
console.log(data); // ==> { a: '1', b: ['s', 's2'] }

Parse the same name key as an array.

const data = parse('a=1&b=s1&b=s2');
console.log(data); // ==> { a: '1', b: ['s1', 's2'] }

Parse it for customize value.

const fn = (key: string, value: string) => {
  if (key === 'b') return +value;
  if (key === 'c') return { on: true, off: false }[value];
  return value;
};
const data = parse('a=test&b=1&c=on', null, null, fn);
console.log(data); // ==> { a: 'test', b: 1, c: true }

stringify

Stringify the object to a string. Support the stringifying of arrays into keys of the same name. The stringified keys and values are encoded by encodeURIComponent function. If you specified the fn argument, that means you can to use it for customize value.

  • obj (object) source object
  • [sep] (string | null optional) group separator, default &
  • [eq] (string | null optional) key-value separator, default =
  • [fn] (StringifyFunction) a function, it can be used to customize return values.
  • return (string)

Stringify an object.

const query = stringify({ a: '1', b: 's' });
console.log(query); // ==> 'a=1&b=s'

Stringify the array to same name key.

const query = stringify({ a: '1', b: ['s1', 's2'] });
console.log(query); // ==> 'a=1&b=s1&b=s2'

Stringify the boolean value.

const query = stringify({ a: '1', b: 's', c: false });
console.log(query); // ==> 'a=1&b=s&c=false'

Stringify it when if the key or value need to encoded.

const query = stringify({ 'test=': 'test &* test' });
console.log(query); // ==> 'test%3D=test%20%26*%20test'

Stringify it when if sep and eq are specified.

const query = stringify({ a: '1', b: ['s', 's2'] }, '|', '#');
console.log(query); // ==> 'a#1|b#s|b#s2'

Stringify it for customize value.

const fn = (key, value) => {
  if (key === 'c') return value ? 'on' : 'off';
  return value;
};
const obj = { a: 'test', b: 1, c: true };
const query = stringify(obj, null, null, fn);
console.log(query); // ==> 'a=test&b=1&c=on'

ParseFunction

A function for customize parse.

The declaration is like this:

type ParseFunction = (key: string, value: string) => any;

StringifyFunction

A function for customize stringify.

The declaration is like this:

type StringifyFunction = (key: string, value: any) => any;

License

MIT License

query-string's People

Contributors

billjs avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.