Skip to content

Commit

Permalink
fix: allow empty ux.stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed May 24, 2024
1 parent dc85bae commit da1e4cb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
20 changes: 14 additions & 6 deletions src/ux/write.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import {format} from 'node:util'

export const stdout = (str: string | string[] | undefined, ...args: string[]): void => {
if (typeof str === 'string' || !str) {
process.stdout.write(format(str, ...args) + '\n')
export const stdout = (str?: string | string[] | undefined, ...args: string[]): void => {
if (!str && args) {
process.stdout.write(format(...args) + '\n')
} else if (!str) {
process.stdout.write('\n')
} else if (typeof str === 'string') {
process.stdout.write((str && format(str, ...args)) + '\n')
} else {
process.stdout.write(format(...str, ...args) + '\n')
}
}

export const stderr = (str: string | string[] | undefined, ...args: string[]): void => {
if (typeof str === 'string' || !str) {
process.stderr.write(format(str, ...args) + '\n')
export const stderr = (str?: string | string[] | undefined, ...args: string[]): void => {
if (!str && args) {
process.stderr.write(format(...args) + '\n')
} else if (!str) {
process.stderr.write('\n')
} else if (typeof str === 'string') {
process.stderr.write((str && format(str, ...args)) + '\n')
} else {
process.stderr.write(format(...str, ...args) + '\n')
}
Expand Down
18 changes: 14 additions & 4 deletions test/ux/write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ describe('write', () => {
expect(stdout).to.equal('foo bar\n')
})

it('should format undefined input', async () => {
it('should handle undefined input', async () => {
const {stdout} = await captureOutput(async () => writeStdout(undefined, 'bar'))
expect(stdout).to.equal('undefined bar\n')
expect(stdout).to.equal('bar\n')
})

it('should write a new line with no input', async () => {
const {stdout} = await captureOutput(async () => writeStdout())
expect(stdout).to.equal('\n')
})
})

Expand All @@ -52,9 +57,14 @@ describe('write', () => {
expect(stderr).to.equal('foo bar\n')
})

it('should format undefined input', async () => {
it('should handle undefined input', async () => {
const {stderr} = await captureOutput(async () => writeStderr(undefined, 'bar'))
expect(stderr).to.equal('undefined bar\n')
expect(stderr).to.equal('bar\n')
})

it('should write a new line with no input', async () => {
const {stderr} = await captureOutput(async () => writeStderr())
expect(stderr).to.equal('\n')
})
})
})

0 comments on commit da1e4cb

Please sign in to comment.