Skip to content

Commit

Permalink
Merge pull request #69 from reapit/task/add-utils-query-params
Browse files Browse the repository at this point in the history
add utils query-params
  • Loading branch information
dannd4 committed Sep 23, 2019
2 parents aa1bee4 + 86bfcd0 commit ed6dc4e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from './components/Pagination'
export * from './utils/cognito'
export * from './utils/fetcher/fetcher'
export * from './utils/datetime/datetime'
export * from './utils/query-params/query-params'
export * from './components/Menu'
export * from './components/AppointmentTile'
export * from './components/Map'
Expand Down
15 changes: 15 additions & 0 deletions src/utils/query-params/__tests__/query-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { setQueryParams } from '../query-params'

describe('setQueryParams', () => {
it('should create query params', () => {
;[
[{ name: '1', address: '1' }, 'name=1&address=1'],
[{ name: '', address: '1' }, 'address=1'],
[{ name: ['a', 'b'], address: '1' }, 'name=a&name=b&address=1'],
[{ name: null, address: undefined }, '']
].forEach(([params, expected]) => {
const result = setQueryParams(params)
expect(result).toBe(expected)
})
})
})
11 changes: 11 additions & 0 deletions src/utils/query-params/query-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const setQueryParams = (params: Object) => {
return Object.keys(params)
.filter(key => params[key] !== undefined && params[key] !== null && params[key] !== '')
.map(key => {
if (Array.isArray(params[key])) {
return params[key].map((value: any) => `${key}=${value}`).join('&')
}
return `${key}=${params[key]}`
})
.join('&')
}

0 comments on commit ed6dc4e

Please sign in to comment.