Skip to content

Commit

Permalink
New: Added use
Browse files Browse the repository at this point in the history
Added the possibility to colourise data without it being logged and thus allowing nested colouring

re #4
  • Loading branch information
Berkmann18 committed Nov 1, 2018
1 parent 8f7f245 commit 9884151
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ clr.setTheme(theme);
*/
const log = (...data) => process.stdout.write(...data);

const THEME = Object.keys(theme);

/**
* @description Colourise something.
* @param {string} name Name of the colour in the theme
* @param {...*} data Data
* @return {*} Coloured output
*/
const use = (name, ...data) => {
if (THEME.includes(name)) return clr[name](data.join(' '));
else throw new Error(`The name ${name} isn't specified in the theme used`);
};

/**
* @description Print an error.
* @param {...*} data Data to print
Expand Down Expand Up @@ -111,4 +124,4 @@ const extend = (extension) => {
clr.setTheme(theme);
};

module.exports = { error, info, dbg, out, inp, warn, quest, log, extend }
module.exports = { error, info, dbg, out, inp, warn, quest, log, extend, use }
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
"lint-staged": {
"*.js": [
"eslint -c ./config/.eslintrc.js --fix",
"mocha --exit",
"git add"
"git add",
"jest --bail --findRelatedTests"
],
"*.md": [
"remark",
Expand Down
41 changes: 37 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const nclr = require('../index');
const { info, dbg, out, inp, warn, quest, error, log, extend } = nclr;
const { info, dbg, out, inp, warn, quest, error, log, extend, use } = nclr;
const stdout = require('test-console').stdout;

const clr = require('colors/safe');
Expand Down Expand Up @@ -74,7 +74,7 @@ test('extend', () => {
extend({
suc: 'magenta'
});

nclr.suc(text);
expect('suc' in nclr).toBeTruthy();
expect(typeof nclr.suc).toStrictEqual('function');
Expand All @@ -83,6 +83,39 @@ test('extend', () => {
expect(res).toStrictEqual([`\u001b[35m${text}${END}\n`]);
});

test('use', () => {
let result = `\u001b[32m${text}${END}`;
const output = stdout.inspectSync(() => process.stdout.write(use('info', text)));
expect(output).toStrictEqual([result]);
const res = stdout.inspectSync(() => log(use('info', text)));
expect(res).toStrictEqual([result]);
expect(use('info', text)).toStrictEqual(result);
});

test('nested use()', () => {
let result = `\u001b[32m${text} \u001b[31mError\u001b[32m${END}`;
const output = stdout.inspectSync(() => process.stdout.write(use('info', text, use('error', 'Error'))));
expect(output).toStrictEqual([result]);
});

test('info and use', () => {
let result = `\u001b[32m${text} \u001b[31mError\u001b[32m${END}`;
const output = stdout.inspectSync(() => info(text, use('error', 'Error')));
expect(output).toStrictEqual([`${result}\n`]);
});

test('info and use(use)', () => {
let result = `\u001b[32m${text} \u001b[33mMy \u001b[31mdear\u001b[33m\u001b[32m${END}`;
const output = stdout.inspectSync(() => info(text, use('warn', 'My', use('error', 'dear'))));
expect(output).toStrictEqual([`${result}\n`]);
});

test('info and use(`${use}`)', () => {
let result = `\u001b[32m${text} \u001b[33mMy\u001b[31mDear\u001b[33m\u001b[32m${END}`;
const output = stdout.inspectSync(() => info(text, use('warn', `My${use('error', 'Dear')}`)));
expect(output).toStrictEqual([`${result}\n`]);
});

test('Simple overriding with extend()...', () => {
expect('info' in clr).toBeTruthy();
extend({
Expand All @@ -92,7 +125,7 @@ test('Simple overriding with extend()...', () => {
expect('info' in clr).toBeTruthy();
const initialInfo = `\u001b[32m${text}${END}`,
overridenInfo = `\u001b[35m${text}${END}`;

const outInfo = stdout.inspectSync(() => process.stdout.write(clr.info(text)));
expect(outInfo).not.toStrictEqual([initialInfo]);
expect(outInfo).toStrictEqual([overridenInfo]);
Expand All @@ -113,7 +146,7 @@ test('Overriding with extend()', () => {
expect('warn' in clr).toBeTruthy();
const initialWarn = `\u001b[33m${text}${END}`,
overrideWarn = `\u001b[4m\u001b[33m${text}${END}\u001b[24m`;

const outWarn = stdout.inspectSync(() => process.stdout.write(clr.warn(text)));
expect(outWarn).not.toStrictEqual([initialWarn]);
expect(outWarn).toStrictEqual([overrideWarn]);
Expand Down

0 comments on commit 9884151

Please sign in to comment.