UNPKG

pybuiltinfunc

Version:

Python built-in Functions and Modules in Javascript

225 lines (224 loc) 5.8 kB
class Str { constructor(str = "") { this.str = str; } capitalize() { return this.str.charAt(0).toUpperCase() + this.str.slice(1); } casefold() { return this.str.toLowerCase(); } center(width = 0, fillchar = " ") { let len = this.str.length; let pad = width - len; let padLeft = Math.floor(pad / 2); let padRight = Math.ceil(pad / 2); let result = ""; for (let i = 0; i < padLeft; i++) { result += fillchar; } result += this.str; for (let i = 0; i < padRight; i++) { result += fillchar; } return result; } count(value = "", start = 0, end = this.str.length - 1) { let str1 = this.str.slice(start, end); const valueRegex = new RegExp(value, "g"); const count = (str1.match(valueRegex) || []).length; return count; } endswith(suffix = "", start = 0, end = this.str.length - 1) { let str1 = this.str.slice(start, end); return str1.endsWith(suffix); } expandtabs(tabsize = 8) { return this.str.replace(/\t/g, " ".repeat(tabsize - 1)); } find(sub = "", start = 0, end = this.str.length - 1) { return this.str.slice(start, end).indexOf(sub); } return() { return this.str; } index(sub = "", start = 0, end = this.str.length - 1) { let str1 = this.str.slice(start, end); let isError = str1.indexOf(sub) !== -1; if (isError) { return str1.indexOf(sub); } else { throw new Error("substring not found"); } } isalnum() { return /^[a-zA-Z0-9]+$/.test(this.str); } isalpha() { return /^[a-zA-Z]+$/.test(this.str); } isdecimal() { return /^[0-9]+$/.test(this.str); } isidentifier() { return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(this.str); } isprintable() { return /^[\x20-\x7E]+$/.test(this.str); } isdigit() { return /^\d+$/.test(this.str); } islower() { return /^[a-z]+$/.test(this.str); } isspace() { return /^\s+$/.test(this.str); } istitle() { return /^[A-Z][a-z]+$/.test(this.str); } isupper() { return /^[A-Z]+$/.test(this.str); } join(iterable = []) { let result = ""; for (let i = 0; i < iterable.length; i++) { result += iterable[i]; } return result; } ljust(width = 0, fillchar = " ") { let len = this.str.length; let pad = width - len; let padLeft = Math.floor(pad / 2); let padRight = Math.ceil(pad / 2); let result = ""; for (let i = 0; i < padLeft; i++) { result += fillchar; } result += this.str; for (let i = 0; i < padRight; i++) { result += fillchar; } return result; } lower() { return this.str.toLowerCase(); } lstrip() { return this.str.replace(/^\s+/, ""); } partition(sep = "") { let index = this.str.indexOf(sep); let isError = index !== -1; if (isError) { let result = [ this.str.slice(0, index), sep, this.str.slice(index + sep.length), ]; return result; } else { throw new Error("substring not found"); } } replace(old = "", new_ = "", count = -1) { return this.str.replace(old, new_, count); } rfind(sub = "", start = 0, end = this.str.length - 1) { return this.str.slice(start, end).lastIndexOf(sub); } rindex(sub = "", start = 0, end = this.str.length - 1) { let str1 = this.str.slice(start, end); let isError = str1.lastIndexOf(sub) !== -1; if (isError) { return str1.lastIndexOf(sub); } else { throw new Error("substring not found"); } } rjust(width = 0, fillchar = " ") { let len = this.str.length; let pad = width - len; let padLeft = Math.floor(pad / 2); let padRight = Math.ceil(pad / 2); let result = ""; for (let i = 0; i < padLeft; i++) { result += fillchar; } result += this.str; for (let i = 0; i < padRight; i++) { result += fillchar; } return result; } rpartition(sep = "") { let index = this.str.lastIndexOf(sep); let isError = index !== -1; if (isError) { let result = [ this.str.slice(0, index), sep, this.str.slice(index + sep.length), ]; return result; } else { throw new Error("substring not found"); } } rsplit(sep = "", maxsplit = -1) { return this.str.split(sep, maxsplit); } rstrip() { return this.str.replace(/\s+$/, ""); } split(sep = "", maxsplit = -1) { return this.str.split(sep, maxsplit); } splitlines() { return this.str.split(/\r?\n/); } startswith(prefix = "", start = 0, end = this.str.length - 1) { return this.str.slice(start, end).startsWith(prefix); } strip() { return this.str.replace(/^\s+|\s+$/g, ""); } swapcase() { return this.str.replace(/[a-z]/g, function (c) { return c === c.toUpperCase() ? c.toLowerCase() : c.toUpperCase(); }); } title() { return this.str.replace(/\b\w/g, function (c) { return c.toUpperCase(); }); } translate(table = "") { let result = ""; for (let i = 0; i < this.str.length; i++) { result += table[this.str[i]]; } return result; } upper() { return this.str.toUpperCase(); } zfill(width = 0) { let len = this.str.length; let pad = width - len; let padLeft = Math.floor(pad / 2); let padRight = Math.ceil(pad / 2); let result = ""; for (let i = 0; i < padLeft; i++) { result += "0"; } result += this.str; for (let i = 0; i < padRight; i++) { result += "0"; } return result; } } module.exports.Str = Str;