Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
♻️ Use a pure function when stripping ANSI codes
Browse files Browse the repository at this point in the history
  • Loading branch information
willclarktech committed Dec 11, 2017
1 parent 7326e64 commit 0c89e6d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
17 changes: 7 additions & 10 deletions src/utils/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,17 @@ import config from './config';
import { shouldUseJsonOutput, shouldUsePrettyOutput } from './helpers';
import tablify from './tablify';

const disableAnsi = (result) => {
// eslint-disable-next-line no-param-reassign
Object.keys(result).forEach((key) => { result[key] = stripAnsi(result[key]); });
return result;
};
const removeAnsi = result => Object.entries(result)
.reduce(
(strippedResult, [key, value]) =>
Object.assign({}, strippedResult, { [key]: stripAnsi(value) })
, {},
);

export const printResult = (vorpal, options = {}) => (result) => {
const useJsonOutput = shouldUseJsonOutput(config, options);
const prettifyOutput = shouldUsePrettyOutput(config, options);
let resultToPrint = result;

if (useJsonOutput) {
resultToPrint = disableAnsi(result);
}
const resultToPrint = useJsonOutput ? removeAnsi(result) : result;

const output = useJsonOutput
? JSON.stringify(resultToPrint, null, prettifyOutput ? '\t' : null)
Expand Down
16 changes: 8 additions & 8 deletions test/specs/utils/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ describe('print utils', () => {
When('the result is printed', when.theResultIsPrinted, () => {
Then('shouldUseJsonOutput should be called with the config and the options', then.shouldUseJsonOutputShouldBeCalledWithTheConfigAndTheOptions);
Then('shouldUsePrettyOutput should be called with the config and the options', then.shouldUsePrettyOutputShouldBeCalledWithTheConfigAndTheOptions);
Then('the result should be returned', then.theResultShouldBeReturned);
Then('JSON output should be logged', then.jSONOutputShouldBeLogged);
Then('the result stripped of ANSI codes should be returned', then.theResultStrippedOfANSICodesShouldBeReturned);
Then('JSON output should be logged without ANSI codes', then.jSONOutputShouldBeLoggedWithoutANSICodes);
});
});
Given('the options object has key "pretty" set to boolean true', given.theOptionsObjectHasKeySetToBoolean, () => {
Given('output should be pretty', given.outputShouldBePretty, () => {
When('the result is printed', when.theResultIsPrinted, () => {
Then('shouldUseJsonOutput should be called with the config and the options', then.shouldUseJsonOutputShouldBeCalledWithTheConfigAndTheOptions);
Then('shouldUsePrettyOutput should be called with the config and the options', then.shouldUsePrettyOutputShouldBeCalledWithTheConfigAndTheOptions);
Then('the result should be returned', then.theResultShouldBeReturned);
Then('pretty JSON output should be logged', then.prettyJSONOutputShouldBeLogged);
Then('the result stripped of ANSI codes should be returned', then.theResultStrippedOfANSICodesShouldBeReturned);
Then('pretty JSON output should be logged without ANSI codes', then.prettyJSONOutputShouldBeLoggedWithoutANSICodes);
});
});
});
Expand All @@ -65,8 +65,8 @@ describe('print utils', () => {
When('the result is printed', when.theResultIsPrinted, () => {
Then('shouldUseJsonOutput should be called with the config and the options', then.shouldUseJsonOutputShouldBeCalledWithTheConfigAndTheOptions);
Then('shouldUsePrettyOutput should be called with the config and the options', then.shouldUsePrettyOutputShouldBeCalledWithTheConfigAndTheOptions);
Then('the result should be returned', then.theResultShouldBeReturned);
Then('pretty JSON output should be logged', then.prettyJSONOutputShouldBeLogged);
Then('the result stripped of ANSI codes should be returned', then.theResultStrippedOfANSICodesShouldBeReturned);
Then('pretty JSON output should be logged without ANSI codes', then.prettyJSONOutputShouldBeLoggedWithoutANSICodes);
});
});
});
Expand All @@ -77,8 +77,8 @@ describe('print utils', () => {
When('the result is printed', when.theResultIsPrinted, () => {
Then('shouldUseJsonOutput should be called with the config and the options', then.shouldUseJsonOutputShouldBeCalledWithTheConfigAndTheOptions);
Then('shouldUsePrettyOutput should be called with the config and the options', then.shouldUsePrettyOutputShouldBeCalledWithTheConfigAndTheOptions);
Then('the result should be returned', then.theResultShouldBeReturned);
Then('JSON output should be logged', then.jSONOutputShouldBeLogged);
Then('the result stripped of ANSI codes should be returned', then.theResultStrippedOfANSICodesShouldBeReturned);
Then('JSON output should be logged without ANSI codes', then.jSONOutputShouldBeLoggedWithoutANSICodes);
});
});
});
Expand Down
3 changes: 2 additions & 1 deletion test/steps/printing/1_given.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
} from '../../../src/utils/helpers';

export function thereIsAResultToPrint() {
this.test.ctx.result = { lisk: 'JS' };
this.test.ctx.result = { lisk: 'Some prefix: \u001B[4mJS\u001B[0m' };
this.test.ctx.resultWithoutANSICodes = { lisk: 'Some prefix: JS' };
}

export function jsonShouldBePrinted() {
Expand Down
12 changes: 12 additions & 0 deletions test/steps/printing/3_then.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,24 @@ export function prettyJSONOutputShouldBeLogged() {
return (vorpal.activeCommand.log).should.be.calledWithExactly(prettyJsonOutput);
}

export function prettyJSONOutputShouldBeLoggedWithoutANSICodes() {
const { resultWithoutANSICodes, vorpal } = this.test.ctx;
const prettyJsonOutput = JSON.stringify(resultWithoutANSICodes, null, '\t');
return (vorpal.activeCommand.log).should.be.calledWithExactly(prettyJsonOutput);
}

export function jSONOutputShouldBeLogged() {
const { result, vorpal } = this.test.ctx;
const jsonOutput = JSON.stringify(result);
return (vorpal.activeCommand.log).should.be.calledWithExactly(jsonOutput);
}

export function jSONOutputShouldBeLoggedWithoutANSICodes() {
const { resultWithoutANSICodes, vorpal } = this.test.ctx;
const jsonOutput = JSON.stringify(resultWithoutANSICodes);
return (vorpal.activeCommand.log).should.be.calledWithExactly(jsonOutput);
}

export function shouldUseJsonOutputShouldBeCalledWithTheConfigAndAnEmptyOptionsObject() {
const { config } = this.test.ctx;
return (shouldUseJsonOutput).should.be.calledWithExactly(config, {});
Expand Down
12 changes: 12 additions & 0 deletions test/steps/queries/3_then.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Removal or modification of this copyright notice is prohibited.
*
*/
import stripAnsi from 'strip-ansi';
import {
getFirstQuotedString,
} from '../utils';
Expand Down Expand Up @@ -43,3 +44,14 @@ export function theResultShouldBeReturned() {
const { returnValue, result } = this.test.ctx;
return (returnValue).should.equal(result);
}

export function theResultStrippedOfANSICodesShouldBeReturned() {
const { returnValue, result } = this.test.ctx;
const resultWithoutANSICodes = Object.entries(result)
.reduce(
(strippedResult, [key, value]) =>
Object.assign({}, strippedResult, { [key]: stripAnsi(value) })
, {},
);
return (returnValue).should.eql(resultWithoutANSICodes);
}

0 comments on commit 0c89e6d

Please sign in to comment.