Git Product home page Git Product logo

qsomeness's Introduction

qsomeness

A zero-dependencies tiny ~ 8kb (2.1kb gzipped) tool to work with url querystrings. Works both on server and client. It automatically encode/decode params.

Build Status

how to

npm i qsomeness and ๐Ÿ’ฅ

If you look inside tests/spec.js, it should be straightforward understanding how to use it :) However, there's also an handy api reference.

API reference

URLObject

This is a quite nice feature as it allows to chain methods (and act as a proxy for static library methods) by setting an instance of URLObject. So it's quite useful if you want to do multiple things on an url w/out creating a string each time, and to keep it on a single instance. Let's see an example:

const { URLObject } = require('qsomeness');

const myUrlObj = new URLObject('http://google.com');

myUrlObj
  .add({ foo: ['bar', 'baz'] })
  .update({ foo: ['baz', 'boz'] })
  // .addMultiple
  // .updateMultiple
  // .remove
  // .removeMultiple
  // .removeSingleParam
  // .removeMultipleParams
  ;

console.log(myUrlObj.getUrl()); // 'http://google.com?foo=baz&foo=boz'
    
console.log(myUrlObj.getQuerystringObject()); // { foo: ['baz', 'boz'] };

get

const { get } = require('qsomeness');

const paramValue = get('http://google.com?foo=bar', 'foo');
// paramValue => ["bar"]

const multipleParams = get('http://google.com?foo=bar&foo=baz', 'foo');
// multipleParams => ["bar","baz"]

add

const { add } = require('qsomeness');

const newUrl = add('http://google.com?foo=bar', { q: 'baz' });
// newUrl => "http://google.com?foo=bar&q=baz"

const anotherUrl = add('http://google.com?foo=bar', { foo: 'baz' })
// anotherUrl => "http://google.com?foo=bar&foo=baz"

const thirdUrl = add('http://google.com', { foo: ['bar', 'baz'] });
// thirdUrl => "http://google.com?foo=bar&foo=baz"

addMultiple

const { addMultiple } = require('qsomeness');

const newUrl = addMultiple('http://google.com', [{ q: 'baz' }, { foo: 'bar' }]);
// newUrl => "http://google.com?q=baz&foo=bar"

update

const { update } = require('qsomeness');

const newUrl = update('http://google.com?foo=bar', { foo: 'baz' });
// newUrl => "http://google.com?foo=baz"

const anotherUrl = update('http://google.com?foo=bar', { foo: '' }, { removeEmpty: true });
// anotherUrl => "http://google.com"

updateMultiple

const { updateMultiple } = require('qsomeness');

const newUrl = updateMultiple('http://google.com?foo=bar', [{ foo: 'baz' }, { q: 'bizz' }]);
// newUrl => "http://google.com?foo=baz&q=bizz"

const anotherUrl = updateMultiple('http://google.com?foo=a&bar=b&qux=c', [{ foo: 'baz' }, { bar: null }, { qux: 'bar' }], { removeEmpty: true });
// anotherUrl => "http://google.com?foo=baz&qux=bar

remove

const { remove } = require('qsomeness');

const newUrl = remove('http://google.com?foo=bar', 'foo');
// newUrl => "http://google.com"

const anotherUrl = remove('http://google.com?foo=bar&q=baz', 'foo');
// newUrl => "http://google.com?q=baz"

removeMultiple

const { removeMultiple } = require('qsomeness');

const newUrl = removeMultiple('http://google.com?foo=bar&foo=baz&q=string&key=val', ['foo', 'key']);
// newUrl => "http://google.com?q=string"

removeSingleParam

const { removeSingleParam } = require('qsomeness');

const newUrl = removeSingleParam('http://google.com?foo=bar&foo=baz', { foo: 'bar' });
// newUrl => "http://google.com?foo=baz"

removeMultipleParams

const { removeMultipleParams } = require('qsomeness');

const newUrl = removeMultipleParams('http://google.com?foo=bar&foo=baz&q=string', [{ foo: 'bar' }, { q: 'string' }]);
// newUrl => "http://google.com?foo=baz"

getQuerystringObject

const { getQuerystringObject } = require('qsomeness');

const qsParamsObject = getQuerystringObject('http://google.com?foo=bar&foo=baz&q=fizz');
// qsParamsObject => { foo: ['bar', 'baz'], q: 'fizz' }

setParam

const { setParam } = require('qsomeness');

const myParam = setParam('foo', 'bar');
// myParam => "foo=bar"

const myArrayParam = setParam('foo', ['bar', 'baz']);
// myArrayParam => "foo=bar&foo=baz"

Usage in browser

If you're using qsomeness in the browser, you can get the current window url with getUrl(). This feature will throw an Error if called from the server.

const { URLObject } = require('qsomeness');

const myUrlObj = new URLObject();

console.log(myUrlObj.getUrl()); // current url in browser page

Contributing

Just make a PR ๐Ÿบ

qsomeness's People

Contributors

stecb avatar pasalino avatar

Watchers

James Cloos avatar Charlene Tshitoka 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.