Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 636d302

Browse files
author
Alan Shaw
committed
perf: use URLSearchParams in the browser
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent cea1bd5 commit 636d302

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"glob": false,
1919
"fs": false,
2020
"stream": "readable-stream",
21-
"./src/lib/configure.js": "./src/lib/configure.browser.js"
21+
"./src/lib/configure.js": "./src/lib/configure.browser.js",
22+
"./src/lib/querystring.js": "./src/lib/querystring.browser.js"
2223
},
2324
"repository": "github:ipfs/js-ipfs-http-client",
2425
"scripts": {

src/lib/querystring.browser.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
// Convert an object to a query string INCLUDING leading ?
4+
// Excludes null/undefined values
5+
exports.objectToQuery = obj => {
6+
if (!obj) return ''
7+
8+
let qs = new URLSearchParams()
9+
10+
for (const [key, value] of Object.entries(obj)) {
11+
if (value != null) {
12+
if (Array.isArray(value)) {
13+
value.forEach(v => qs.append(key, v))
14+
} else {
15+
qs.append(key, value)
16+
}
17+
}
18+
}
19+
20+
qs = qs.toString()
21+
22+
return qs ? `?${qs}` : qs
23+
}

0 commit comments

Comments
 (0)