Skip to content

Commit

Permalink
feat(dedent): 支持通过函数调用处理纯文本
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 8, 2021
1 parent 3e063bb commit 8f0e27d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/utils/dedent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,14 @@ describe('dedent', () => {
const y = 'a\nb\n\nc\n\nd\ne'
expect(x).toBe(y)
})

test('通过函数调用正常', () => {
expect(
dedent(`
a
b
`),
).toBe('a\nb')
expect(dedent('')).toBe('')
})
})
35 changes: 34 additions & 1 deletion src/utils/dedent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { indent } from './indent'

/**
* 移除每一行的公共前导空白。
*
* @public
* @param text 文本
* @returns 返回处理后的结果
* @example
* ```typescript
* dedent(' a\n b') // => 'a\nb'
* ```
*/
export function dedent(text: string): string

/**
* 首先,每一行紧跟前导空白的插入值为多行时,保持缩进。
* 然后,移除每一行的公共前导空白。
Expand All @@ -16,8 +29,28 @@ import { indent } from './indent'
export function dedent(
literals: TemplateStringsArray,
...interpolations: Array<string | number>
): string

/**
* 首先,每一行紧跟前导空白的插入值为多行时,保持缩进。
* 然后,移除每一行的公共前导空白。
*
* @public
* @param literals 字面值
* @param interpolations 插入值
* @returns 返回处理后的结果
* @example
* ```typescript
* dedent` a\n b` // => 'a\nb'
* ```
*/
export function dedent(
literals: TemplateStringsArray | string,
...interpolations: Array<string | number>
): string {
const text = indent(literals, ...interpolations)
const text = Array.isArray(literals)
? indent(literals as TemplateStringsArray, ...interpolations)
: (literals as string)

// 公共的前导空白
let commonLeadingWhitespace!: string
Expand Down

0 comments on commit 8f0e27d

Please sign in to comment.