Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | 11x 11x 11x 53x 53x 32x 32x 9x 32x 9x 11x 35x | import { indent } from './indent'
/**
* 移除每一行的公共前导空白。
*
* @public
* @param text 文本
* @returns 返回处理后的结果
* @example
* ```typescript
* dedent(' a\n b') // => 'a\nb'
* ```
*/
export function dedent(text: string): string
/**
* 首先,每一行紧跟前导空白的插入值为多行时,保持缩进。
* 然后,移除每一行的公共前导空白。
*
* @public
* @param literals 字面值
* @param interpolations 插入值
* @returns 返回处理后的结果
* @example
* ```typescript
* dedent` a\n b` // => 'a\nb'
* ```
*/
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 = Array.isArray(literals)
? indent(literals as TemplateStringsArray, ...interpolations)
: (literals as string)
// 公共的前导空白
let commonLeadingWhitespace!: string
// 第一个非空行
let firstLineIndex!: number
// 最后一个非空行
let lastLineIndex!: number
const lines = text.split(/[\r\n]/g)
for (let index = 0; index < lines.length; index++) {
// 当前行的前导空白
const leadingWhitespace = lines[index].match(/^\s*/)![0]
// 如果当前行的前导空白等于当前行的长度,则认为这是一个空行,跳过
if (leadingWhitespace.length !== lines[index].length) {
lastLineIndex = index
if (firstLineIndex == null) {
firstLineIndex = index
}
if (
commonLeadingWhitespace == null ||
leadingWhitespace.length < commonLeadingWhitespace.length
) {
commonLeadingWhitespace = leadingWhitespace
}
}
}
return commonLeadingWhitespace == null
? text
: lines
.slice(firstLineIndex, lastLineIndex + 1)
.map(line => line.substr(commonLeadingWhitespace.length))
.join('\n')
}
|