Skip to content

Commit

Permalink
fix: improve number detection
Browse files Browse the repository at this point in the history
Fixes #149
  • Loading branch information
wkeese committed Apr 2, 2024
1 parent 9e7718c commit db1c7a5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/index.js
Expand Up @@ -21,7 +21,7 @@ const DEFAULT_OPTIONS = {
}

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

// Note: Repeating string escapes like 'sql''server' will also work as they are just repeating strings
/(?<string>'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")/,
Expand Down
23 changes: 23 additions & 0 deletions test/index.test.js
Expand Up @@ -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([
Expand Down

0 comments on commit db1c7a5

Please sign in to comment.