Skip to content

Instantly share code, notes, and snippets.

@FrameMuse
Created December 1, 2022 09:38
Show Gist options
  • Save FrameMuse/05671aeae822d3456afe597f70ac501b to your computer and use it in GitHub Desktop.
Save FrameMuse/05671aeae822d3456afe597f70ac501b to your computer and use it in GitHub Desktop.
Simple bidirectional (two ways) map class
/**
* This class represent keys and values mapping (swapping).
*
* ### Research
* - https://www.google.com/search?q=bidirectional+map+js&oq=bidirectional+map+js&aqs=chrome..69i57.2532j0j7&sourceid=chrome&ie=UTF-8
* - https://www.google.com/search?q=bilateral+mapping+npm
* - https://startfunction.com/2020/11/26/bidirectional-map-javascript/#initialize
* - https://startfunction.com/bidirectional-map-javascript/
* - https://www.npmjs.com/package/bi-directional-map
*/
class BiMap<A1 extends keyof never, A2 extends keyof never> {
private forwardMap: Record<A1, A2>
private backwardMap: Record<A2, A1>
constructor(mapping: Record<A1, A2>) {
const mappingKeys = Object.keys(mapping) as A1[]
this.forwardMap = { ...mapping }
this.backwardMap = mappingKeys.reduce((result, key) => ({ ...result, [mapping[key]]: key }), {} as Record<A2, A1>)
}
public mapForward(key: A1): A2 {
return this.forwardMap[key]
}
public mapBackward(key: A2): A1 {
return this.backwardMap[key]
}
}
export default BiMap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment