From d97a80bb397be235f192c5f1c0eda17ec3d3f862 Mon Sep 17 00:00:00 2001 From: Kato Twofold Date: Sat, 7 May 2022 14:02:55 +0300 Subject: [PATCH] First version of the working date formatter --- package.json | 4 ++-- src/index.ts | 17 ++++++++++++++--- tests/fullTest.ts | 9 +++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 tests/fullTest.ts diff --git a/package.json b/package.json index bd95069..7775b5d 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "dist/index.js", "scripts": { "build": "esbuild src/index.ts --bundle --minify --sourcemap --target=chrome58,firefox57,safari11,edge16 --outfile=dist/tyme.js", - "test": "tsc" + "test": "tsc" }, "keywords": [ "time", @@ -18,4 +18,4 @@ "repository": { "url": "https://github.com/JustKato/Tyme.js" } -} +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index cb5a27b..04713ea 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, monthType, yearType } from "./Types/formatTypes"; +import { commonDateFormats, dateType, isDateType, isMonthType, isYearType, monthType, yearType } from "./Types/formatTypes"; /** @@ -45,7 +45,7 @@ export default class TymeJS { * @param dateTimeFormat * @returns The date formatted in the provided dateTimeFormat */ - public format(dateTimeFormat: string | commonDateFormats): string { + public format(dateTimeFormat: commonDateFormats | string ): string { /** * The formatted date */ @@ -53,7 +53,18 @@ export default class TymeJS { // Go through all of the characters of the dateTimeFormat string for ( let char of Array.from(dateTimeFormat) ) { - + // Check which one of the formatters can handle this + if ( isDateType(char) ) + formattedDate += this.getDate(char); + else if ( isMonthType(char) ) + formattedDate += this.getMonth(char); + else if ( isYearType(char) ) + formattedDate += this.getYear(char); + else { + // Just append the char to the result + formattedDate += char; + } + } // Return the formatted date diff --git a/tests/fullTest.ts b/tests/fullTest.ts new file mode 100644 index 0000000..2ee48a8 --- /dev/null +++ b/tests/fullTest.ts @@ -0,0 +1,9 @@ +import TymeJS from "../src"; + +const a = new TymeJS(); + +console.log(a.format("d/m/Y")); +console.log(a.format("d/m/Y h:i A")); +console.log(a.format("Y-m-d")); +console.log(a.format("Y-m-d H:i:s")); +