Last active
November 29, 2018 15:26
-
-
Save boriscy/0322df7a1cef79977f20756e7ea7247e to your computer and use it in GitHub Desktop.
replace of jQuery.param
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts an Object to params similar to jQuery.param function | |
* @param {Object} params | |
* @param {Array} keys | |
* @return {String} | |
*/ | |
getUrlString (params, keys = [], isArray = false) { | |
const p = Object.keys(params).map(key => { | |
let val = params[key] | |
if ("[object Object]" === Object.prototype.toString.call(val) || Array.isArray(val)) { | |
if (Array.isArray(params)) { | |
keys.push("") | |
} else { | |
keys.push(key) | |
} | |
return getUrlString(val, keys, Array.isArray(val)) | |
} else { | |
let tKey = key | |
if (keys.length > 0) { | |
const tKeys = isArray ? keys : [...keys, key] | |
tKey = tKeys.reduce((str, k) => { return "" === str ? k : `${str}[${k}]` }, "") | |
} | |
if (isArray) { | |
return `${ tKey }[]=${ val }` | |
} else { | |
return `${ tKey }=${ val }` | |
} | |
} | |
}).join('&') | |
keys.pop() | |
return p | |
} | |
describe("getUrlParams", () => { | |
test("normal", () => { | |
const p = {a: 1, b: "value", other: true} | |
expect(vm.getUrlParams(p)).toEqual(encodeURI("a=1&b=value&other=true")) | |
}) | |
test("objects within objects", () => { | |
const p = {a: 1, b: "value", obj: {a: 2, b: {val: "str", dec: 12.3, bool: false} }} | |
const resp = encodeURI("a=1&b=value&obj[a]=2&obj[b][val]=str&obj[b][dec]=12.3&obj[b][bool]=false") | |
expect(vm.getUrlParams(p)).toEqual(resp) | |
}) | |
test("array vals", () => { | |
const p = {a: 1, b: "value", obj: {a: 2, b: {val: "str", dec: 12.3, bool: false, arr: [1, 2, "s"]} }} | |
const resp = encodeURI("a=1&b=value&obj[a]=2&obj[b][val]=str&obj[b][dec]=12.3&obj[b][bool]=false&obj[b][arr][]=1&obj[b][arr][]=2&obj[b][arr][]=s") | |
expect(vm.getUrlParams(p)).toEqual(resp) | |
}) | |
test("multiple arrays", () => { | |
const p = {a: 1, b: [true, false, "bs"], obj: {a: 2, b: {val: "str", dec: 12.3, bool: false, arr: [1, 2, "s"]} }} | |
const arr = [1, 2, 3] | |
const resp = "a=1&b[]=true&b[]=false&b[]=bs&obj[a]=2&obj[b][val]=str&obj[b][dec]=12.3&obj[b][bool]=false&obj[b][arr][]=1&obj[b][arr][]=2&obj[b][arr][]=s" | |
expect(vm.getUrlString(p)).toEqual(resp) | |
}) | |
test("all kinds with very deep objectt", () => { | |
const p = { | |
a: 7, b: "string", c: false, d: 12.3, | |
arr: [1, 2.3, {a: 1, b: 3}], | |
obj: {a: 2, b: {arr: [1, 2.5, false], obj: {a: 1.1, b: ["str", 1, 2, true]} } } | |
} | |
const resp = "a=7&b=string&c=false&d=12.3&arr[]=1&arr[]=2.3&arr[][a]=1&arr[][b]=3&obj[a]=2&obj[b][arr][]=1&obj[b][arr][]=2.5&obj[b][arr][]=false&obj[b][obj][a]=1.1&obj[b][obj][b][]=str&obj[b][obj][b][]=1&obj[b][obj][b][]=2&obj[b][obj][b][]=true" | |
expect(vm.getUrlString(p)).toEqual(resp) | |
expect(vm.getUrlParams(p)).toEqual(encodeURI(resp)) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment