using typescript 2.3.4 in an Ionic 3 Angular 4 application I create a file called stringExtensions.ts and put it in it
export { } // to make it a module declare global { // to access the global type String interface String { truncate(max: number, decorator: string): string; } } // then the actual code String.prototype.truncate = function(max, decorator){ decorator = decorator || '...'; return (this.length > max ? this.substring(0,max)+decorator : this); };
source share