-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
118 additions
and
54 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
let param = (a) => { | ||
let s = [], rbracket = /\[\]$/, | ||
isArray = function (obj) { | ||
return Object.prototype.toString.call(obj) === '[object Array]'; | ||
}, add = function (k, v) { | ||
v = typeof v === 'function' ? v() : v === null ? '' : v === undefined ? '' : v; | ||
s[s.length] = encodeURIComponent(k) + '=' + encodeURIComponent(v); | ||
}, buildParams = function (prefix, obj) { | ||
let i, len, key; | ||
|
||
if (prefix) { | ||
if (isArray(obj)) { | ||
for (i = 0, len = obj.length; i < len; i++) { | ||
if (rbracket.test(prefix)) { | ||
add(prefix, obj[i]); | ||
} else { | ||
buildParams(prefix + ':' + (typeof obj[i] === 'object' ? i : ''), obj[i]); | ||
} | ||
} | ||
} else if (obj && String(obj) === '[object Object]') { | ||
for (key in obj) { | ||
buildParams(prefix + ':' + key, obj[key]); | ||
} | ||
} else { | ||
add(prefix, obj); | ||
} | ||
} else if (isArray(obj)) { | ||
for (i = 0, len = obj.length; i < len; i++) { | ||
add(obj[i].name, obj[i].value); | ||
} | ||
} else { | ||
for (key in obj) { | ||
buildParams(key, obj[key]); | ||
} | ||
} | ||
return s; | ||
}; | ||
|
||
return buildParams('', a).join('&').replace(/%20/g, '+'); | ||
}; | ||
|
||
export {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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {param} from '../../../src/util/param' | ||
|
||
describe('param method', function () { | ||
|
||
it('should parse object to query params', function () { | ||
let obj = { | ||
a: 'literal', | ||
obj: { | ||
b: 'canon-literal', | ||
canon_array: [1, 2] | ||
}, | ||
array: ['c', 3, {d: 4}] | ||
}; | ||
let query_params_str = param(obj); | ||
let query_params_arr = decodeURIComponent(query_params_str).split('&'); | ||
query_params_arr[0].should.equal('a=literal'); | ||
query_params_arr[1].should.equal('obj:b=canon-literal'); | ||
query_params_arr[2].should.equal('obj:canon_array:=1'); | ||
query_params_arr[3].should.equal('obj:canon_array:=2'); | ||
query_params_arr[4].should.equal('array:=c'); | ||
query_params_arr[5].should.equal('array:=3'); | ||
query_params_arr[6].should.equal('array:2:d=4'); | ||
}); | ||
}); |