sabberworm / deparam

$.param reverse function

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

deparam()

A $.param() reverse function without dependencies. Supports array-parameters and object-parameters with infinite nesting. An extracted and refactored deparam method from Ben Alman's jQuery BBQ

Tests

  it('should deparam obj', function () {
    const target = {a: "1", b: "qwerty"};
    const target2 = deparam(decodeURIComponent($.param(target)));

    expect(target2).eql(target);
  })

  it('should deparam obj with array property', function () {
    const target = {a: ["x", "y"]};
    const target2 = deparam(decodeURIComponent($.param(target)));

    expect(target2).eql(target);
  })

  it('should deparam obj with obj property', function () {
    const target = {a: {b1: "x", b2: "y"}};
    const target2 = deparam(decodeURIComponent($.param(target)));

    expect(target2).eql(target);
  })

  it('should deparam obj with obj with obj and arr property', function () {
    const target = {o1: {o2: {s: "x", a: ["a1", "a2"]}}};
    const target2 = deparam(decodeURIComponent($.param(target)));

    expect(target2).eql(target);
  })

Installation

npm i deparam

Usage

const deparam = require("deparam");
const query = "a=1&b=qwerty";
const params = deparam(query); // params == {a:"1", b:"qwerty"}
ezoic increase your site revenue

About

$.param reverse function


Languages

Language:JavaScript 100.0%