-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
461 lines (382 loc) · 19.6 KB
/
index.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
'use strict';
/* SQL-92 standard data types and keywords
* http://www.frontbase.com/docs/5.3.html
*/
const sql92 = {
defaultDataTypes: ['SMALLINT', 'INTEGER', 'INT', 'NUMERIC', 'DECIMAL', 'DEC', 'FLOAT', 'REAL', 'DOUBLE PRECISION', 'CHARACTER', 'CHAR', 'NCHAR', 'VARCHAR', 'BIT', 'DATE', 'TIME', 'TIMESTAMP', 'INTERVAL', 'NATIONAL', 'VARYING', 'TIME ZONE']
};
/* Oracle data types
* https://docs.oracle.com/en/database/oracle/oracle-database/18/sqlrf/Data-Types.html#GUID-7B72E154-677A-4342-A1EA-C74C1EA928E6
*/
const oracle = {
defaultDataTypes: ['VARCHAR2', 'NVARCHAR2', 'NUMBER', 'FLOAT', 'LONG', 'DATE', 'BINARY_FLOAT', 'BINARY_DOUBLE', 'TIMESTAMP', 'INTERVAL', 'RAW', 'ROWID', 'UROWID', 'CHAR', 'NCHAR', 'CLOB', 'NCLOB', 'BLOB', 'BFILE', 'BYTE', 'LOCAL', 'TIME ZONE']
};
/* T-SQL data types and keywords
* https://docs.microsoft.com/en-us/sql/t-sql/data-types/data-types-transact-sql?view=sql-server-2017
*/
const tsql = {
defaultDataTypes: ['BIGINT', 'NUMERIC', 'BIT', 'SMALLINT', 'DECIMAL', 'SMALLMONEY', 'INT', 'TINYINT', 'MONEY', 'FLOAT', 'REAL', 'DATE', 'DATETIMEOFFSET', 'DATETIME2', 'SMALLDATETIME', 'DATETIME', 'TIME', 'CHAR', 'VARCHAR', 'TEXT', 'NCHAR', 'NVARCHAR', 'NTEXT', 'BINARY', 'VARBINARY', 'IMAGE', 'GEOMETRY', 'GEOGRAPHY', 'UNIQUEIDENTIFIER', 'XML']
};
/* PostgreSQL data types and keywords
* https://www.postgresql.org/docs/10/static/datatype.html
*/
const postgresql = {
defaultDataTypes: ['SMALLINT', 'INTEGER', 'BIGINT', 'DECIMAL', 'NUMERIC', 'REAL', 'DOUBLE PRECISION', 'SMALLSERIAL', 'SERIAL', 'BIGSERIAL', 'MONEY', 'CHAR', 'CHARACTER', 'VARCHAR', 'TEXT', 'BYTEA', 'TIMESTAMP', 'TIMESTAMPTZ', 'DATE', 'TIME', 'INTERVAL', 'BOOLEAN', 'ENUM', 'POINT', 'LINE', 'LSEG', 'BOX', 'PATH', 'POLYGON', 'CIRCLE', 'CIDR', 'INET', 'MACADDR', 'MACADDR8', 'BIT', 'UUID', 'XML', 'JSON', 'JSONB', 'TSQUERY', 'TSVECTOR', 'INT4RANGE', 'INT8RANGE', 'NUMRANGE', 'TSRANGE', 'TSTZRANGE', 'DATERANGE', 'ARRAY', 'TIME ZONE']
};
/* MariaDB data types and keywords
* https://mariadb.com/kb/en/library/data-types
*/
const mariadb = {
defaultDataTypes: ['TINYINT', 'BOOLEAN', 'SMALLINT', 'MEDIUMINT', 'INT', 'INTEGER', 'BIGINT', 'DECIMAL', 'DEC', 'NUMERIC', 'FIXED', 'FLOAT', 'DOUBLE', 'DOUBLE PRECISION', 'REAL', 'BIT', 'CHAR', 'VARCHAR', 'BINARY', 'CHAR BYTE', 'VARBINARY', 'TINYBLOB', 'BLOB', 'MEDIUMBLOB', 'LONGBLOB', 'TINYTEXT', 'TEXT', 'MEDIUMTEXT', 'LONGTEXT', 'JSON', 'ENUM', 'SET', 'ROW', 'DATE', 'TIME', 'DATETIME', 'TIMESTAMP', 'YEAR', 'POINT', 'LINESTRING', 'POLYGON', 'MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION', 'GEOMETRY']
};
/**
* Creates an array of unique values
* @param {...Array} [arrays]
* @returns {Array}
*/
/* Incompatible with current node version
* Bump version on 2.0 release
*
* function union(...arrays) {
* return [...new Set([].concat(...arrays))];
* }
*/
function union() {
let array = [];
for (let arg of arguments)
for (let item of arg)
if (!~array.indexOf(item))
array.push(item);
return array;
}
/**
* Moves all compound keywords to the front of the array
* @param {string[]} array - Array of keywords to mutate
* @returns {string[]}
*/
function unshiftCompoundKeywords(array) {
for (let i = array.length - 1; i >= 0; i--) {
const word = array[i];
if (/\w+\s\w+/.test(word))
array.unshift(array.splice(i, 1)[0]);
}
return array;
}
const defaultDataTypes = unshiftCompoundKeywords(union(sql92.defaultDataTypes, tsql.defaultDataTypes));
const defaultStandardKeywords = ['ACTION', 'ADD', 'AFTER', 'ALTER', 'AUTHORIZATION', 'BEFORE', 'BEGIN', 'BREAK', 'BY', 'CASCADE', 'CASE', 'CHECK', 'CHECKPOINT', 'CLOSE', 'COLUMN', 'COMMIT', 'CONSTRAINT', 'CONTINUE', 'CREATE', 'CROSS', 'CURSOR', 'DATABASE', 'DECLARE', 'DEFAULT', 'DELETE', 'DISTINCT', 'DROP', 'EACH', 'ELSE', 'ELSEIF', 'END', 'EXCEPT', 'EXEC', 'EXECUTE', 'EXIT', 'FETCH', 'FIRST', 'FOR', 'FOREIGN', 'FROM', 'FULL', 'FUNCTION', 'GO', 'GRANT', 'GROUP', 'HAVING', 'IDENTITY', 'IF', 'INDEX', 'INNER', 'INSERT', 'INTERSECT', 'INTO', 'JOIN', 'KEY', 'LEFT', 'LIMIT', 'LAST', 'LOOP', 'MERGE', 'MODIFY', 'NEXT', 'NO', 'OFFSET', 'ON', 'OPEN', 'ORDER', 'OUTER', 'PRIMARY', 'PROC', 'PROCEDURE', 'REFERENCES', 'RELATIVE', 'REPLACE', 'RETURN', 'RETURNS', 'REVOKE', 'RIGHT', 'ROLLBACK', 'ROW', 'ROWS', 'SAVE', 'SCHEMA', 'SELECT', 'SET', 'TABLE', 'THEN', 'TOP', 'TRAN', 'TRANSACTION', 'TRIGGER', 'TRUNCATE', 'UNION', 'UNIQUE', 'UPDATE', 'USE', 'USING', 'VALUES', 'VIEW', 'WHEN', 'WHERE', 'WHILE', 'WITH', 'WITHOUT'];
const defaultLesserKeywords = ['ALL', 'AND', 'ANY', 'AS', 'ASC', 'AVG', 'BETWEEN', 'COLLATE', 'COUNT', 'DESC', 'ESCAPE', 'EXISTS', 'IN', 'IS', 'LIKE', 'MAX', 'MIN', 'NOT', 'NULL', 'OR', 'SOME', 'SUM', 'TO'];
let dataTypes = defaultDataTypes.slice();
let standardKeywords = defaultStandardKeywords.slice();
let lesserKeywords = defaultLesserKeywords.slice();
const ANSIModes = {
reset: '\x1b[0m',
bold: '\x1b[1m',
dim: '\x1b[2m',
italic: '\x1b[3m',
underline: '\x1b[4m',
blink: '\x1b[5m',
inverse: '\x1b[7m',
hidden: '\x1b[8m',
strikethrough: '\x1b[9m'
};
const ANSIColours = {
fg: {
black: '\x1b[30m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m'
},
bg: {
black: '\x1b[40m',
red: '\x1b[41m',
green: '\x1b[42m',
yellow: '\x1b[43m',
blue: '\x1b[44m',
magenta: '\x1b[45m',
cyan: '\x1b[46m',
white: '\x1b[47m'
}
};
const defaults = {
comments: { mode: 'dim', fg: 'white' },
constants: { mode: 'dim', fg: 'red' },
delimitedIdentifiers: { mode: 'dim', fg: 'yellow' },
variables: { mode: 'dim', fg: 'magenta' },
dataTypes: { mode: 'dim', fg: 'green', casing: 'uppercase' },
standardKeywords: { mode: 'dim', fg: 'cyan', casing: 'uppercase' },
lesserKeywords: { mode: 'bold', fg: 'black', casing: 'uppercase' },
prefix: { replace: /.*?: / }
};
let runestone;
/**
* Forge ANSI escape code sequence for text formatting.
* @param {Object} rule - Object that defines the colors to use for formatting a particular rule.
* @returns {string}
*/
function forgeANSISequence(rule) {
let mode, fg, bg;
mode = (rule.mode && ANSIModes[rule.mode]) ? ANSIModes[rule.mode] : '';
fg = (rule.fg && ANSIColours.fg[rule.fg]) ? ANSIColours.fg[rule.fg] : '';
bg = (rule.bg && ANSIColours.bg[rule.bg]) ? ANSIColours.bg[rule.bg] : '';
let ANSISequence = mode + bg + fg;
return ANSISequence;
}
/**
* Remove all ANSI escape code sequences from text.
* @param {string} text - Text piece from which to void all formatting.
* @returns {string}
*/
function voidFormatting(text) {
return text.replace(/\x1b\[\d{1,2}m/g, '');
}
/**
* Highlight syntax of SQL-statments and log to terminal.
* @param {string|Object} text - String of SQL-statements to highlight.
*/
function illumine(text) {
let output,
type = typeof text;
// Coerce entry to string primitive capable of being altered or exit.
if (text && (type === 'string' || type === 'object'))
output = type === 'string' ? text : text.toString();
else
return;
// If a given prefix should be replaced or removed, extract it before any subsequent highlights taint it.
let __prefix;
if (runestone.prefix && runestone.prefix.replace) {
let match = runestone.prefix.replace.exec(output);
if (match) {
__prefix = match[0];
output = output.substr(__prefix.length);
}
}
let __archetypes = {};
if (runestone.own) {
for (const key of Object.keys(runestone.own)) {
const rule = runestone.own[key];
// Extract custom-built archetypes so no subsequent operations alter them. Mark their positions for reinsertion.
__archetypes[key] = output.match(rule.regexp);
if (__archetypes[key] && __archetypes[key].length) {
output = output.replace(rule.regexp, '⥂_' + key + '⥄');
}
}
}
// Extract delimited identifiers so no subsequent operations alter them. Mark their positions for reinsertion.
let __identifiers = output.match(/(\[.*?\]|".*?")/g);
if (__identifiers && __identifiers.length) {
output = output.replace(/(\[.*?\]|".*?")/g, '⇁※↼');
}
// Extract constants so no subsequent operations alter them. Mark their positions for reinsertion.
let __constants = output.match(/('.*?')/g);
if (__constants && __constants.length) {
output = output.replace(/('.*?')/g, '⇝※⇜');
}
// Extract local variables so no subsequent operations alter them. Mark their positions for reinsertion.
let __variables = output.match(/(\B@[@#$_\w\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u00ff\u0100-\u017f\u0180-\u024f]*)/g);
if (__variables && __variables.length) {
output = output.replace(/(\B@[@#$_\w\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u00ff\u0100-\u017f\u0180-\u024f]*)/g, '↪※↩');
}
// Extract comment sections so no subsequent operations alter them. Mark their positions for reinsertion.
let __comments = output.match(/(-{2}.*)|(\/\*(.|[\r\n])*?\*\/)/g);
if (__comments && __comments.length) {
output = output.replace(/(-{2}.*)|(\/\*(.|[\r\n])*?\*\/)/g, '⥤※⥢');
}
if (runestone.dataTypes && runestone.dataTypes.sequence) {
let regex = new RegExp('\\b' + '(' + dataTypes.join('|') + ')' + '\\b' + '(?![\'"\\]])', 'gi');
output = output.replace(regex, (match, g1) => {
let word = g1;
const casing = runestone.dataTypes.casing;
if (typeof casing === 'string' && (casing === 'lowercase' || casing === 'uppercase'))
word = casing === 'lowercase' ? word.toLowerCase() : word.toUpperCase();
return runestone.dataTypes.sequence + word + ANSIModes.reset;
});
}
if (runestone.standardKeywords && runestone.standardKeywords.sequence) {
let regex = new RegExp('\\b' + '(' + standardKeywords.join('|') + ')' + '\\b' + '(?![\'"\\]])', 'gi');
output = output.replace(regex, (match, g1) => {
let word = g1;
const casing = runestone.standardKeywords.casing;
if (typeof casing === 'string' && (casing === 'lowercase' || casing === 'uppercase'))
word = casing === 'lowercase' ? word.toLowerCase() : word.toUpperCase();
return runestone.standardKeywords.sequence + word + ANSIModes.reset;
});
}
if (runestone.lesserKeywords && runestone.lesserKeywords.sequence) {
let regex = new RegExp('\\b' + '(' + lesserKeywords.join('|') + ')' + '\\b' + '(?![\'"\\]])', 'gi');
output = output.replace(regex, (match, g1) => {
let word = g1;
const casing = runestone.lesserKeywords.casing;
if (typeof casing === 'string' && (casing === 'lowercase' || casing === 'uppercase'))
word = casing === 'lowercase' ? word.toLowerCase() : word.toUpperCase();
return runestone.lesserKeywords.sequence + word + ANSIModes.reset;
});
}
if (runestone.numbers && runestone.numbers.sequence) {
output = output.replace(/((\d+\.{1}){0,1}(\d+)(?![a-z\x1b]))(?!\d)/gi, runestone.numbers.sequence + '$1' + ANSIModes.reset);
}
if (runestone.operators && runestone.operators.sequence) {
output = output.replace(/(\+|-|\*|\/|%|&|\||\^|=|>|<)+/g, runestone.operators.sequence + '$&' + ANSIModes.reset);
}
// If comment sections were found and extracted, reinsert them on the marked positions and cordon off the area for reference.
if (__comments && __comments.length) {
for (let i of __comments) {
// If comment sections were to be formatted, apply the provided style.
if (runestone.comments && runestone.comments.sequence)
output = output.replace('⥤※⥢', runestone.comments.sequence + 'c†s' + i + 'c‡e' + ANSIModes.reset);
else
output = output.replace('⥤※⥢', 'c†s' + i + 'c‡e');
}
}
// If local variables were found and extracted, reinsert them on the marked positions.
if (__variables && __variables.length) {
for (let i of __variables) {
// If local variables were to be formatted, apply the provided style.
if (runestone.variables && runestone.variables.sequence)
output = output.replace('↪※↩', runestone.variables.sequence + i + ANSIModes.reset);
else
output = output.replace('↪※↩', i);
}
}
// If constants were found and extracted, reinsert them on the marked positions.
if (__constants && __constants.length) {
for (let i of __constants) {
// If constants were to be formatted, apply the provided style.
if (runestone.constants && runestone.constants.sequence)
output = output.replace('⇝※⇜', runestone.constants.sequence + i + ANSIModes.reset);
else
output = output.replace('⇝※⇜', i);
}
}
// If delimited identifiers were found and extracted, reinsert them on the marked positions.
if (__identifiers && __identifiers.length) {
for (let i of __identifiers) {
// If delimited identifiers were to be formatted, apply the provided style.
if (runestone.delimitedIdentifiers && runestone.delimitedIdentifiers.sequence)
output = output.replace('⇁※↼', runestone.delimitedIdentifiers.sequence + i + ANSIModes.reset);
else
output = output.replace('⇁※↼', i);
}
}
if (runestone.own) {
for (const key of Object.keys(runestone.own).reverse()) {
const rule = runestone.own[key];
// If custom-built archetypes were found and extracted, reinsert them on the marked positions.
if (__archetypes[key] && __archetypes[key].length) {
for (let i of __archetypes[key]) {
let re = i;
if (typeof rule.transform === 'string')
re = rule.transform;
else if (typeof rule.transform === 'function')
re = rule.transform(i);
// Prevent back-reference
re = re.replace(/\$/g,'$$$');
// If custom-built archetypes were to be formatted, apply the provided style.
if (rule && rule.sequence)
output = output.replace('⥂_' + key + '⥄', rule.sequence + re + ANSIModes.reset);
else
output = output.replace('⥂_' + key + '⥄', re);
}
}
}
}
// Constants are to be formatted as a whole and no other format should exist inside them. Void any that could have been applied.
output = output.replace(/('.*?')/g, (match) => {
return voidFormatting(match);
});
// Comment sections are to be formatted as a whole and no other format should exist inside them. Void any that could have been applied and remove cordon.
output = output.replace(/(c†s)((-{2}.*)|(\/\*(.|[\r\n])*?\*\/))(c‡e)/g, (match, p1, p2) => {
return voidFormatting(p2);
});
// If the given prefix was found and a replacement pattern was provided, substitute it.
if (__prefix && typeof runestone.prefix.text === 'string') {
output = __prefix + output;
output = output.replace(__prefix, runestone.prefix.sequence + runestone.prefix.text + ANSIModes.reset);
}
// If only the prefix text was provided, append it.
else if (runestone.prefix && runestone.prefix.text && !runestone.prefix.replace) {
output = runestone.prefix.sequence + runestone.prefix.text + ANSIModes.reset + output;
}
if (runestone.postfix && runestone.postfix.text) {
output = output + runestone.postfix.sequence + runestone.postfix.text + ANSIModes.reset;
}
return runestone.output(output);
}
/**
* Create logger.
* @param {any} [options] - Custom format rules.
* @returns {function} - Syntax highlighter and logging function.
*/
function igniculus(options) {
/* Draft all format sequences from the provided or default
* configuration and save them.
*/
runestone = options || defaults;
if (runestone.comments) {
runestone.comments.sequence = forgeANSISequence(runestone.comments);
}
if (runestone.constants) {
runestone.constants.sequence = forgeANSISequence(runestone.constants);
}
if (runestone.delimitedIdentifiers) {
runestone.delimitedIdentifiers.sequence = forgeANSISequence(runestone.delimitedIdentifiers);
}
if (runestone.numbers) {
runestone.numbers.sequence = forgeANSISequence(runestone.numbers);
}
if (runestone.operators) {
runestone.operators.sequence = forgeANSISequence(runestone.operators);
}
if (runestone.variables) {
runestone.variables.sequence = forgeANSISequence(runestone.variables);
}
if (runestone.dataTypes) {
if (Array.isArray(runestone.dataTypes.types))
dataTypes = runestone.dataTypes.types;
else
dataTypes = defaultDataTypes.slice();
runestone.dataTypes.sequence = forgeANSISequence(runestone.dataTypes);
}
if (runestone.standardKeywords) {
if (Array.isArray(runestone.standardKeywords.keywords))
standardKeywords = runestone.standardKeywords.keywords;
else
standardKeywords = defaultStandardKeywords.slice();
runestone.standardKeywords.sequence = forgeANSISequence(runestone.standardKeywords);
}
if (runestone.lesserKeywords) {
if (Array.isArray(runestone.lesserKeywords.keywords))
lesserKeywords = runestone.lesserKeywords.keywords;
else
lesserKeywords = defaultLesserKeywords.slice();
runestone.lesserKeywords.sequence = forgeANSISequence(runestone.lesserKeywords);
}
if (runestone.prefix) {
runestone.prefix.sequence = forgeANSISequence(runestone.prefix);
/* If prefix should replace a given pattern and that pattern is a string,
* escape it so it can be passed to the RegExp constructor.
*/
if (runestone.prefix.replace && typeof runestone.prefix.replace === 'string') {
runestone.prefix.replace = runestone.prefix.replace.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
runestone.prefix.replace = new RegExp('^' + runestone.prefix.replace, 'i');
}
}
if (runestone.postfix) {
runestone.postfix.sequence = forgeANSISequence(runestone.postfix);
}
if (runestone.own) {
for (const key of Object.keys(runestone.own)) {
const rule = runestone.own[key];
rule.sequence = forgeANSISequence(rule);
}
}
if (typeof runestone.output !== 'function') {
runestone.output = console.log;
}
return illumine;
}
module.exports = igniculus;
module.exports.log = illumine;