This module provides a class for expanding tab characters ('\t') in a string to a specified number of spaces.
To use this module, you should have the "node-string-methods" npm package installed. You can install it using npm or yarn:
npm install node-string-methods
const ExpandTabs = require('node-string-methods/expandTabs');
// Create an instance of the ExpandTabs class with a string and optional tabWidth
const str = 'Expand\tthese\ttabs';
const tabWidth = 4;
const tabExpander = new ExpandTabs(str, tabWidth);
// Execute the tab expansion operation
const result = tabExpander.execute();
console.log(result); // Output: 'Expand these tabs'
str
(String): The input string that may contain tab characters ('\t').tabWidth
(Number, optional): The number of spaces to replace each tab character. Default is4
.
Creates a new instance of the ExpandTabs
class with the provided input string and tabWidth.
Expands tab characters ('\t') in the input string to the specified number of spaces.
- Returns: The string with tab characters expanded to spaces.
const ExpandTabs = require('node-string-methods/expandTabs');
const str = 'Expand\tthese\ttabs\twith\tcustom\ttab\twidth';
const tabWidth = 2;
const tabExpander = new ExpandTabs(str, tabWidth);
const result = tabExpander.execute();
console.log(result); // Output: 'Expand these tabs with custom tab width'
- Throws an
Error
if the input provided to the constructor is not a string.