Skip to content

Commit

Permalink
fix: Jsonc comments remover
Browse files Browse the repository at this point in the history
  • Loading branch information
pluveto committed Feb 5, 2022
1 parent dbfe70e commit 22c5b7f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
19 changes: 19 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"os"
"path/filepath"

Expand Down Expand Up @@ -53,3 +54,21 @@ func RemoveFmtUnderscore(in string) (out string) {
}
return
}

func RemoveJsoncComments(data []byte) []byte {
var buf bytes.Buffer
var inLineComment bool = false
for i, b := range data {
if b == '/' && i+1 < len(data) && data[i+1] == '/' {
inLineComment = true
}
if b == '\n' {
inLineComment = false
}
if inLineComment {
continue
}
buf.WriteByte(b)
}
return buf.Bytes()
}
39 changes: 39 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"reflect"
"testing"
)

Expand All @@ -27,3 +28,41 @@ func TestRemoveFmtUnderscore(t *testing.T) {
})
}
}

func TestRemoveJsoncComments(t *testing.T) {
type args struct {
data []byte
}
tests := []struct {
name string
args args
want []byte
}{
{"1", args{
[]byte(
`
a//bcde//
//c
//
a
`,
),
},
[]byte(
`
a
a
`,
),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RemoveJsoncComments(tt.args.data); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RemoveJsoncComments() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 22c5b7f

Please sign in to comment.