-
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
1 parent
4c1d779
commit a95c186
Showing
12 changed files
with
95 additions
and
22 deletions.
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
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
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
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,17 @@ | ||
declare global { | ||
export interface Array<T> { | ||
mapAndFilter<V>(mapper: (value: T) => V|undefined): V[] | ||
} | ||
} | ||
|
||
Array.prototype.mapAndFilter = function<T, V>(mapper: (value: T) => V|undefined): V[] { | ||
const result: V[] = [] | ||
this.forEach((element: T) => { | ||
const mapped = mapper(element) | ||
if (mapped !== undefined) { | ||
result.push(mapped) | ||
} | ||
}) | ||
|
||
return result | ||
} |
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,32 @@ | ||
declare global { | ||
export interface Map<K, V> { | ||
getWithDefault(key: K, defaultValue: V): V | ||
} | ||
export interface MapConstructor { | ||
fromObject<V>(obj: Record<string, V>): Map<string, V> | ||
fromArray<V>(array: Array<V>, mapToKey: (value: V) => string): Map<string, V> | ||
} | ||
} | ||
|
||
Map.prototype.getWithDefault = function<K, V>(key: K, defaultValue: V): V { | ||
if (this.has(key)) { | ||
return this.get(key) | ||
} | ||
|
||
this.set(key, defaultValue) | ||
|
||
return defaultValue | ||
} | ||
|
||
Map.fromObject = function<V>(obj: Record<string, V>): Map<string, V> { | ||
return new Map(Object.entries(obj)) | ||
} | ||
|
||
Map.fromArray = function<V>(array: Array<V>, mapToKey: (value: V) => string): Map<string, V> { | ||
const map = new Map<string, V>() | ||
for (const value of array) { | ||
map.set(mapToKey(value), value) | ||
} | ||
|
||
return map | ||
} |
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,15 @@ | ||
import '../extensions/map-extensions.js' | ||
|
||
export function grouping<ElementType>( | ||
elements: ElementType[], | ||
mapper: (value: ElementType) => string, | ||
): Map<string, ElementType[]> { | ||
return elements.reduce((result, element) => { | ||
const key = mapper(element) | ||
result | ||
.getWithDefault(key, []) | ||
.push(element) | ||
|
||
return result | ||
}, new Map<string, ElementType[]>) | ||
} |