$ npm install lightsb
Here is a quick demo of all available functions:
const capacity: number = 7;
const builder: IStringBuilder = new StringBuilder("content", capacity);
builder.setCapacity(1024);
builder.append("first thing").append(420);
builder.appendRepeat("repeat 3 this times", 3);
builder.appendLine("new-line terminated")
builder.appendLineRepeat("3 lines here", 3);
builder.appendLines("line 1", "line 2", "...");
builder.appendEmptyLine();
builder.appendEmptyLines(42);
builder.setContent("content");
builder.clear();
const equals: boolean = builder.equals("other");
const isEmpty: boolean = builder.isEmpty();
const sub: string | undefined = builder.substring(0, 32);
const content: string | undefined = builder.toString();
Initialize a new string builder
constructor(content?: string, capacity?: number)
// initializes a new string builder with no content and no capacity limit
const builder: IStringBuilder = new StringBuilder();
// initializes a new string builder with "content" as content and no capacity limit
const builder: IStringBuilder = new StringBuilder("content");
// initializes a new string builder with "content" as content and a capacity of 42
// `capacity` can't be less than 0
const builder: IStringBuilder = new StringBuilder("content", 42);
Sets the string builder’s capacity
setCapacity(capacity: number | undefined): IStringBuilder;
// set capacity to 240
// if the builder was containing something, the overflow is removed
// `capacity` can't be less than 0
builder.setCapacity(240);
// remove capacity limit
builder.setCapacity();
Appends content
append(content: any): IStringBuilder;
// appends "content"
// if the new content exceeds the builder's capacity, throws an `OutOfRangeError`
builder.append("content");
Appends content count times
appendRepeat(content: any, count: number): IStringBuilder;
// appends "content" times
// if the new content exceeds the builder's capacity, throws an `OutOfRangeError`
// `count` can't be less than 0
builder.appendRepeat("content", 23);
Appends content as a line
appendLine(content: any): IStringBuilder;
// appends "content\n"
// if the new content exceeds the builder's capacity, throws an `OutOfRangeError`
builder.appendLine("content");
Appends content as lines count times
appendLineRepeat(content: any, count: number): IStringBuilder;
// appends "content\n" 4 times
// if the new content exceeds the builder's capacity, throws an `OutOfRangeError`
// `count` can't be less than 0
builder.appendLine("content", 4);
Appends content as lines
appendLines(...contents: any[]): IStringBuilder;
// appends "line 1\nline 2\n"
// if the new content exceeds the builder's capacity, throws an `OutOfRangeError`
builder.appendLines("line 1", "line 2");
Appends an empty new line
appendEmptyLine(): IStringBuilder;
// appends "\n"
// if the new content exceeds the builder's capacity, throws an `OutOfRangeError`
builder.appendEmptyLine();
Appends count empty new lines
appendEmptyLines(count: number): IStringBuilder;
// appends "\n\n\n"
// if the new content exceeds the builder's capacity, throws an `OutOfRangeError`
// `count` can't be less than 0
builder.appendEmptyLines(3);
Sets the content
setContent(content: any): IStringBuilder;
// sets the content to "content"
// if the new content exceeds the builder's capacity, throws an `OutOfRangeError`
builder.setContent("content");
Clears the content (sets to undefined)
clear(): IStringBuilder;
// clears the builder
builder.clear();
Checks if the content is equal to other
equals(other: string): boolean;
// checks if the content is equal to "other"
const result: boolean = builder.equals("other");
Checks if the content is empty
isEmpty(): boolean;
// checks if the content is empty (either undefined or 0 length)
const result: boolean = builder.isEmpty();
Returns the substring at start of length end (if provided), else, the rest of the content
substring(start: number, end?: number | undefined): string | undefined;
// get first two letters of the builder's content
const sub: string | undefined = builder.substring(0, 2);
Return the content as string
toString(): string | undefined;
// get the content of the builder
const content: string | undefined = builder.toString();
This project is MIT licensed.