Advertisement
Lulunga

Classes 04. Length Limit

Oct 18th, 2019
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Stringer {
  2.     constructor(string, length) {
  3.         this.innerString = string;
  4.         this.innerLength = Number(length);
  5.     }
  6.     increase(length) {
  7.         this.innerLength += length;
  8.     }
  9.     decrease(length) {
  10.         this.innerLength = Math.max(this.innerLength - length, 0);
  11.     }
  12.     toString() {
  13.         if (this.innerString.length > this.innerLength) {
  14.             return `${this.innerString.substring(0, this.innerLength)}...`;
  15.         } else if (this.innerString.length <= this.innerLength) {
  16.             return this.innerString;
  17.         } else {
  18.             return `...`;
  19.         }
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement