From 9884151820afa2af2068d710eefa19434578a814 Mon Sep 17 00:00:00 2001 From: Berkmann18 Date: Thu, 1 Nov 2018 17:22:20 +0000 Subject: [PATCH] New: Added `use` Added the possibility to colourise data without it being logged and thus allowing nested colouring re #4 --- index.js | 15 ++++++++++++++- package.json | 4 ++-- test/test.js | 41 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index e78587f2..af699e63 100644 --- a/index.js +++ b/index.js @@ -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 @@ -111,4 +124,4 @@ const extend = (extension) => { clr.setTheme(theme); }; -module.exports = { error, info, dbg, out, inp, warn, quest, log, extend } \ No newline at end of file +module.exports = { error, info, dbg, out, inp, warn, quest, log, extend, use } \ No newline at end of file diff --git a/package.json b/package.json index 2f742423..f29cfa4c 100644 --- a/package.json +++ b/package.json @@ -85,8 +85,8 @@ "lint-staged": { "*.js": [ "eslint -c ./config/.eslintrc.js --fix", - "mocha --exit", - "git add" + "git add", + "jest --bail --findRelatedTests" ], "*.md": [ "remark", diff --git a/test/test.js b/test/test.js index 5c06f83b..59d972fa 100644 --- a/test/test.js +++ b/test/test.js @@ -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'); @@ -74,7 +74,7 @@ test('extend', () => { extend({ suc: 'magenta' }); - + nclr.suc(text); expect('suc' in nclr).toBeTruthy(); expect(typeof nclr.suc).toStrictEqual('function'); @@ -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({ @@ -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]); @@ -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]);