@c_s_v_s_subrahmanyam/string-utils
Version:
A comprehensive utility library for advanced string operations.
26 lines (19 loc) • 845 B
JavaScript
module.exports = {
// String Translation
translate: (str, mapping) => {
const translated = [...str].map((char) => mapping[char] || char).join('');
return translated;
},
// Expand Tabs
expandTabs: (str, tabSize = 4) => str.replace(/\t/g, ' '.repeat(tabSize)),
// Character Iteration
iterateCharacters: (str) => [...str],
// Indexing and Slicing
sliceString: (str, start, end) => str.slice(start, end),
getIndex: (str, index) => (index >= 0 && index < str.length ? str[index] : undefined),
// String Partitioning
splitAt: (str, index) => [str.slice(0, index), str.slice(index)],
// Multibyte Encoding (UTF-8)
encodeToBytes: (str) => new TextEncoder().encode(str),
decodeFromBytes: (bytes) => new TextDecoder().decode(bytes),
};