Contains some helpful TypeScript functions.
Last active
May 18, 2018 07:52
-
-
Save bryandh/5d4beefd85d4f8203b02145e67573a8c to your computer and use it in GitHub Desktop.
Typescript general utilities
This file contains hidden or 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
| export class GeneralUtilities { | |
| /** | |
| * Retrieves the given function's argument names | |
| * @param func The function to retrieve the argument names from | |
| * @returns List of argument names | |
| */ | |
| public static getFunctionArgumentNames(func: () => any): string[] { | |
| // Retrieve the function's signature (first line in a stringified function) | |
| const signature = func.toString().split('\n')[0]; | |
| const args = signature | |
| .substring(signature.indexOf('(') + 1, signature.indexOf(')')) // Retrieve the args (between parantheses) | |
| .split(',') // Split on the argument separator ',' to separate the arguments | |
| .map((arg) => arg.trim()) // Trim any spaces surrounding arguments | |
| .filter((arg) => arg.length > 0); // Filter out any non-arguments, which are empty strings | |
| return args; | |
| } | |
| /** | |
| * Checks if the user agent is an Internet Explorer user agent | |
| */ | |
| public static userAgentIsIE(): boolean { | |
| return navigator.userAgent.indexOf('MSIE') !== -1 | |
| || navigator.appVersion.indexOf('Trident/') > 0 | |
| || /Trident\//ig.test(navigator.userAgent); | |
| } | |
| /** | |
| * Distance between coordinates formula. | |
| * Formula used from SO answer => https://stackoverflow.com/a/5548877/4795677 | |
| * The 111.2 value represents KM/degree of the earth, | |
| * see => https://www.thoughtco.com/degree-of-latitude-and-longitude-distance-4070616 | |
| * The 57.3 value represents roughly 180/pi. | |
| * @param originLatitude The origin latitude. | |
| * @param originLongitude The origin longitude. | |
| * @param destinationLatitude The destination latitude. | |
| * @param destinationLongitude The destination longitude. | |
| */ | |
| private calculateCoordinateDistance( | |
| originLatitude: number, | |
| originLongitude: number, | |
| destinationLatitude: number, | |
| destinationLongitude: number | |
| ): number { | |
| return Math.pow( | |
| 111.2 * (originLongitude - destinationLongitude) | |
| * Math.cos(destinationLatitude / 57.3), 2 | |
| ) + Math.pow(111.2 * (destinationLatitude - originLatitude), 2); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment