-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Emmanuel-Develops/validate-inputs
feat: validate frequency and amount inputs
- Loading branch information
Showing
3 changed files
with
44 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const ErrorConfig = { | ||
"POSITIVE_INTEGER": "must be a positive interger" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { input } from '@inquirer/prompts'; | ||
import { ErrorConfig } from './err_config.js'; | ||
|
||
const getFrequency = async () => { | ||
const errMessage = `Frequency ${ErrorConfig.POSITIVE_INTEGER}` | ||
try { | ||
const freq = await input({ message: 'At what time would you like to run this action?', default: 0 }); | ||
if (isNaN(parseInt(freq)) || parseInt(freq) < 1) { | ||
throw new Error(errMessage) | ||
} | ||
return parseInt(freq) | ||
} catch (err) { | ||
if (err.message === errMessage) { | ||
console.error(err.message) | ||
return await getFrequency() | ||
} | ||
} | ||
} | ||
const getAmountInSats = async () => { | ||
const errMessage = `Amount ${ErrorConfig.POSITIVE_INTEGER}` | ||
try { | ||
const amt = await input({ message: 'How many sats?', default: 1000 }); | ||
if (isNaN(parseInt(amt)) || parseInt(amt) < 1) { | ||
throw new Error(errMessage) | ||
} | ||
return parseInt(amt) | ||
} catch (err) { | ||
console.error(err.message) | ||
if (err.message === errMessage) { | ||
return await getAmountInSats() | ||
} | ||
} | ||
} | ||
|
||
|
||
export { getFrequency, getAmountInSats } |