From db1c7a58c7feb58ae329ff7de4a9083ba097e468 Mon Sep 17 00:00:00 2001 From: Bill Keese Date: Tue, 2 Apr 2024 16:58:54 +0900 Subject: [PATCH] fix: improve number detection Fixes #149 --- lib/index.js | 2 +- test/index.test.js | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 4a77ef0..adf4615 100644 --- a/lib/index.js +++ b/lib/index.js @@ -21,7 +21,7 @@ const DEFAULT_OPTIONS = { } const highlighters = [ - /\b(?\d+(?:\.\d+)?)\b/, + /(?[+-]?(?:\d+\.\d+|\d+|\.\d+)(?:E[+-]?\d+)?)/, // Note: Repeating string escapes like 'sql''server' will also work as they are just repeating strings /(?'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")/, diff --git a/test/index.test.js b/test/index.test.js index ab04fe3..685219e 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -256,6 +256,29 @@ describe('html', () => { }) describe('getSegments', () => { + it('numbers and operators', () => { + expect(getSegments('34 - -.5 + +0.5 * 1.23E45 / 4E-3')) + .toStrictEqual([ + { name: 'number', content: '34' }, + { name: 'whitespace', content: ' ' }, + { name: 'special', content: '-' }, + { name: 'whitespace', content: ' ' }, + { name: 'number', content: '-.5' }, + { name: 'whitespace', content: ' ' }, + { name: 'special', content: '+' }, + { name: 'whitespace', content: ' ' }, + { name: 'number', content: '+0.5' }, + { name: 'whitespace', content: ' ' }, + { name: 'special', content: '*' }, + { name: 'whitespace', content: ' ' }, + { name: 'number', content: '1.23E45' }, + { name: 'whitespace', content: ' ' }, + { name: 'special', content: '/' }, + { name: 'whitespace', content: ' ' }, + { name: 'number', content: '4E-3' } + ]) + }) + it('complex query', () => { expect(getSegments("SELECT COUNT(id), `id`, `username` FROM `users` WHERE `email` = 'test@example.com' AND `foo` = 'BAR' OR 1=1")) .toStrictEqual([