Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/string/stringf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,35 @@ export class stringf {
static numeric(x:string):string {
return x && x.replace(/[^0-9\.]/gi,'');
}
}

/**
* Strip characters from the left side of a string
*/
static lstrip(value: string, characters: string[] = [' ']): string {
const charSet = new Set(characters);
let startIndex: number = 0;
while (charSet.has(value[startIndex])) {
startIndex++;
}
return value.slice(startIndex);
}

/**
* Strip characters from the right side of a string
*/
static rstrip(value: string, characters: string[] = [' ']): string {
const charSet = new Set(characters);
let endIndex: number = value.length;
while (charSet.has(value[endIndex - 1])) {
endIndex--;
}
return value.slice(0, endIndex);
}

/**
* Strip characters from the left and right side of a string
*/
static trim(value: string, characters: string[] = [' ']): string {
return stringf.lstrip(stringf.rstrip(value, characters), characters);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this just value.trim()?

}
}