Skip to content

Commit

Permalink
Shrink size futher (thanks @adriantombu!), add JSDOC for better DX
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoni Kepinski committed Aug 24, 2020
1 parent ac2d767 commit 3c8aaac
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
/**
* Use this API for simple, most common formatting.
* @param {Date} date - Date object, which should be used.
* @param {string} exp - String, which you want to format, for example: {yyyy}-{MM}-{dd} or Current time: {hh}:{mm}:{ss}.
* @return {string} String with formatted date.
*/
export const format = (date: Date, exp: string): string => exp.replace(/{.*?}/g, key => {
switch (key) {
case '{yyyy}':
return `${date.getFullYear()}`;
case '{yy}':
return `${date.getFullYear()}`.slice(-2);
case '{MM}':
return (date.getMonth() + 1).toString().padStart(2, '0');
return `${(date.getMonth() + 1)}`.padStart(2, '0');
case '{dd}':
return date.getDate().toString().padStart(2, '0');
return `${date.getDate()}`.padStart(2, '0');
case '{HH}':
return date.getHours().toString().padStart(2, '0');
return `${date.getHours()}`.padStart(2, '0');
case '{mm}':
return date.getMinutes().toString().padStart(2, '0');
return `${date.getMinutes()}`.padStart(2, '0');
case '{ss}':
return date.getSeconds().toString().padStart(2, '0');
return `${date.getSeconds()}`.padStart(2, '0');
case '{SSS}':
return `${date.getMilliseconds()}`.padStart(3, '0');
/* c8 ignore next 2 */
default:
return '';
}
});

/* c8 ignore next */
export {default as localeFormat} from './locale';

0 comments on commit 3c8aaac

Please # to comment.