Files
nodejs.esm-library/source/text/text-utilities.mjs
2026-05-04 21:39:59 +02:00

31 lines
755 B
JavaScript
Raw Blame History

export const char_width_lut = {
'〃': 2,
}
//NOTE - this one only support posix newlines, this should be fixed later
export function tabs_to_spaces(text, tab_width=4) {
let pending_column_index=0;
let result = '';
for (const char of text) {
if (char == '\t') {
const new_column_index = (Math.floor(pending_column_index / tab_width) + 1) * tab_width;
result += ' '.repeat(new_column_index - pending_column_index);
pending_column_index = new_column_index;
} else {
if (char.charCodeAt(0) === 10) {
result += '\n';
pending_column_index = 0;
/* } else if (char.charCodeAt(0) < 32) {
result += '<27>';
*/ } else {
pending_column_index += (char_width_lut[char] ?? 1)
result += char;
}
}
}
return result;
}