提交 6839bdf7 编写于 作者: J Johannes Rieken

more getLeadingWhitespace-tricks

上级 fb722316
......@@ -293,14 +293,14 @@ export function firstNonWhitespaceIndex(str: string): number {
* Returns the leading whitespace of the string.
* If the string contains only whitespaces, returns entire string
*/
export function getLeadingWhitespace(str: string, from: number = 0): string {
for (let i = from, len = str.length; i < len; i++) {
export function getLeadingWhitespace(str: string, start: number = 0, end: number = str.length): string {
for (let i = start; i < end; i++) {
let chCode = str.charCodeAt(i);
if (chCode !== CharCode.Space && chCode !== CharCode.Tab) {
return str.substring(from, i);
return str.substring(start, i);
}
}
return str.substr(from);
return str.substring(start, end);
}
/**
......
......@@ -328,7 +328,13 @@ suite('Strings', () => {
test('getLeadingWhitespace', () => {
assert.equal(strings.getLeadingWhitespace(' foo'), ' ');
assert.equal(strings.getLeadingWhitespace(' foo', 2), '');
assert.equal(strings.getLeadingWhitespace(' foo', 1, 1), '');
assert.equal(strings.getLeadingWhitespace(' foo', 0, 1), ' ');
assert.equal(strings.getLeadingWhitespace(' '), ' ');
assert.equal(strings.getLeadingWhitespace(' ', 1), ' ');
assert.equal(strings.getLeadingWhitespace(' ', 0, 1), ' ');
assert.equal(strings.getLeadingWhitespace('\t\tfunction foo(){', 0, 1), '\t');
assert.equal(strings.getLeadingWhitespace('\t\tfunction foo(){', 0, 2), '\t\t');
});
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册