-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
symbols.js
128 lines (116 loc) · 3.62 KB
/
symbols.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const clr = require('colors/safe'),
fig = require('figures');
let theme = require('./src/theme'),
{ log, use, isValidName, updateTheme, restoreTheme } = require('./src/lib');
/**
* @fileoverview Set of functions for coloured logs with symbols.
* @module
*/
/**
* @description Print an error.
* @param {...*} data Data to print
* @example error('Something wrong happened with', new Error(this));
* @see log
* @returns {boolean} Did it happened?
*/
const error = (...data) => log(`${clr.error(fig.cross + ' ' + data.join(' '))}\n`);
/**
* @description Print an information.
* @param {...*} data Data to print
* @example info('Welcome John');
* @see log
* @returns {boolean} Did it happened?
*/
const info = (...data) => log(`${clr.info(fig.info + ' ' + data.join(' '))}\n`);
/**
* @description Print a debug message.
* @param {...*} data Data to print
* @example dbg('i=', i);
* @see log
* @returns {boolean} Did it happened?
*/
const dbg = (...data) => log(clr.dbg(data.join(' ')) + '\n');
/**
* @description Print an output.
* @param {...*} data Data to print
* @example out(`1 + 1 = ${rpc('1 1 +')}`);
* @see log
* @returns {boolean} Did it happened?
*/
const out = (...data) => log(clr.out(data.join(' ')) + '\n');
/**
* @description Print an input.
* @param {...*} data Data to print
* @example inp(name);
* @see log
* @returns {boolean} Did it happened?
*/
const inp = (...data) => log(clr.inp(data.join(' ')) + '\n');
/**
* @description Print a warning.
* @param {...*} data Data to print
* @example warn('The following function is deprecated');
* @see log
* @returns {boolean} Did it happened?
*/
const warn = (...data) => log(`${clr.warn(fig.warning + ' ' + data.join(' '))}\n`);
/**
* @description Print a question.
* @param {...*} data Data to print
* @example quest('What is your username?');
* @see log
* @returns {boolean} Did it happened?
*/
const quest = (...data) => log(clr.quest(fig.questionMarkPrefix + ' ' + data.join(' ')) + '\n');
/**
* @description Print a success.
* @param {...*} data Data to print
* @example succ('Achievement unlocked');
* @see log
* @returns {boolean} Did it happened?
*/
const succ = (...data) => log(clr.succ(fig.tick + ' ' + data.join(' ')) + '\n');
/**
* @description Extend the current theme.
* @param {{string: (string|string[])}} extension Theme to add
* @example <caption>Using extensions as methods:</caption>
* const nclr = require('nclr');
* nclr.extend({
* suc: {
* styles: ['green', 'underline'],
* symbol: 'check'
* }
* data: {
* styles: 'magenta',
* symbol: 'pointer',
* }
* });
* nclr.suc('Yay!');
* nclr.data(42);
* @example <caption>Using extensions as functions:</caption>
* const nclr = require('nclr');
* nclr.extend({
* suc: {
* styles: ['green', 'underline'],
* symbol: 'check'
* }
* data: {
* styles: 'magenta',
* symbol: 'pointer'
* });
* const { suc, data } = nclr;
* suc('Yay!');
* data(42);
* @throws {Error} Invalid extension key
* @throws {Error} No <code>styles</code> or <code>symbol</code> property found
*/
const extend = (extension) => {
for (let key in extension) {
if (!isValidName(key)) throw new Error(`Invalid extension key "${key}"`);
if (extension[key].styles === undefined || extension[key].symbol === undefined) throw new Error(`No 'styles' or 'symbol' property found for "${key}"`);
theme[key] = extension[key].styles;
module.exports[key] = (...data) => log(clr[key](fig[extension[key].symbol] + ' ' + data.join(' ')) + '\n');
}
updateTheme();
};
module.exports = { error, info, dbg, out, inp, warn, quest, succ, log, extend, use, restore: restoreTheme }