-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
44 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { mapValues } from './map-values'; | ||
|
||
describe('mapValues', () => { | ||
it('should apply the callback to every value of the source', () => { | ||
const o = { | ||
one: 1, | ||
two: 2, | ||
three: 3, | ||
four: 4, | ||
}; | ||
const e = { | ||
one: 2, | ||
two: 4, | ||
three: 6, | ||
four: 8, | ||
}; | ||
const result = mapValues(o, item => item + item); | ||
expect(result).toEqual(e); | ||
}); | ||
|
||
it('should support strings', () => { | ||
const o = { one: 'one' }; | ||
const e = { one: 'ONE' }; | ||
const result = mapValues(o, item => item.toUpperCase()); | ||
expect(result).toEqual(e); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Creates an object with the same keys as the `source` object and values generated by running each own enumerable property of `source` thru the `fn` function. | ||
* | ||
* @param source Object to map the values from. | ||
* @param fn Function to apply to each value. | ||
*/ | ||
|
||
export function mapValues<T, K extends keyof T>( | ||
source: T, | ||
fn: (value: T[K], key: K) => T[K] | ||
) { | ||
const entries = Object.entries(source).map<[K, T[K]]>(([key, value]) => [ | ||
key as K, | ||
fn(value, key as K), | ||
]); | ||
return Object.fromEntries(entries); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ | |
"paths": { | ||
"*": ["src/*", "node_modules/*"] | ||
}, | ||
"jsx": "react", | ||
"esModuleInterop": true | ||
} | ||
} |