Last active
October 26, 2022 02:27
-
-
Save RodrigoNovais/7038e5ad7f196768b9e20ebb284afa6d to your computer and use it in GitHub Desktop.
Typescript extension methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare global { | |
interface Array<T> { | |
/** | |
* Creates an tuple of grouped elements limited by the length of the first array. | |
* @template U | |
* @param {U} list | |
*/ | |
zip<U>(list: U[]): [T, U][] | |
} | |
} | |
Array.prototype.zip = function<T, U>(this: T[], list: U[]): [T, U][] { | |
return this.map((v, k) => [v, list[k]]); | |
}; | |
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare global { | |
interface PolarCoordinates { angle: number, radius: number } | |
interface CartesianCoordinates { x: number, y: number} | |
interface Math { | |
randomRangeFloat(min: number, max: number): number; | |
randomRangeInt(min: number, max: number): number; | |
clamp(value: number, min: number, max: number): number; | |
clamp01(value: number): number; | |
toRad(deg: number): number; | |
toDeg(rad: number): number; | |
toPolar(coordinates: CartesianCoordinates): PolarCoordinates; | |
toCartesian(coordinates: PolarCoordinates): CartesianCoordinates; | |
percentageInRange(min: number, max: number, percent: number): number; | |
} | |
} | |
Math.randomRangeFloat = (min: number, max: number): number => { | |
return Math.random() * (max - min) + min; | |
}; | |
Math.randomRangeInt = (min: number, max: number): number => { | |
// min = Number.isInteger(min) ? min: Math.round(min); | |
// max = Number.isInteger(min) ? max: Math.round(max); | |
// if (Number.isInteger(min)) throw new Error("minimum value must be an integer"); | |
// if (Number.isInteger(min)) throw new Error("maximum value must be an integer"); | |
return Math.round(Math.randomRangeFloat(min, max)); | |
}; | |
Math.clamp = (value: number, min: number, max: number): number => { | |
return Math.min(Math.max(value, min), max); | |
}; | |
Math.clamp01 = (value: number): number => { | |
return Math.clamp(value, 0, 1); | |
}; | |
Math.toRad = (deg: number): number => { return deg * (Math.PI / 180); }; | |
Math.toDeg = (rad: number): number => { return rad * (180 / Math.PI); }; | |
Math.toPolar = (coordinates: CartesianCoordinates): PolarCoordinates => { | |
const { x, y } = coordinates; | |
const radius = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); | |
let angle = Math.atan(y / x); | |
if (x < 0 && y > 0) angle += Math.PI; | |
if (x < 0 && y < 0) angle += Math.PI; | |
if (x > 0 && y < 0) angle += Math.PI * 2; | |
return { radius, angle }; | |
}; | |
Math.toCartesian = (coordinates: PolarCoordinates): CartesianCoordinates => { | |
const { angle, radius } = coordinates; | |
const x = radius * Math.cos(angle); | |
const y = radius * Math.sin(angle); | |
return { x, y }; | |
}; | |
Math.percentageInRange = (min: number, max: number, percent: number): number => { | |
return (percent * (max - min) / 100) + min; | |
}; | |
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare global { | |
interface NumberConstructor { | |
/** | |
* Converts a string to a floating-point number. | |
* @param {string} string A string that contains a floating-point number. | |
* @param {string} defaultValue A value to be returned if the conversion does not succeed. | |
*/ | |
tryParseFloat(string: string, defaultValue?: number): number; | |
/** | |
* Attempt to convert a string to an integer. Returns a default value if the conversion does not succeed. | |
* @param {string} string A string to convert into a number. | |
* @param {number} defaultValue A value to be returned if the conversion does not succeed. | |
* @param {number} radix A value between 2 and 36 that specifies the base of the number in numString. | |
* If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. | |
* All other strings are considered decimal. | |
*/ | |
tryParseInt(string: string, defaultValue?: number, radix?: number): number; | |
} | |
interface Number { | |
/** | |
* Calculate percentual value over the current number | |
* | |
* @param {number} percent | |
*/ | |
percentage(percent: number): number; | |
} | |
} | |
Number.prototype.percentage = function(percent: number) { | |
return (this * percent) / 100; | |
}; | |
Number.tryParseFloat = (value: string, defaultValue = 0): number => { | |
try { | |
if (!value) return defaultValue; | |
if (!value.length) return defaultValue; | |
if (Number.isNaN(value)) return defaultValue; | |
return Number.parseFloat(value); | |
} catch (error) { console.error(error); } | |
return defaultValue; | |
}; | |
Number.tryParseInt = (value: string, defaultValue = 0, radix = 10): number => { | |
try { | |
if (!value) return defaultValue; | |
if (!value.length) return defaultValue; | |
if (Number.isNaN(value)) return defaultValue; | |
return Number.parseInt(value, radix); | |
} catch (error) { console.error(error); } | |
return defaultValue; | |
}; | |
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare global { | |
interface String { | |
/** | |
* Converts a string to [camel case](https://en.wikipedia.org/wiki/CamelCase). | |
*/ | |
toCamelCase(): string; | |
/** | |
* Converts a string to [pascal case](https://en.wikipedia.org/wiki/PascalCase). | |
*/ | |
toPascalCase(): string; | |
} | |
} | |
String.prototype.toCamelCase = function(this: string): string { | |
return this.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => { | |
return index === 0 ? word.toLowerCase() : word.toUpperCase(); | |
}).replace(/\s+/g, ''); | |
}; | |
String.prototype.toPascalCase = function(this: string): string { | |
return this.replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => word.toUpperCase()) | |
.replace(/\s+/g, ''); | |
}; | |
export {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment