Skip to content

Instantly share code, notes, and snippets.

@romanonthego
Created July 24, 2020 10:03
Show Gist options
  • Save romanonthego/1a86a8411dda99aea6c65b6991de0991 to your computer and use it in GitHub Desktop.
Save romanonthego/1a86a8411dda99aea6c65b6991de0991 to your computer and use it in GitHub Desktop.
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