Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (() => {
- String.prototype.ensureStart = function(str = '') {
- if (!this.startsWith(str)) {
- return str + this;
- }
- return this.toString();
- }
- String.prototype.ensureEnd = function(str = '') {
- if (!this.endsWith(str)) {
- return this + str;
- }
- return this.toString();
- }
- String.prototype.isEmpty = function() {
- return this.length == 0;
- }
- String.prototype.truncate = function(n) {
- if (n < 4) {
- return '.'.repeat(n);
- }
- if (this.length < n) {
- return this.toString;
- }
- let foundedSpace = this.lastIndexOf(' ');
- if (foundedSpace != -1) {
- return str.substring(0,foundedSpace) + '...';
- } else {
- return str.substring(0, n - 3 ) + '...';
- }
- }
- String.format = function(str, ...params) {
- params.forEach((key, index) => {
- str = str.replace(`{${index}}`, key);
- })
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment