Skip to content

Commit daf4b3a

Browse files
committed
refactor: simplify query params
1 parent eded16c commit daf4b3a

File tree

2 files changed

+4
-13
lines changed

2 files changed

+4
-13
lines changed

src/utils.js

+2-11
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,8 @@ export const createMemberPatchQuery = ({ members, operation }) => {
2222

2323
export const buildUrlWithParams = (url, params = {}) => {
2424
const queryParams = Object.keys(params)
25-
.reduce((list, key) => {
26-
const currentValue = params[key]
27-
if (currentValue !== undefined && currentValue !== null) {
28-
return [
29-
...list,
30-
`${encodeURIComponent(key)}=${encodeURIComponent(currentValue)}`
31-
]
32-
} else {
33-
return list
34-
}
35-
}, [])
25+
.filter((k) => params[k] !== undefined && params[k] !== null)
26+
.map(k => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`)
3627
.join('&')
3728

3829
return queryParams ? `${url}?${queryParams}` : url

tests/unit/utils.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test('parameters should be enconded', () => {
2323
expect(buildUrlWithParams(url, params)).toBe('http://typeform.com?a=%401&b=%232')
2424
})
2525

26-
test.only('undefined values for parameter will be skipped', () => {
26+
test('undefined values for parameter will be skipped', () => {
2727
const url = 'http://typeform.com'
2828
const params = {
2929
a: '@1',
@@ -32,7 +32,7 @@ test.only('undefined values for parameter will be skipped', () => {
3232
expect(buildUrlWithParams(url, params)).toBe('http://typeform.com?a=%401')
3333
})
3434

35-
test.only('falsy values should be passed', () => {
35+
test('falsy values should be passed', () => {
3636
const url = 'http://typeform.com'
3737
const params = {
3838
a: '0',

0 commit comments

Comments
 (0)