-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(strings): add tab conversion fns
- Loading branch information
1 parent
2a283c0
commit aefdd97
Showing
2 changed files
with
95 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,94 @@ | ||
import { repeat } from "./repeat"; | ||
|
||
const nextTab = (x: number, tabSize: number) => | ||
Math.floor((x + tabSize) / tabSize) * tabSize; | ||
|
||
/** | ||
* Multi-line version of {@link tabsToSpacesLine}. | ||
* | ||
* @example | ||
* ```ts | ||
* console.log( | ||
* tabsToSpaces("0\t1\t2", 10) | ||
* + "\n" + | ||
* tabsToSpaces("0\t45\t890\t\t6\t0") | ||
* + "\n" + | ||
* tabsToSpaces("^\t^\t^\t^\t^\t^") | ||
* ); | ||
* // 0 1 2 | ||
* // 0 45 890 6 0 | ||
* // ^ ^ ^ ^ ^ ^ | ||
* ``` | ||
* | ||
* @param src | ||
* @param tabSize | ||
*/ | ||
export const tabsToSpaces = (src: string, tabSize = 4) => | ||
src | ||
.split(/\r?\n/g) | ||
.map((line) => tabsToSpacesLine(line, tabSize)) | ||
.join("\n"); | ||
|
||
/** | ||
* Takes a single line string and converts all tab characters to spaces, using | ||
* given `tabSize`. | ||
* | ||
* @param line | ||
* @param tabSize | ||
*/ | ||
export const tabsToSpacesLine = (line: string, tabSize = 4) => { | ||
let res = ""; | ||
let words = line.split(/\t/g); | ||
let n = words.length - 1; | ||
for (let i = 0; i < n; i++) { | ||
const w = words[i]; | ||
res += w; | ||
res += repeat(" ", nextTab(res.length, tabSize) - res.length); | ||
} | ||
res += words[n]; | ||
return res; | ||
}; | ||
|
||
/** | ||
* Multi-line version of {@link spacesToTabsLine}. | ||
* | ||
* @param src | ||
* @param tabSize | ||
*/ | ||
export const spacesToTabs = (src: string, tabSize = 4) => | ||
src | ||
.split(/\r?\n/g) | ||
.map((line) => spacesToTabsLine(line, tabSize)) | ||
.join("\n"); | ||
|
||
/** | ||
* Takes a single line string and converts all tab characters to spaces, using | ||
* given `tabSize`. Inverse op of {@link tabsToSpacesLine}. | ||
* | ||
* @param line | ||
* @param tabSize | ||
*/ | ||
export const spacesToTabsLine = (line: string, tabSize = 4) => { | ||
const re = /\s{2,}/g; | ||
let i = 0; | ||
let res = ""; | ||
let m: RegExpExecArray | null; | ||
while ((m = re.exec(line))) { | ||
const numSpaces = m[0].length; | ||
res += line.substring(i, m.index); | ||
i = m.index; | ||
const end = m.index + numSpaces; | ||
while (i < end) { | ||
const j = nextTab(i, tabSize); | ||
if (j <= end) { | ||
res += "\t"; | ||
i = j; | ||
} else { | ||
res += repeat(" ", end - i); | ||
i = end; | ||
} | ||
} | ||
i = end; | ||
} | ||
return res + line.substr(i); | ||
}; |