Created
July 24, 2020 10:03
-
-
Save romanonthego/1a86a8411dda99aea6c65b6991de0991 to your computer and use it in GitHub Desktop.
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
const types: { | |
[key: string]: string; | |
} = { | |
y: 'years', | |
mo: 'months', | |
w: 'weeks', | |
d: 'days', | |
h: 'hours', | |
m: 'minutes', | |
s: 'seconds', | |
}; | |
export const parseDuration = (timePeriod: string) => { | |
const result = /^(?<_value>[\d]+)(?<_type>[\w]+)$/.exec(timePeriod)!; | |
const { _value, _type } = result!.groups!; | |
const type = types[_type]; | |
const value = Number(_value); | |
return `${value} ${type}` | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment