Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function solve() {
- String.prototype.ensureStart = function (str) {
- if (!this.toString().startsWith(str)) { return str + this.toString(); }
- return this.toString();
- };
- String.prototype.ensureEnd = function (str) {
- if (!this.toString().endsWith(str)) { return this.toString() + str; }
- return this.toString();
- };
- String.prototype.isEmpty = function () { return this.toString().length === 0; };
- String.prototype.truncate = function (n) {
- let text = this.toString();
- if (text.length <= n) { return text; }
- else if (n < 4) { return '.'.repeat(n); }
- else {
- text = text.substring(0, n);
- if (text.includes(' ')) {
- let found = true;
- let newtextEnd = undefined;
- while (found) {
- let lastWhiteSpaceIndex = text.lastIndexOf(' ');
- const truncText = `${text.substring(0, lastWhiteSpaceIndex)}...`;
- if (truncText.length <= n) { found = false; newtextEnd = truncText; }
- else { text = text.substring(0, lastWhiteSpaceIndex); }
- }
- return newtextEnd;
- } else { const newtext = text.substring(0, n - 3); return `${newtext}...`; }
- }
- };
- String.format = function (string, ...args) {
- for (let i = 0; i < args.length; i++) {
- if (string.includes(`${i}`)) {
- string = string.replace(`{${i}}`, args[i]);
- }
- }
- return string;
- };
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement