Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cli]: Consistent list logging (#4287)
* add consistent logger * improve log logic
- Loading branch information
1 parent
e645615
commit 823e1d5
Showing
4 changed files
with
104 additions
and
34 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,58 @@ | ||
// @flow | ||
import colors from 'colors'; | ||
|
||
import {listItem} from '../logger'; | ||
|
||
describe('logger', () => { | ||
const consoleLog = jest.fn(); | ||
jest.spyOn(console, 'log').mockImplementation(consoleLog); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('listItem', () => { | ||
it('logs single value', () => { | ||
listItem('test'); | ||
|
||
expect(consoleLog).toHaveBeenCalledWith(colors.bold(` • test`)); | ||
}); | ||
|
||
it('logs two values', () => { | ||
listItem('test', 'a'); | ||
|
||
expect(consoleLog).toHaveBeenCalledWith( | ||
colors.bold(` • test | ||
└> a`), | ||
); | ||
}); | ||
|
||
it('logs multiple values', () => { | ||
listItem('test', 'a', 'b'); | ||
|
||
expect(consoleLog).toHaveBeenCalledWith( | ||
colors.bold(` • test | ||
└> a | ||
b`), | ||
); | ||
}); | ||
|
||
it('ignores undefined values', () => { | ||
listItem(undefined, 'a', 'b'); | ||
|
||
expect(consoleLog).toHaveBeenCalledWith( | ||
colors.bold(` • a | ||
└> b`), | ||
); | ||
}); | ||
|
||
it('ignores empty strings', () => { | ||
listItem('test', '', 'b'); | ||
|
||
expect(consoleLog).toHaveBeenCalledWith( | ||
colors.bold(` • test | ||
└> b`), | ||
); | ||
}); | ||
}); | ||
}); |
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,32 @@ | ||
// @flow | ||
import colors from 'colors'; | ||
|
||
const bulletPoint = ` • `; | ||
const arrow = ` └> `; | ||
const genericRowPad = ` `; | ||
|
||
const shouldNewLine = (condition: boolean) => (condition ? '\n' : ''); | ||
|
||
const createListItem = (values: Array<string>): string => { | ||
const [first, second, ...rest] = values; | ||
let value = `${bulletPoint}${first}${shouldNewLine(!!second)}`; | ||
if (second) { | ||
value += `${arrow}${second}${shouldNewLine(rest.length > 0)}`; | ||
} | ||
if (rest.length > 0) { | ||
rest.forEach((r, i) => { | ||
value += `${genericRowPad}${r}${shouldNewLine(i !== rest.length - 1)}`; | ||
}); | ||
} | ||
return colors.bold(value); | ||
}; | ||
|
||
export const listItem = (...values: Array<string | void>): void => { | ||
const validItems: Array<string> = values | ||
.map(o => { | ||
if (!o) return ''; | ||
return o; | ||
}) | ||
.filter(o => !!o); | ||
console.log(createListItem(validItems)); | ||
}; |
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