export const format = Symbol('format'); function formatImpl(this: string, ...replaceArgs: Array) { var args = arguments; return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) { if (m == "{{") { return "{"; } if (m == "}}") { return "}"; } return args[n]; }); }; export const startsWith = Symbol('startsWith'); function startsWithImpl(this: string, str: string) { return this.indexOf(str) == 0; }; export const endsWith = Symbol('endsWith'); function endsWithImpl(this: string, suffix: string) { return this.indexOf(suffix, this.length - suffix.length) !== -1; }; export const capitalize = Symbol('capitalize'); function capitalizeImpl(this: string) { { if (this != null) { if (this.length > 1) { return this.charAt(0).toUpperCase() + this.slice(1); } else { return this.toUpperCase(); } } else { return null; } } } export const latinize = Symbol('latinize'); function latinizeImpl(this: string) { { try { return (this).normalize('NFD').replace(/[\u0300-\u036f]/g, ""); } catch (e) { return this as any; } }; } declare global { export interface String { /** * Replaces one or more format items in the string with the string specification of specified object * * @param replaceArgs Format objects */ [format]: typeof formatImpl; /** * Determines if current string starts with given string * * @param str String that should be checked */ [startsWith]: typeof startsWithImpl; /** * Determines if current string ends with given string * * @param str String that should be checked */ [endsWith]: typeof endsWithImpl; /** * Returns capitalized string (with leading letter ensured in capitals) * * @param str String that should be capitalized */ [capitalize]: typeof capitalizeImpl /** * Returns latinized string (with local accents replaced with ASCII ones) * * @param str String that should be latinized */ [latinize]: typeof latinizeImpl } } (String as any).prototype[format] = formatImpl; (String as any).prototype[startsWith] = startsWithImpl; (String as any).prototype[endsWith] = endsWithImpl; (String as any).prototype[capitalize] = capitalizeImpl; (String as any).prototype[latinize] = latinizeImpl;