Skip to content

Commit

Permalink
type tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Sep 27, 2023
1 parent e9140c2 commit 246e26d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
15 changes: 11 additions & 4 deletions src/async.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import { coinTypeMap } from "./consts/coinTypeMap.js";
import type {
Coin,
CoinName,
CoinType,
CoinTypeInvertedReference,
Formats,
} from "./types.js";

export const getCoderByCoinNameAsync = async <TCoinName extends CoinName>(
export const getCoderByCoinNameAsync = async <
TCoinName extends CoinName | string = string
>(
name: TCoinName
): Promise<Formats[TCoinName]> => {
): Promise<TCoinName extends CoinName ? Formats[TCoinName] : Coin> => {
const mod = await import(`./coin/${name}`);
if (!mod) {
throw new Error(`Unsupported coin: ${name}`);
}
return mod[name];
};

export const getCoderByCoinTypeAsync = async <TCoinType extends CoinType>(
export const getCoderByCoinTypeAsync = async <
TCoinType extends CoinType | number = number
>(
coinType: TCoinType
): Promise<CoinTypeInvertedReference[TCoinType]> => {
): Promise<
TCoinType extends CoinType ? CoinTypeInvertedReference[TCoinType] : Coin
> => {
const name = coinTypeMap[String(coinType) as keyof typeof coinTypeMap];
if (!name) {
throw new Error(`Unsupported coin type: ${coinType}`);
Expand Down
35 changes: 27 additions & 8 deletions src/index.ts
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;
};

0 comments on commit 246e26d

Please sign in to comment.