Skip to content

Commit

Permalink
feat: min/max for integer flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jul 20, 2022
1 parent 18e77f7 commit 7e05ef7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/parser/flags.ts
Expand Up @@ -35,13 +35,21 @@ export function boolean<T = boolean>(
} as BooleanFlag<T>
}

export const integer = build({
parse: async input => {
if (!/^-?\d+$/.test(input))
throw new Error(`Expected an integer but received: ${input}`)
return Number.parseInt(input, 10)
},
})
export const integer = (opts: { min?: number; max?: number } & Partial<OptionFlag<number>> = {}): OptionFlag<number | undefined> => {
return build<number>({
...opts,
parse: async input => {
if (!/^-?\d+$/.test(input))
throw new Error(`Expected an integer but received: ${input}`)
const num = Number.parseInt(input, 10)
if (typeof opts.min === 'number' && Number(input) < opts.min)
throw new Error(`Expected an integer greater than or equal to ${opts.min} but received: ${input}`)
if (typeof opts.max === 'number' && Number(input) > opts.max)
throw new Error(`Expected an integer less than or equal to ${opts.max} but received: ${input}`)
return num
},
})()
}

export const directory = (opts: { exists?: boolean } & Partial<OptionFlag<string>> = {}): OptionFlag<string | undefined> => {
return build<string>({
Expand Down
52 changes: 52 additions & 0 deletions test/parser/parse.test.ts
Expand Up @@ -428,6 +428,58 @@ See more help with --help`)

expect(message).to.equal('Expected an integer but received: s10')
})

describe('min/max', () => {
it('min pass equal', async () => {
const out = await parse(['--int', '10'], {
flags: {int: flags.integer({min: 10, max: 20})},
})
expect(out.flags).to.deep.include({int: 10})
})
it('min pass gt', async () => {
const out = await parse(['--int', '11'], {
flags: {int: flags.integer({min: 10, max: 20})},
})
expect(out.flags).to.deep.include({int: 11})
})
it('max pass lt', async () => {
const out = await parse(['--int', '19'], {
flags: {int: flags.integer({min: 10, max: 20})},
})
expect(out.flags).to.deep.include({int: 19})
})
it('max pass equal', async () => {
const out = await parse(['--int', '20'], {
flags: {int: flags.integer({min: 10, max: 20})},
})
expect(out.flags).to.deep.include({int: 20})
})

it('min fail lt', async () => {
let message = ''
try {
await parse(['--int', '9'], {
flags: {int: flags.integer({min: 10, max: 20})},
})
} catch (error: any) {
message = error.message
}

expect(message).to.equal('Expected an integer greater than or equal to 10 but received: 9')
})
it('max fail gt', async () => {
let message = ''
try {
await parse(['--int', '21'], {
flags: {int: flags.integer({min: 10, max: 20})},
})
} catch (error: any) {
message = error.message
}

expect(message).to.equal('Expected an integer less than or equal to 20 but received: 21')
})
})
})
})

Expand Down

0 comments on commit 7e05ef7

Please sign in to comment.