lightsb

A simple light TypeScript string builder

Table of content

Installation

$ npm install lightsb

Usage

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();

Features

Constructor

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);

Set capacity

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();

Append content

Appends content

append(content: any): IStringBuilder;
// appends "content"
// if the new content exceeds the builder's capacity, throws an `OutOfRangeError`
builder.append("content");

Append content N times

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);

Append a new-line terminated content

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");

Append N new-line terminated contents

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);

Append new-line terminated contents

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");

Append a new line

Appends an empty new line

appendEmptyLine(): IStringBuilder;
// appends "\n"
// if the new content exceeds the builder's capacity, throws an `OutOfRangeError`
builder.appendEmptyLine();

Append N new lines

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);

Set content

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");

Clear

Clears the content (sets to undefined)

clear(): IStringBuilder;
// clears the builder
builder.clear();

Compare

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");

Check emptiness

Checks if the content is empty

isEmpty(): boolean;
// checks if the content is empty (either undefined or 0 length)
const result: boolean = builder.isEmpty();

Substring

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);

Get content

Return the content as string

toString(): string | undefined;
// get the content of the builder
const content: string | undefined = builder.toString();

License

This project is MIT licensed.