From 04dc772e05bee0845fe1282b09cdfde6f71cb334 Mon Sep 17 00:00:00 2001 From: Kato Twofold Date: Sat, 7 May 2022 14:28:26 +0300 Subject: [PATCH] + Seconds Handler --- src/Types/formatTypes.ts | 2 +- src/index.ts | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Types/formatTypes.ts b/src/Types/formatTypes.ts index d4d89cd..fc600d9 100644 --- a/src/Types/formatTypes.ts +++ b/src/Types/formatTypes.ts @@ -35,7 +35,7 @@ const secondtypes = [ "s" ]; /** * Possible formats for the date */ -export type secondType = (typeof secondtypes)[number]; +export type secondType = "s"; export const isSecondType = (x: any): x is secondType => secondtypes.includes(x); diff --git a/src/index.ts b/src/index.ts index 97f3f21..938a52b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { dayDictionary } from "./Dictionaries/dateDictionary"; import { monthDictionary } from "./Dictionaries/monthDictionary"; -import { commonDateFormats, dateType, hourType, isDateType, isHourType, isMeridiemType, isMinuteType, isMonthType, isYearType, meridiemType, minuteType, monthType, yearType } from "./Types/formatTypes"; +import { commonDateFormats, dateType, hourType, isDateType, isHourType, isMeridiemType, isMinuteType, isMonthType, isSecondType, isYearType, meridiemType, minuteType, monthType, secondType, yearType } from "./Types/formatTypes"; /** @@ -70,6 +70,8 @@ export default class TymeJS { formattedDate += this.getHour(char); else if ( isMinuteType(char) ) formattedDate += this.getMinutes(char); + else if ( isSecondType(char) ) + formattedDate += this.getSeconds(char); else { // Just append the char to the result formattedDate += char; @@ -115,7 +117,7 @@ export default class TymeJS { return String(h).padStart(2, "0"); } - throw new Error(`Invalid format for Hour ${t}`); + throw new Error(`Invalid format for Hours ${t}`); } public getMinutes(t: minuteType ): string { @@ -124,7 +126,15 @@ export default class TymeJS { return String(this.ts.getMinutes()).padStart(2, "0"); } - throw new Error(`Invalid format for Minute ${t}`); + throw new Error(`Invalid format for Minutes ${t}`); + } + + public getSeconds(t: secondType) { + if ( t === "s" ) { + return String(this.ts.getSeconds()).padStart(2, "0"); + } + + throw new Error(`Invalid format for Seconds ${t}`); }