Skip to content

Commit ea355da

Browse files
committed
perf: tweak up constants
1 parent 229c294 commit ea355da

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/main/ts/envapi.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const DOTENV = '.env'
66
const Q1 = '"' // double quote
77
const Q2 = "'" // single quote
88
const Q3 = '`' // backtick
9-
9+
const KR = /^[a-zA-Z_]\w*$/
10+
const SR = /\s/
1011
const decoder = new TextDecoder()
12+
1113
export const parse = (content: string | Buffer): NodeJS.ProcessEnv => {
12-
const kr = /^[a-zA-Z_]+\w*$/
13-
const sr = /\s/
1414
const e: Record<string, string> = {}
1515
let k = '' // key
1616
let b = '' // buffer
@@ -19,7 +19,7 @@ export const parse = (content: string | Buffer): NodeJS.ProcessEnv => {
1919
const cap = () => {
2020
k = k.trim()
2121
if (k) {
22-
if (!kr.test(k)) throw new Error(`Invalid identifier: ${k}`)
22+
if (!KR.test(k)) throw new Error(`Invalid identifier: ${k}`)
2323
e[k] = b.trim(); b = k = ''
2424
}
2525
}
@@ -38,15 +38,14 @@ export const parse = (content: string | Buffer): NodeJS.ProcessEnv => {
3838
cap()
3939
continue
4040
}
41-
if (sr.test(c)) {
41+
if (SR.test(c)) {
4242
if (!k && b === 'export') b = ''
4343
if (!b) continue
4444
}
4545
if (c === '=') {
4646
if (!k) { k = b; b = ''; continue }
4747
}
4848
}
49-
5049
if (c === Q1 || c === Q2 || c === Q3) {
5150
if (!q && !b) {
5251
q = c
@@ -69,7 +68,7 @@ const formatValue = (v: string): string => {
6968
const q1 = v.includes(Q1)
7069
const q2 = v.includes(Q2)
7170
const q3 = v.includes(Q3)
72-
const s = /\s/.test(v)
71+
const s = SR.test(v)
7372

7473
if (!q1 && !q2 && !q3 && !s) return v
7574
if (!q1) return `${Q1}${v}${Q1}`

0 commit comments

Comments
 (0)