UNPKG

@full-pack/string-builder

Version:

A lightweight and versatile String Builder Class for Node.js & Browser.

9 lines (8 loc) 7.82 kB
/** * @full-pack/string-builder v2.0.0 * Copyright (c) 2024 Ram Amoncar <ramamonkar444@gmail.com> * @license MIT */ "use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:!0})},__copyProps=(to,from,except,desc)=>{if(from&&typeof from=="object"||typeof from=="function")for(let key of __getOwnPropNames(from))!__hasOwnProp.call(to,key)&&key!==except&&__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable});return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:!0}),mod);var index_exports={};__export(index_exports,{ChainableStringBuilder:()=>ChainableStringBuilder,StringBuilder:()=>StringBuilder,default:()=>index_default});module.exports=__toCommonJS(index_exports);var StringBuilder=class _StringBuilder{constructor(str,capacity){this._str=String(str!=null?str:""),typeof capacity=="number"&&isFinite(capacity)&&capacity>-1?(this._cap=capacity,this._str=this._str.slice(void 0,capacity)):this._cap=-1}insert(index,value,repeatCount=1){if(index>-1&&index<this._str.length){value=value.toString();let operating=[this._str.slice(void 0,index)];for(;repeatCount>0;repeatCount--)operating.push(value);operating.push(this._str.slice(index));let insertion=operating.join("");return this.checkCapacity(insertion.length)?(this._str=insertion,!0):!1}else throw new RangeError("Invalid Index")}append(value,repeatCount=1){value=value.toString();let operating=[this._str];for(;repeatCount>0;repeatCount--)operating.push(value);let appended=operating.join("");return this.checkCapacity(appended.length)?(this._str=appended,!0):!1}prepend(value,repeatCount=1){value=value.toString();let operating=[];for(;repeatCount>0;repeatCount--)operating.push(value);operating.push(this._str);let prepended=operating.join("");return this.checkCapacity(prepended.length)?(this._str=prepended,!0):!1}appendNewLine(num=1){let operating=[this._str];for(;num>0;num--)operating.push(` `);let appended=operating.join("");return this.checkCapacity(appended.length)?(this._str=appended,!0):!1}prependNewLine(num=1){let operating=[];for(;num>0;num--)operating.push(` `);operating.push(this._str);let prepended=operating.join("");return this.checkCapacity(prepended.length)?(this._str=prepended,!0):!1}appendSpace(num=1){let operating=[this._str];for(;num>0;num--)operating.push(" ");let appended=operating.join("");return this.checkCapacity(appended.length)?(this._str=appended,!0):!1}prependSpace(num=1){let operating=[];for(;num>0;num--)operating.push(" ");operating.push(this._str);let prepended=operating.join("");return this.checkCapacity(prepended.length)?(this._str=prepended,!0):!1}appendArray(arr,separator=""){let appended=this._str.concat(arr.join(separator));return this.checkCapacity(appended.length)?(this._str=appended,!0):!1}prependArray(arr,separator=""){let prepended=arr.join(separator).concat(this._str);return this.checkCapacity(prepended.length)?(this._str=prepended,!0):!1}appendJSON(json,space){let operating=[this._str];Array.isArray(json)?json.forEach(value=>operating.push(JSON.stringify(value,void 0,space))):operating.push(JSON.stringify(json,void 0,space));let appended=operating.join("");return this.checkCapacity(appended.length)?(this._str=appended,!0):!1}prependJSON(json,space){let operating=[];Array.isArray(json)?json.forEach(value=>operating.push(JSON.stringify(value,void 0,space))):operating.push(JSON.stringify(json,void 0,space));let prepended=operating.join("")+this._str;return this.checkCapacity(prepended.length)?(this._str=prepended,!0):!1}appendCodePoint(...codePoints){let appended=this._str+String.fromCodePoint(...codePoints);return this.checkCapacity(appended.length)?(this._str=appended,!0):!1}prependCodePoint(...codePoints){let prepended=String.fromCodePoint(...codePoints)+this._str;return this.checkCapacity(prepended.length)?(this._str=prepended,!0):!1}replaceSubstring(str,start,end){let replacementString="";if(start===void 0&&end===void 0)if(typeof str=="string")replacementString=str;else{let operating=[];for(let i=0;i<this._str.length;i++)operating.push(str(this._str[i],i,this._str));replacementString=operating.join("")}else if(start!=null||(start=0),end!=null||(end=this._str.length),typeof str=="string")replacementString=this._str.slice(void 0,start)+str+this._str.slice(end);else{let operating=[];for(let i=start;i<end;i++)operating.push(str(this._str[i],i,this._str));operating.push(this._str.slice(end)),replacementString=this._str.slice(void 0,start)+operating.join("")}return this.checkCapacity(replacementString.length)?(this._str=replacementString,!0):!1}setString(value){return this.checkCapacity(value.length)?(this._str=value,!0):!1}checkCapacity(newStrLen){return this._cap===-1||this._cap>=newStrLen}getString(){return this._str}indexOf(searchString,position){return this._str.indexOf(searchString,position)}lastIndexOf(searchString,position){return this._str.lastIndexOf(searchString,position)}setChar(index,char){let removed="";return(char.length===1||char.length===0)&&(removed=this._str.charAt(index),this._str=this._str.slice(void 0,index)+char+this._str.slice(index+1)),removed}substring(start,end){return this._str.substring(start,end)}trim(){this._str=this._str.trim()}clear(){this._str=""}reset(){this._str="",this._cap=-1}clone(){return new _StringBuilder(this._str,this._cap)}isFull(){return this._cap!==-1&&this._cap===this._str.length}isEmpty(){return this._str.length===0}get capacity(){return this._cap}set capacity(value){typeof value=="number"&&isFinite(value)&&value>-1?(this._cap=value,this._str=this._str.slice(void 0,value)):this._cap=-1}get length(){return this._str.length}set length(value){typeof value=="number"&&isFinite(value)&&value>-1&&(this._str=this._str.slice(void 0,value))}};var ChainableStringBuilder=class _ChainableStringBuilder{constructor(str,capacity){this.UNSET_VALUE=-1;this.sb=new StringBuilder(str!=null?str:"",capacity!=null?capacity:this.UNSET_VALUE)}static fromStringBuilder(sb){return new _ChainableStringBuilder(sb.getString(),sb.capacity)}insert(index,value,repeatCount=1){return this.sb.insert(index,value,repeatCount),this}append(value,repeatCount=1){return this.sb.append(value,repeatCount),this}prepend(value,repeatCount=1){return this.sb.prepend(value,repeatCount),this}appendNewLine(num=1){return this.sb.appendNewLine(num),this}prependNewLine(num=1){return this.sb.prependNewLine(num),this}appendSpace(num=1){return this.sb.appendSpace(num),this}prependSpace(num=1){return this.sb.prependSpace(num),this}appendArray(arr,separator=""){return this.sb.appendArray(arr,separator),this}prependArray(arr,separator=""){return this.sb.prependArray(arr,separator),this}appendJSON(json,space){return this.sb.appendJSON(json,space),this}prependJSON(json,space){return this.sb.prependJSON(json,space),this}appendCodePoint(...codePoints){return this.sb.appendCodePoint(...codePoints),this}prependCodePoint(...codePoints){return this.sb.prependCodePoint(...codePoints),this}replaceSubstring(str,start,end){return this.sb.replaceSubstring(str,start,end),this}setString(value){return this.sb.setString(value),this}trim(){return this.sb.trim(),this}clear(){return this.sb.clear(),this}reset(){return this.sb.reset(),this}clone(){return new _ChainableStringBuilder(this.sb.getString(),this.sb.capacity)}build(){return this.sb}getString(){return this.sb.getString()}get capacity(){return this.sb.capacity}set capacity(value){this.sb.capacity=value}get length(){return this.sb.length}set length(value){this.sb.length=value}};var index_default=StringBuilder;0&&(module.exports={ChainableStringBuilder,StringBuilder});