Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function Extend() {
- String.prototype.ensureStart = function (str) {
- let startsWith = true;
- for (let i = 0; i < str.length; i++) {
- startsWith = str[i] === this[i];
- }
- return startsWith ? '' + this : str + this;
- };
- String.prototype.ensureEnd = function (str) {
- let endsWith = true;
- for (let i = 0; i < str.length; i++) {
- endsWith = str[str.length - 1 - i] === this[this.length - 1 - i];
- }
- return endsWith ? '' + this : this + str;
- };
- String.prototype.isEmpty = function () {
- return str === '';
- };
- String.prototype.truncate = function (n) {
- if (this.length <= n){
- return this + "";
- }else {
- if (!/\s+/.test(this)){
- if (n < 4){
- return ".".repeat(n);
- }
- return this.slice(0, n - 3) + "...";
- }else {
- if (n < 4) {
- return ".".repeat(n);
- }
- return this.slice(0, n - 3) + "...";
- }
- }
- };
- String.prototype.format = function (str1, str2, str3) {
- if (str2 !== '' || str2 !== undefined){
- str1.replace('{0}', str2);
- }
- if (str3 !== '' || str3 !== undefined){
- str1.replace('{1}', str3);
- }
- return str1;
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement