Skip to content

Commit 02d459a

Browse files
wkeesescriptcoded
authored andcommitted
fix: improve number detection
Fixes #149
1 parent 25677d4 commit 02d459a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const DEFAULT_OPTIONS = {
2121
}
2222

2323
const highlighters = [
24-
/\b(?<number>\d+(?:\.\d+)?)\b/,
24+
/(?<number>[+-]?(?:\d+\.\d+|\d+|\.\d+)(?:E[+-]?\d+)?)/,
2525

2626
// Note: Repeating string escapes like 'sql''server' will also work as they are just repeating strings
2727
/(?<string>'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")/,

test/index.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,29 @@ describe('html', () => {
258258
})
259259

260260
describe('getSegments', () => {
261+
it('numbers and operators', () => {
262+
expect(getSegments('34 - -.5 + +0.5 * 1.23E45 / 4E-3'))
263+
.toStrictEqual([
264+
{ name: 'number', content: '34' },
265+
{ name: 'whitespace', content: ' ' },
266+
{ name: 'special', content: '-' },
267+
{ name: 'whitespace', content: ' ' },
268+
{ name: 'number', content: '-.5' },
269+
{ name: 'whitespace', content: ' ' },
270+
{ name: 'special', content: '+' },
271+
{ name: 'whitespace', content: ' ' },
272+
{ name: 'number', content: '+0.5' },
273+
{ name: 'whitespace', content: ' ' },
274+
{ name: 'special', content: '*' },
275+
{ name: 'whitespace', content: ' ' },
276+
{ name: 'number', content: '1.23E45' },
277+
{ name: 'whitespace', content: ' ' },
278+
{ name: 'special', content: '/' },
279+
{ name: 'whitespace', content: ' ' },
280+
{ name: 'number', content: '4E-3' }
281+
])
282+
})
283+
261284
it('complex query', () => {
262285
expect(getSegments("SELECT COUNT(id), `id`, `username` FROM `users` WHERE `email` = '[email protected]' AND `foo` = 'BAR' OR 1=1"))
263286
.toStrictEqual([

0 commit comments

Comments
 (0)