-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (38 loc) · 1.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"use strict"
module.exports = validate
function validate(input, silent) {
let valid = false
try {
JSON.parse(input)
valid = true
} catch(exception) {
const errorPosition = /\d+$/
warn(exception.message)
if( errorPosition.test(exception.message) ) {
const red = process.stdout.isTTY ? msg => `\u001B[31m${msg}\u001B[39m` : x=>x
const position = exception.message.match(errorPosition)[0]|0
warn(
input.slice(0, position) +
red(input.slice(position, position + 1)) +
input.slice(position + 1, position + 2)
)
// recalculate position so new line doesn't count
warn( red("-".repeat(recalcPosition(position, input)) + "^") )
}
} finally {
validate.running = false
return valid
}
function warn(msg) {
silent || console.warn(msg)
}
}
function recalcPosition(position, input) {
let i = 0|0, newPos = 0|0
while (i !== position) {
newPos++
if(input[i] === "\n") newPos = 0|0
i++
}
return newPos
}