Created
March 22, 2018 12:10
-
-
Save eps1lon/a1a40465929905d3c70674cab331614f to your computer and use it in GitHub Desktop.
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
import { Omit } from 'utility-types'; | |
import { BaseItemTypeProps, EssenceProps, ModProps } from '../schema'; | |
/** | |
* *_mod_key is keyof mods/essenceMods.json | |
*/ | |
export interface NormalizedEssenceProps { | |
primary: number; | |
tier: number; | |
base_item_type: BaseItemTypeProps; | |
essence_type: { | |
id: string; | |
essence_type: number; | |
is_corrupted_essence: boolean; | |
}; | |
is_screaming: boolean; | |
item_level_restriction: number; | |
quiver_mods_key?: number; | |
amulet_mods_key?: number; | |
ring_mods_key?: number; | |
belt_mods_key?: number; | |
shield_mods_key?: number; | |
helmet_mods_key?: number; | |
body_armour_mods_key?: number; | |
boots_mods_key?: number; | |
gloves_mods_key?: number; | |
bow_mods_key?: number; | |
wand_mods_key?: number; | |
staff_mods_key?: number; | |
two_hand_sword_mods_key?: number; | |
two_hand_axe_mods_key?: number; | |
two_hand_mace_mods_key?: number; | |
claw_mods_key?: number; | |
dagger_mods_key?: number; | |
one_hand_sword_mods_key?: number; | |
one_hand_thrusting_sword_mods_key?: number; | |
one_hand_axe_mods_key?: number; | |
one_hand_mace_mods_key?: number; | |
sceptre_mods_key?: number; | |
} | |
export default function denormalizeEssences( | |
denormalized: NormalizedEssenceProps[], | |
mods: { [key: number]: ModProps }, | |
): EssenceProps[] { | |
const mapping = { | |
gloves_mods_key: 'gloves_mod', | |
helmet_mods_key: 'helmet_mod', | |
boots_mods_key: 'boots_mod', | |
shield_mods_key: 'shield_mod', | |
body_armour_mods_key: 'body_armour_mod', | |
amulet_mods_key: 'amulet_mod', | |
ring_mods_key: 'ring_mod', | |
belt_mods_key: 'belt_mod', | |
quiver_mods_key: 'quiver_mod', | |
one_hand_axe_mods_key: 'one_hand_axe_mod', | |
one_hand_mace_mods_key: 'one_hand_mace_mod', | |
one_hand_sword_mods_key: 'one_hand_sword_mod', | |
one_hand_thrusting_sword_mods_key: 'one_hand_thrusting_sword_mod', | |
dagger_mods_key: 'dagger_mod', | |
claw_mods_key: 'claw_mod', | |
wand_mods_key: 'wand_mod', | |
sceptre_mods_key: 'sceptre_mod', | |
two_hand_axe_mods_key: 'two_hand_axe_mod', | |
two_hand_mace_mods_key: 'two_hand_mace_mod', | |
two_hand_sword_mods_key: 'two_hand_sword_mod', | |
staff_mods_key: 'staff_mod', | |
bow_mods_key: 'bow_mod', | |
}; | |
return denormalized.map(essence => { | |
return denormalize( | |
essence, | |
mapping, | |
(key: number | undefined) => mods[key!], | |
); | |
}); | |
} | |
type MappedRenamedProps< | |
T extends Object, | |
K1 extends keyof T, | |
K2 extends string, | |
V | |
> = { [K in K2]: V } & Omit<T, K1>; // Omit<T, K1> | |
function denormalize< | |
T extends Object, | |
K1 extends keyof T, | |
K2 extends string, | |
V | |
>( | |
props: T, | |
mapping: { [K in K1]: K2 }, | |
get: (props: T[K1]) => V, | |
): MappedRenamedProps<T, K1, K2, V> { | |
return Object.entries( | |
mapping, | |
// this should be [K1, K2] | |
).reduce((obj, [key_prop, mapped_prop]: any) => { | |
// @ts-ignore: rest spread only for objects | |
const { [key_prop]: key, ...rest } = obj; | |
return { | |
...rest, | |
[mapped_prop]: get(key), | |
}; | |
}, props); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment