UNPKG

@contextjs/system

Version:
42 lines (41 loc) 1.36 kB
import os from "node:os"; export class StringExtensions { static lineBreaks = ['\r', '\n', '\u0085', '\u2028', '\u2029']; static empty = ""; static newLine = os.EOL; static isNullOrEmpty(value) { return value === null || value === undefined || value === StringExtensions.empty; } static isNullOrUndefined(value) { return value === null || value === undefined; } static isNullOrWhiteSpace(value) { return this.isNullOrEmpty(value) || value?.trim() === StringExtensions.empty; } static removeWhiteSpaces(value) { return value.replace(/\s/g, ""); } static isLineBreak(value) { return this.lineBreaks.includes(value); } static isDigit(character) { return /^\d$/.test(character); } static isLetter(character) { return /^\p{L}$/u.test(character); } static isLetterOrDigit(value) { return this.isLetter(value) || this.isDigit(value); } static isWhiteSpace(value) { return /^\s$/.test(value); } static containsOnlyLineBreaksAndSpaces(value) { return /^[\s\n\r\u0085\u2028\u2029]*$/.test(value); } static format(template, ...args) { return template.replace(/{(\d+)}/g, (match, index) => { return typeof args[index] !== 'undefined' ? args[index] : match; }); } }