Code Monkey home page Code Monkey logo

tiny-querystring's Introduction

tiny-querystring

Build Status Coverage Status License

Tiny parsing and formatting URL query strings for Node.js and browser. (309B only)

Installation

NPM
npm install tiny-querystring
Yarn
yarn add tiny-querystring
1998 Script Tag
<script src="https://unpkg.com/tiny-querystring/dist/tiny-querystring.umd.js"></script>

Usage

parse(str)

Parses a URL query string (str) into a collection of key and value pairs.

Example
import { parse } from 'tiny-querystring';
parse('foo=bar&abc=xyz&abc=123');

/* returns { foo: 'bar', abc: ['xyz', '123'] } */

stringify(obj)

Produces a URL query string from a given obj by iterating through the object's "own properties".

Example
import { stringify } from 'tiny-querystring';
stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' });

/* returns 'foo=bar&baz=qux&baz=quux&corge=' */

Contributing

Contributions welcome! See the Contributing Guide

License

MIT

tiny-querystring's People

Contributors

cap32 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

amit08255

tiny-querystring's Issues

parse smaller

feel free to steal my code ; )

var parse = (hashString) => (
  Object.fromEntries(
    hashString.slice(1).split('&')
    .map(keyval => (
      cut = keyval.indexOf('='),
      (cut == -1) ? [keyval, true] : (
        key = keyval.slice(0, cut),
        val = decodeURIComponent(keyval.slice(cut + 1)),
        [key, val]
      )
    ))
    .filter(([key, val]) => Boolean(key))
  )
)

sample

> parse('#&&=ignoreMe&foo=bar&foo2=bar=baz=boo&keyOnly')
{ foo: 'bar', foo2: 'bar=baz=boo', keyOnly: true }
a bit more complex (parse numbers and number arrays)
var parseGet = (typekeys) => {
  typekeys = Object.fromEntries(['number', 'numberArray'].map(type => (
    keys = typekeys[type],
    [type, (Array.isArray(keys) ? new Set(keys) : keys.has ? keys : new Set())]
  )));
  if (Array.isArray(keys.number)) keys.number = new Set(numKeys);
  if (!keys.number || !keys.number.has) keys.number = new Set();
  var parse = (hashString) => (
    Object.fromEntries(
      hashString.slice(1).split('&')
      .map(keyval => (
        cut = keyval.indexOf('='),
        (cut == -1) ? [keyval, true] : (
          key = keyval.slice(0, cut),
          raw = keyval.slice(cut + 1),
          val = (
            typekeys.number.has(key) ? parseFloat(raw) :
            typekeys.numberArray.has(key) ? raw.split(',').map(parseFloat) :
            decodeURIComponent(raw)
          ),
          [key, val]
        )
      ))
      .filter(([key, val]) => Boolean(key))
    )
  );
  return parse;
}

var parse = parseGet(); // default parser

var parse = parseGet({ number: ['num1', 'num2'], numberArray: ['numarr1', 'numarr2'] });

parse('#&&=ignoreMe&foo=bar&foo2=bar=baz=boo&keyOnly&num1=1234.5678&numarr1=1.0,2,3,4.567')
var res = {
  foo: 'bar',
  foo2: 'bar=baz=boo',
  keyOnly: true,
  num1: 1234.5678,
  numarr1: [ 1, 2, 3, 4.567 ]
}

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.