PHP's http_build_query in JavaScript

Here’s what our current JavaScript equivalent to PHP's http_build_query looks like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module.exports = function http_build_query (formdata, numericPrefix, argSeparator) { // eslint-disable-line camelcase
// discuss at: http://locutus.io/php/http_build_query/
// original by: Kevin van Zonneveld (http://kvz.io)
// improved by: Legaev Andrey
// improved by: Michael White (http://getsprink.com)
// improved by: Kevin van Zonneveld (http://kvz.io)
// improved by: Brett Zamir (http://brett-zamir.me)
// revised by: stag019
// input by: Dreamer
// bugfixed by: Brett Zamir (http://brett-zamir.me)
// bugfixed by: MIO_KODUKI (http://mio-koduki.blogspot.com/)
// note 1: If the value is null, key and value are skipped in the
// note 1: http_build_query of PHP while in locutus they are not.
// example 1: http_build_query({foo: 'bar', php: 'hypertext processor', baz: 'boom', cow: 'milk'}, '', '&')
// returns 1: 'foo=bar&php=hypertext+processor&baz=boom&cow=milk'
// example 2: http_build_query({'php': 'hypertext processor', 0: 'foo', 1: 'bar', 2: 'baz', 3: 'boom', 'cow': 'milk'}, 'myvar_')
// returns 2: 'myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&php=hypertext+processor&cow=milk'

var urlencode = require('../url/urlencode')

var value
var key
var tmp = []

var _httpBuildQueryHelper = function (key, val, argSeparator) {
var k
var tmp = []
if (val === true) {
val = '1'
} else if (val === false) {
val = '0'
}
if (val !== null) {
if (typeof val === 'object') {
for (k in val) {
if (val[k] !== null) {
tmp.push(_httpBuildQueryHelper(key + '[' + k + ']', val[k], argSeparator))
}
}
return tmp.join(argSeparator)
} else if (typeof val !== 'function') {
return urlencode(key) + '=' + urlencode(val)
} else {
throw new Error('There was an error processing for http_build_query().')
}
} else {
return ''
}
}

if (!argSeparator) {
argSeparator = '&'
}
for (key in formdata) {
value = formdata[key]
if (numericPrefix && !isNaN(key)) {
key = String(numericPrefix) + key
}
var query = _httpBuildQueryHelper(key, value, argSeparator)
if (query !== '') {
tmp.push(query)
}
}

return tmp.join(argSeparator)
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/url/http_build_query'). You could also require the url module in full so that you could access url.http_build_query instead.

If you intend to target the browser, you can then use a module bundler such as Browserify, webpack or rollup.js.

ES5/ES6

This function targets ES5, but as of Locutus 2.0.2 we also support ES6 functions. Locutus transpiles to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Notes

  • If the value is null, key and value are skipped in the http_build_query of PHP while in locutus they are not.

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1http_build_query({foo: 'bar', php: 'hypertext processor', baz: 'boom', cow: 'milk'}, '', '&')'foo=bar&php=hypertext+processor&baz=boom&cow=milk'
2http_build_query({'php': 'hypertext processor', 0: 'foo', 1: 'bar', 2: 'baz', 3: 'boom', 'cow': 'milk'}, 'myvar_')'myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&php=hypertext+processor&cow=milk'

« More PHP url functions