From aa9d1c7555b9ecea43987d03dbac2b66c58fb974 Mon Sep 17 00:00:00 2001 From: Jakob Helgesson Date: Sun, 2 Jan 2022 23:21:46 +0100 Subject: [PATCH] Update return type to throw Error instead of return null (#9) --- src/hex2dec.ts | 4 +--- src/index.ts | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/hex2dec.ts b/src/hex2dec.ts index 6f6fc24..8931b76 100644 --- a/src/hex2dec.ts +++ b/src/hex2dec.ts @@ -46,7 +46,7 @@ const parseToDigitsArray = (str: string, base: number) => { for (let i = digits.length - 1; i >= 0; i--) { let n = parseInt(digits[i], base); - if (isNaN(n)) return null; + if (isNaN(n)) throw new Error(`'${digits[i]}' is not a valid digit in base ${base}`); ary.push(n); } @@ -57,8 +57,6 @@ const parseToDigitsArray = (str: string, base: number) => { const convertBase = (str: string, fromBase: number, toBase: number) => { let digits = parseToDigitsArray(str, fromBase); - if (digits === null) return null; - let outArray: number[] = []; let power = [1]; for (let i = 0; i < digits.length; i++) { diff --git a/src/index.ts b/src/index.ts index ba32ba2..c8fadda 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,7 +20,7 @@ export type SunflakeConfig = { epoch?: number; }; -export const generateSunflake = (config: SunflakeConfig) => async (time: number = Date.now()) => { +export const generateSunflake = (config: SunflakeConfig) => async (time: number = Date.now()): Promise => { let { machineID = 1, epoch = 1640988001000 } = config; lastTime = time;