Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class StringBuilder {
- constructor(string) {
- if (string !== undefined) {
- StringBuilder._vrfyParam(string);
- this._stringArray = Array.from(string);
- } else {
- this._stringArray = [];
- }
- }
- append(string) {
- StringBuilder._vrfyParam(string);
- for (let i = 0; i < string.length; i++) {
- this._stringArray.push(string[i]);
- }
- }
- prepend(string) {
- StringBuilder._vrfyParam(string);
- for (let i = string.length - 1; i >= 0; i--) {
- this._stringArray.unshift(string[i]);
- }
- }
- insertAt(string, startIndex) {
- StringBuilder._vrfyParam(string);
- this._stringArray.splice(startIndex, 0, ...string);
- }
- remove(startIndex, length) {
- this._stringArray.splice(startIndex, length);
- }
- static _vrfyParam(param) {
- if (typeof param !== 'string') throw new TypeError('Argument must be a string');
- }
- toString() {
- return this._stringArray.join('');
- }
- }
- module.exports = StringBuilder;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement