-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { indent } from './indent' | ||
|
||
describe(indent.name, () => { | ||
test('空字符串正常', () => { | ||
expect(indent``).toBe('') | ||
}) | ||
|
||
test('首行无前导空白正常', () => { | ||
expect(indent`${[1, 2].join('\n')}`).toBe('1\n2') | ||
}) | ||
|
||
test('首行有前导空白正常', () => { | ||
expect(indent` ${[1, 2].join('\n')}`).toBe(' 1\n 2') | ||
}) | ||
|
||
test('非首行无前导空白正常', () => { | ||
expect(indent`\n${[1, 2].join('\n')}`).toBe('\n1\n2') | ||
expect(indent`\r\n${[1, 2].join('\n')}`).toBe('\r\n1\n2') | ||
expect(indent`0\r\r\n${[1, 2].join('\n')}`).toBe('0\r\r\n1\n2') | ||
}) | ||
|
||
test('非首行有前导空白正常', () => { | ||
expect(indent`\n ${[1, 2].join('\n')}`).toBe('\n 1\n 2') | ||
expect(indent`\r\n ${[1, 2].join('\n')}`).toBe('\r\n 1\n 2') | ||
expect(indent`0 \r\r\n ${[1, 2].join('\n')}`).toBe('0 \r\r\n 1\n 2') | ||
expect(indent`x \n\n ${[1, 2].join('\n')}`).toBe('x \n\n 1\n 2') | ||
}) | ||
|
||
test('只处理紧跟前导空白后面的插入值', () => { | ||
expect(indent`\n 0 ${[1, 2].join('\n')}`).toBe('\n 0 1\n2') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* 每一行紧跟前导空白的插入值为多行时,保持缩进。 | ||
* | ||
* ``` | ||
* indent` ${'a\nb'}` // => ' a\n b' | ||
* ``` | ||
* | ||
* @param literals 字面值 | ||
* @param interpolations 插入值 | ||
* @returns 返回处理后的结果 | ||
*/ | ||
export function indent( | ||
literals: TemplateStringsArray, | ||
...interpolations: string[] | ||
): string { | ||
let result = '' | ||
|
||
for (let i = 0; i < interpolations.length; i++) { | ||
const literal = literals[i] | ||
let interpolation = interpolations[i] | ||
const match = literal.match(/(?:^|[\r\n]+)([^\S\r\n]*)$/) | ||
if (match && match[1]) { | ||
interpolation = interpolation.replace( | ||
/(?<=[\r\n]+)(?=[^\r\n])/g, | ||
match[1], | ||
) | ||
} | ||
result += literal | ||
result += interpolation | ||
} | ||
|
||
result += literals[literals.length - 1] | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters