Last active
February 12, 2020 03:33
-
-
Save julianpitt/c8f43e0075ca8d147094595d306f1e8a to your computer and use it in GitHub Desktop.
[Data transforms]
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
| function pad(toPad, padBy = 2, padWith = '0') { | |
| return ('' + toPad).length < padBy ? `${pad(padWith + toPad, padBy, padWith)}` : toPad; | |
| } | |
| function secondsToHMS(totalSeconds) { | |
| var hours = totalSeconds / 3600 - totalSeconds % 3600 / 3600; | |
| var minutes = ((totalSeconds % 3600 / 3600) * 60) - ((totalSeconds % 3600 / 3600) * 60) % 1; | |
| var seconds = totalSeconds % 60; | |
| return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`; | |
| } |
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
| const getStandardDistanceinKM = (stringDist) => { | |
| const numDist = stringDist.match(/([0-9.]+)\s?(k?m)(\s|$)/i); | |
| if(!numDist) { | |
| return false | |
| } | |
| const [full, num, units] = numDist; | |
| let dist = num; | |
| if(units=='m'){ | |
| dist=dist/1000; | |
| } else if(units!='km') { | |
| throw new Error('no valid units'); | |
| } | |
| return dist; | |
| } | |
| console.log(getStandardDistanceinKM('12km')); // 12 | |
| console.log(getStandardDistanceinKM('12 km')) // 12 | |
| console.log(getStandardDistanceinKM('12.3km')) // 12.3 | |
| console.log(getStandardDistanceinKM('12.3 km')) //12.3 | |
| console.log(getStandardDistanceinKM('12.3 m')) // 0.0123 | |
| console.log(getStandardDistanceinKM('100m')) // 0.1 | |
| console.log(getStandardDistanceinKM('This bend trip 12km')); // 12km | |
| console.log(getStandardDistanceinKM('a lon km waltk 12 km trip')) //12 km | |
| console.log(getStandardDistanceinKM('Walk on the beack')) // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment