1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import {Index} from './index';
|
17 |
|
18 | export function normalizeArray<T, K extends keyof T>(arr: Array<T>, key: K): Index<T> {
|
19 | if (!Array.isArray(arr)) throw new Error(`${arr} is not an array`);
|
20 | return arr.reduce((acc, object) => {
|
21 | const keyValue = String(object[key]);
|
22 | return {
|
23 | ...acc,
|
24 | [keyValue]: object,
|
25 | };
|
26 | }, {});
|
27 | }
|
28 |
|
29 | export function denormalize<T>(index: Index<T>): Array<[string, T]> {
|
30 | return Object.keys(index).map(k => [k, index[k]] as [string, T]);
|
31 | } |
\ | No newline at end of file |