diff --git a/index.test.js b/index.test.js index caf7909..5fb0a98 100644 --- a/index.test.js +++ b/index.test.js @@ -8,7 +8,8 @@ const OPTIONS = { string: '[string]', special: '[special]', bracket: '[bracket]', - clear: '[clear]' + clear: '[clear]', + default: '[default]' } } @@ -32,12 +33,12 @@ describe('unicode', () => { it('strings (mixing quotes)', () => { expect(hlUni('\'"`\' "\'`" `"\'`')) - .toBe('[string]\'"`\'[clear] [string]"\'`"[clear] [string]`"\'`[clear]') + .toBe('[string]\'"`\'[clear][default] [clear][string]"\'`"[clear][default] [clear][string]`"\'`[clear]') }) it('strings (scaping quotes)', () => { expect(hlUni('\'\\\'\' "\\"" `\\``')) - .toBe('[string]\'\\\'\'[clear] [string]"\\""[clear] [string]`\\``[clear]') + .toBe('[string]\'\\\'\'[clear][default] [clear][string]"\\""[clear][default] [clear][string]`\\``[clear]') }) it('integers', () => { @@ -81,8 +82,8 @@ describe('unicode', () => { }) it('alphanumeric', () => { - expect(hlUni('(f1)')) - .toBe('[bracket]([clear]f1[bracket])[clear]') + expect(hlUni('(f2)')) + .toBe('[bracket]([clear][default]f2[clear][bracket])[clear]') }) it('functions', () => { @@ -92,12 +93,12 @@ describe('unicode', () => { it('basic query', () => { expect(hlUni("SELECT * FROM `users` WHERE `email` = 'test@example.com'")) - .toBe("[keyword]SELECT[clear] [special]*[clear] [keyword]FROM[clear] [string]`users`[clear] [keyword]WHERE[clear] [string]`email`[clear] [special]=[clear] [string]'test@example.com'[clear]") + .toBe("[keyword]SELECT[clear][default] [clear][special]*[clear][default] [clear][keyword]FROM[clear][default] [clear][string]`users`[clear][default] [clear][keyword]WHERE[clear][default] [clear][string]`email`[clear][default] [clear][special]=[clear][default] [clear][string]'test@example.com'[clear]") }) it('complex query', () => { expect(hlUni("SELECT COUNT(id), `id`, `username` FROM `users` WHERE `email` = 'test@example.com' AND `foo` = 'BAR' OR 1=1")) - .toBe("[keyword]SELECT[clear] [function]COUNT[clear][bracket]([clear]id[bracket])[clear][special],[clear] [string]`id`[clear][special],[clear] [string]`username`[clear] [keyword]FROM[clear] [string]`users`[clear] [keyword]WHERE[clear] [string]`email`[clear] [special]=[clear] [string]'test@example.com'[clear] [keyword]AND[clear] [string]`foo`[clear] [special]=[clear] [string]'BAR'[clear] [keyword]OR[clear] [number]1[clear][special]=[clear][number]1[clear]") + .toBe("[keyword]SELECT[clear][default] [clear][function]COUNT[clear][bracket]([clear][default]id[clear][bracket])[clear][special],[clear][default] [clear][string]`id`[clear][special],[clear][default] [clear][string]`username`[clear][default] [clear][keyword]FROM[clear][default] [clear][string]`users`[clear][default] [clear][keyword]WHERE[clear][default] [clear][string]`email`[clear][default] [clear][special]=[clear][default] [clear][string]'test@example.com'[clear][default] [clear][keyword]AND[clear][default] [clear][string]`foo`[clear][default] [clear][special]=[clear][default] [clear][string]'BAR'[clear][default] [clear][keyword]OR[clear][default] [clear][number]1[clear][special]=[clear][number]1[clear]") }) }) @@ -114,12 +115,12 @@ describe('html', () => { it('strings (mixing quotes)', () => { expect(hlHtml('\'"`\' "\'`" `"\'`')) - .toBe('\'"`\' "\'`" `"\'`') + .toBe('\'"`\' "\'`" `"\'`') }) it('strings (scaping quotes)', () => { expect(hlHtml('\'\\\'\' "\\"" `\\``')) - .toBe('\'\\\'\' "\\"" `\\``') + .toBe("'\\'' \"\\\"\" `\\``") }) it('integers', () => { @@ -159,7 +160,7 @@ describe('html', () => { it('alphanumeric', () => { expect(hlHtml('(f1)')) - .toBe('(f1)') + .toBe('(f1)') }) it('functions', () => { @@ -169,17 +170,17 @@ describe('html', () => { it('basic query', () => { expect(hlHtml("SELECT * FROM `users` WHERE `email` = 'test@example.com'")) - .toBe("SELECT * FROM `users` WHERE `email` = 'test@example.com'") + .toBe("SELECT * FROM `users` WHERE `email` = 'test@example.com'") }) it('complex query', () => { expect(hlHtml("SELECT COUNT(id), `id`, `username` FROM `users` WHERE `email` = 'test@example.com' AND `foo` = 'BAR' OR 1=1")) - .toBe("SELECT COUNT(id), `id`, `username` FROM `users` WHERE `email` = 'test@example.com' AND `foo` = 'BAR' OR 1=1") + .toBe("SELECT COUNT(id), `id`, `username` FROM `users` WHERE `email` = 'test@example.com' AND `foo` = 'BAR' OR 1=1") }) it('query with identifiers without apostrophes', () => { expect(hlHtml('SELECT id FROM users')) - .toBe('SELECT id FROM users') + .toBe('SELECT id FROM users') }) }) diff --git a/lib/index.js b/lib/index.js index 983579f..fce2dbd 100644 --- a/lib/index.js +++ b/lib/index.js @@ -12,7 +12,8 @@ const DEFAULT_OPTIONS = { string: '\x1b[32m', special: '\x1b[33m', bracket: '\x1b[33m', - clear: '\x1b[0m' + clear: '\x1b[0m', + default: '' } } @@ -72,12 +73,12 @@ function getSegments (sqlString) { const trimmedText = hl.trimEnd ? text.substring(0, text.length - hl.trimEnd) - : text; + : text matches.push({ name: hl.name, start: match.index + boringLength, - length: trimmedText.length, - }); + length: trimmedText.length + }) } } @@ -88,7 +89,7 @@ function getSegments (sqlString) { let upperBound = 0 for (let i = 0; i < sortedMatches.length; i++) { if (sortedMatches[i].start < upperBound) { break } - + // If no match, add a default segment if (sortedMatches[i].start > upperBound) { segments.push({ @@ -96,7 +97,7 @@ function getSegments (sqlString) { content: sqlString.substring(upperBound, sortedMatches[i].start) }) } - + segments.push({ name: sortedMatches[i].name, content: sqlString.substring( @@ -124,9 +125,6 @@ function highlight (sqlString, options) { return getSegments(sqlString) .map(({ name, content }) => { - if (name === DEFAULT_KEYWORD) { - return content - } if (options.html) { return `${content}` }