MCPcopy Create free account
hub / github.com/Effect-TS/effect / parseUnsafe

Function parseUnsafe

packages/effect/src/encoding/Jsonc.ts:28–80  ·  view source on GitHub ↗
(input: string)

Source from the content-addressed store, hash-verified

26const lineEnd = /[\r\n]/g
27
28const parseUnsafe = (input: string): unknown => {
29 // Ignore one leading BOM without shifting original-input error positions.
30 if (input.charCodeAt(0) === 0xfeff) input = " " + input.slice(1)
31 let slash = input.indexOf("/")
32 let json = input
33 if (slash !== -1) {
34 let stripped = ""
35 let start = 0
36 let quote = input.indexOf("\"")
37 while (slash !== -1) {
38 if (quote !== -1 && quote < slash) {
39 // Skip the string; a quote after an odd run of backslashes is escaped.
40 let end = quote
41 for (;;) {
42 end = input.indexOf("\"", end + 1)
43 if (end === -1) break
44 let backslash = end - 1
45 while (input.charCodeAt(backslash) === 92) backslash--
46 if ((end - backslash) % 2 === 1) break
47 }
48 if (end === -1) break
49 quote = input.indexOf("\"", end + 1)
50 if (slash < end) slash = input.indexOf("/", end + 1)
51 continue
52 }
53 const next = input.charCodeAt(slash + 1)
54 let index: number
55 if (next === 47) {
56 lineEnd.lastIndex = slash + 2
57 index = lineEnd.test(input) ? lineEnd.lastIndex - 1 : input.length
58 } else if (next === 42) {
59 const end = input.indexOf("*/", slash + 2)
60 if (end === -1) fail(input)
61 index = end + 2
62 } else {
63 // Not a comment; JSON.parse rejects it below.
64 slash = input.indexOf("/", slash + 1)
65 continue
66 }
67 // A space prevents comments inside invalid tokens from joining those tokens.
68 stripped += input.slice(start, slash) + " "
69 start = index
70 slash = input.indexOf("/", index)
71 if (quote !== -1 && quote < index) quote = input.indexOf("\"", index)
72 }
73 json = stripped + input.slice(start)
74 }
75 try {
76 return JSON.parse(json)
77 } catch {
78 return fail(input)
79 }
80}
81
82/**
83 * Parses a JSONC document into a JavaScript value.

Callers 1

parseFunction · 0.70

Calls 3

sliceMethod · 0.80
failFunction · 0.70
parseMethod · 0.45

Tested by

no test coverage detected