Skip to content

Commit

Permalink
feat: utility to lowercase object properties
Browse files Browse the repository at this point in the history
refs antongolub#33

- Adds utility that will help mimicing lowercasing of headers like it's done in express' Request object
- Code heavily inspired by https://github.com/howardabrams/node-mocks-http/blob/a0ad193459c102069a9b7de386f16ebf59cab6e0/lib/utils.js#L3-L9
  • Loading branch information
naz committed Feb 15, 2022
1 parent 88b97d8 commit 3cc79c9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,13 @@ export function appendAdditionalProps (target: Object, props: Object): void {
export function concat (...strings: Array<?string>): string {
return strings.join('')
}

export function convertKeysToLowerCase (map: Object): Object {
const newMap = {}

for (const key in map) {
newMap[key.toLowerCase()] = map[key]
}

return newMap
}
21 changes: 21 additions & 0 deletions src/test/js/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { convertKeysToLowerCase } from '../../main/js/util'

describe('util', () => {
describe('convertKeysToLowerCase', () => {
it('converts object keys to lower case keys', () => {
const map = {
prop: 'value',
standard_Notation: 42,
'BaZ-Header': 'qux'
}

const lowerCasedMap = convertKeysToLowerCase(map)

expect(map.standard_Notation).toEqual(42, 'original map is not modified')

expect(lowerCasedMap.prop).toEqual('value')
expect(lowerCasedMap.standard_notation).toEqual(42)
expect(lowerCasedMap['baz-header']).toEqual('qux')
})
})
})

0 comments on commit 3cc79c9

Please sign in to comment.