Skip to content

Commit d931d90

Browse files
authored
style(util): improve formatCmd (google#1069)
1 parent 828c7eb commit d931d90

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

src/cli.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,14 @@ export function injectGlobalRequire(origin: string) {
217217
}
218218

219219
export function transformMarkdown(buf: Buffer | string): string {
220-
const source = buf.toString()
221220
const output = []
222221
const tabRe = /^( +|\t)/
223222
const codeBlockRe =
224223
/^(?<fence>(`{3,20}|~{3,20}))(?:(?<js>(js|javascript|ts|typescript))|(?<bash>(sh|shell|bash))|.*)$/
225224
let state = 'root'
226225
let codeBlockEnd = ''
227226
let prevLineIsEmpty = true
228-
for (const line of source.split(/\r?\n/)) {
227+
for (const line of buf.toString().split(/\r?\n/)) {
229228
switch (state) {
230229
case 'root':
231230
if (tabRe.test(line) && prevLineIsEmpty) {

src/util.ts

+6-12
Original file line numberDiff line numberDiff line change
@@ -277,23 +277,19 @@ export function formatCmd(cmd?: string): string {
277277
}
278278

279279
function space() {
280-
if (/\s/.test(ch)) return space
281-
return root
280+
return /\s/.test(ch) ? space : root
282281
}
283282

284283
function word() {
285-
if (/[\w/.]/i.test(ch)) return word
286-
return root
284+
return /[\w/.]/i.test(ch) ? word : root
287285
}
288286

289287
function syntax() {
290-
if (isSyntax(ch)) return syntax
291-
return root
288+
return isSyntax(ch) ? syntax : root
292289
}
293290

294291
function dollar() {
295-
if (ch === "'") return str
296-
return root
292+
return ch === "'" ? str : root
297293
}
298294

299295
function str() {
@@ -311,13 +307,11 @@ export function formatCmd(cmd?: string): string {
311307
}
312308

313309
function strDouble() {
314-
if (ch === '"') return strEnd
315-
return strDouble
310+
return ch === '"' ? strEnd : strDouble
316311
}
317312

318313
function strSingle() {
319-
if (ch === "'") return strEnd
320-
return strSingle
314+
return ch === "'" ? strEnd : strSingle
321315
}
322316

323317
function strEnd() {

0 commit comments

Comments
 (0)