Skip to content
Snippets Groups Projects
Commit 6166c894 authored by michael.minelli's avatar michael.minelli
Browse files

Add TypeScript extension file (extensions TypeScript types)

parent ba196c87
No related branches found
No related tags found
No related merge requests found
declare global {
interface Array<T> {
removeObjectDuplicates: (getProperty: (item: T) => any) => Array<T>;
}
interface String {
toBoolean: () => boolean;
capitalizingFirstLetter: () => string;
capitalizeName: () => string;
}
}
function registerAll() {
registerBigIntJson();
registerArrayRemoveObjectDuplicates();
registerStringToBoolean();
registerStringCapitalizingFirstLetter();
registerStringCapitalizeName();
}
function registerBigIntJson() {
(BigInt.prototype as any).toJSON = function () {
return this.toString();
};
}
function registerArrayRemoveObjectDuplicates() {
Array.prototype.removeObjectDuplicates = function <T>(this: Array<T>, getProperty: (item: T) => any): Array<T> {
return this.reduce((accumulator: Array<T>, current: T) => {
if ( !accumulator.find((item: any) => getProperty(item) === getProperty(current)) ) {
accumulator.push(current);
}
return accumulator;
}, Array<T>());
};
}
function registerStringToBoolean() {
String.prototype.toBoolean = function (this: string): boolean {
const tmp = this.toLowerCase().trim();
return tmp === 'true' || tmp === '1';
};
}
function registerStringCapitalizingFirstLetter() {
String.prototype.capitalizingFirstLetter = function (this: string): string {
return this.charAt(0).toUpperCase() + this.slice(1);
};
}
function registerStringCapitalizeName() {
String.prototype.capitalizeName = function (this: string): string {
return this.trim().replace(/(?:^|\s|-)\S/g, s => s.toUpperCase());
};
}
registerAll();
export default null;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment