-
Notifications
You must be signed in to change notification settings - Fork 128
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
2 changed files
with
38 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,51 @@ | ||
import * as formats from "./coins.js"; | ||
import { coinTypeMap } from "./consts/coinTypeMap.js"; | ||
import type { | ||
Coin, | ||
CoinName, | ||
CoinType, | ||
CoinTypeInvertedReference, | ||
DecoderFunction, | ||
EncoderFunction, | ||
Formats, | ||
} from "./types.js"; | ||
|
||
export const getCoderByCoinName = <TCoinName extends CoinName>( | ||
export type { | ||
Coin, | ||
CoinName, | ||
CoinType, | ||
CoinTypeInvertedReference, | ||
DecoderFunction, | ||
EncoderFunction, | ||
Formats, | ||
}; | ||
|
||
export const getCoderByCoinName = < | ||
TCoinName extends CoinName | string = string | ||
>( | ||
name: TCoinName | ||
): Formats[TCoinName] => { | ||
const format = formats[name]; | ||
): TCoinName extends CoinName ? Formats[TCoinName] : Coin => { | ||
const format = formats[name as keyof typeof formats]; | ||
if (!format) { | ||
throw new Error(`Unsupported coin: ${name}`); | ||
} | ||
return format; | ||
return format as TCoinName extends CoinName ? Formats[TCoinName] : Coin; | ||
}; | ||
|
||
export const getCoderByCoinType = <TCoinType extends CoinType>( | ||
export const getCoderByCoinType = < | ||
TCoinType extends CoinType | number = number | ||
>( | ||
coinType: TCoinType | ||
): CoinTypeInvertedReference[TCoinType] => { | ||
): TCoinType extends CoinType ? CoinTypeInvertedReference[TCoinType] : Coin => { | ||
const name = coinTypeMap[String(coinType) as keyof typeof coinTypeMap]; | ||
if (!name) { | ||
throw new Error(`Unsupported coin type: ${coinType}`); | ||
} | ||
const format = formats[name] as CoinTypeInvertedReference[TCoinType]; | ||
const format = formats[name]; | ||
if (!format) { | ||
throw new Error(`Unsupported coin type: ${coinType}`); | ||
} | ||
return format; | ||
return format as TCoinType extends CoinType | ||
? CoinTypeInvertedReference[TCoinType] | ||
: Coin; | ||
}; |