Skip to content

Commit

Permalink
refactor input box validator
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed Jan 4, 2023
1 parent f5e928d commit 89e1efa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
13 changes: 6 additions & 7 deletions extension/src/util/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ export const formatNumber = (value: number): string => {
return automatic
}

export const isValidStringInteger = (
input: string | undefined
): input is string =>
!!input && Number.parseInt(input) === Number.parseFloat(input)

export const createValidInteger = (
input: string | number | undefined
): number | undefined => {
if (!input) {
return
}

if (typeof input === 'number') {
return validateNumericInteger(input)
}

return Number.parseInt(input) === Number.parseFloat(input)
? Number.parseInt(input)
: undefined
return isValidStringInteger(input) ? Number.parseInt(input) : undefined
}
9 changes: 4 additions & 5 deletions extension/src/vscode/inputBox.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { window } from 'vscode'
import { Title } from './title'
import { isValidStringInteger } from '../util/number'

export const getInput = (title: Title, value?: string) =>
window.showInputBox({
Expand All @@ -26,13 +27,11 @@ export const getPositiveIntegerInput = async (
const input = await getValidInput(
title,
val => {
const number = Number(val)

if (!Number.isInteger(number) || number <= 0) {
return 'Input needs to be a positive integer'
if (isValidStringInteger(val) && Number(val) > 0) {
return ''
}

return ''
return 'Input needs to be a positive integer'
},
options
)
Expand Down

0 comments on commit 89e1efa

Please sign in to comment.