Skip to content

Commit e5fee51

Browse files
ggerhardGerhard Görlich
andauthored
Bugfixes for Issue #10 and #11 (#12)
* fixed keyword at end of line bug * fixer/exclude nested matches to fix duplicate substring bug * fixed semicolons and indentation Co-authored-by: Gerhard Görlich <[email protected]>
1 parent 628cd72 commit e5fee51

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const SPLIT_CHARS = '[^a-zA-Z_]'
2121
const highlighters = [
2222
{
2323
name: 'keyword',
24-
regex: new RegExp(`(?:^|${SPLIT_CHARS})(?:${keywords.join('|')})(?=${SPLIT_CHARS})`, 'g')
24+
regex: new RegExp(`(?:^|${SPLIT_CHARS})(?:${keywords.join('|')})(?=${SPLIT_CHARS}|$)`, 'g')
2525
},
2626
{
2727
name: 'special',
@@ -65,11 +65,21 @@ function highlight (sqlString, options) {
6565

6666
const sortedMatches = matches.slice().sort((a, b) => a.start - b.start)
6767

68+
// filter/exclude nested matches (matches within the last match)
69+
let filteredMatches = []
70+
let upperBound = 0
71+
for (let i = 0; i < sortedMatches.length; i++) {
72+
if (sortedMatches[i].start >= upperBound) {
73+
filteredMatches.push(sortedMatches[i])
74+
upperBound = sortedMatches[i].start + sortedMatches[i].length
75+
}
76+
}
77+
6878
let highlighted = ''
6979

70-
for (let i = 0; i < sortedMatches.length; i++) {
71-
const match = sortedMatches[i]
72-
const nextMatch = sortedMatches[i + 1]
80+
for (let i = 0; i < filteredMatches.length; i++) {
81+
const match = filteredMatches[i]
82+
const nextMatch = filteredMatches[i + 1]
7383

7484
const stringMatch = sqlString.substr(match.start, match.length)
7585

test/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ const { highlight } = require('../index')
88
console.log(highlight("SELECT COUNT(id), COUNT(id), `id`, `username` FROM `users` WHERE `email` = '[email protected]' AND `something` = 'oke' AND 1=1"))
99
console.log(highlight('SELECT "users".* FROM "users"'))
1010
console.log(highlight(`select "users".* from "users" where ("username" = 'test' or "email" = 'test') and "is_admin" = true and "is_banned" = false limit 1`))
11+
12+
console.log(highlight("SELECT COUNT(id), COUNT(id), `id`, `username` FROM `users` WHERE `email` = '[email protected]' AND `something` = 'oke-doke' AND true = true"))
13+
console.log(highlight("SELECT COUNT(id), `id`, `username` FROM `users` WHERE `email` = '[email protected]' AND (username in ( SELECT \"name\" from aTable)", {html: false} ))

0 commit comments

Comments
 (0)