-
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
5 changed files
with
76 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 |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# Changelog | ||
|
||
## 1.0.1 (DEV) | ||
|
||
- Add: `fromArray()`; | ||
|
||
## 1.0.0 (2022-06-27) | ||
|
||
- Initial release. |
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
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
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,26 @@ | ||
/** | ||
* Creates a map from simple array of items, calculating corresponding key for | ||
* every item with the given callback `calcKey()`. | ||
* | ||
* ```ts | ||
* fromArray([1, 2, 3], (v) => v * 10) | ||
* // => Map(3) { 10 => 1, 20 => 2, 30 => 3 } | ||
* ``` | ||
* | ||
* This is a shortcut for commonly used operation: | ||
* | ||
* ```js | ||
* new Map(array.map(v => [calcKey(v), v])) | ||
* ``` | ||
* | ||
* @param array | ||
* @param calcKey | ||
*/ | ||
function fromArray<T, K>( | ||
array: readonly T[], | ||
calcKey: (item: T) => K, | ||
): ReadonlyMap<K, T> { | ||
return new Map(array.map(item => [calcKey(item), item])); | ||
} | ||
|
||
export default fromArray; |
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,22 @@ | ||
import fromArray from '../src/fromArray'; | ||
|
||
it('creates map', () => { | ||
interface T { | ||
id: number; | ||
name: string; | ||
} | ||
|
||
const array: T[] = [ | ||
{ id: 10, name: 'John Random' }, | ||
{ id: 20, name: 'Pupkin Vasily' }, | ||
{ id: 30, name: 'Lu Lu' }, | ||
]; | ||
|
||
expect(fromArray(array, v => v.id)).toEqual( | ||
new Map([ | ||
[10, { id: 10, name: 'John Random' }], | ||
[20, { id: 20, name: 'Pupkin Vasily' }], | ||
[30, { id: 30, name: 'Lu Lu' }], | ||
]), | ||
); | ||
}); |