From 9fbc4e83837dbcdce3c5ba4f2825c95f3ea1de10 Mon Sep 17 00:00:00 2001 From: Maciej Lenartowicz Date: Thu, 20 Feb 2020 14:30:28 +0100 Subject: [PATCH] Added date and time utilities. --- package-lock.json | 2 +- package.json | 2 +- src/index.js | 1 + src/testUtils/setup.js | 2 +- src/utils/__tests__/datetime.test.js | 51 ++++++++++++++++++++++++++++ src/utils/datetime.js | 8 +++++ 6 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 src/utils/__tests__/datetime.test.js create mode 100644 src/utils/datetime.js diff --git a/package-lock.json b/package-lock.json index 86e6b91..e9da0e7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "foris", - "version": "4.0.0", + "version": "4.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 59b19b5..7c52b40 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "foris", - "version": "4.0.0", + "version": "4.1.0", "description": "Set of components and utils for Foris and its plugins.", "author": "CZ.NIC, z.s.p.o.", "repository": { diff --git a/src/index.js b/src/index.js index afe60fa..727cfda 100644 --- a/src/index.js +++ b/src/index.js @@ -64,6 +64,7 @@ export { } from "./utils/conditionalHOCs"; export { ErrorMessage } from "./utils/ErrorMessage"; export { useClickOutside } from "./utils/hooks"; +export { toLocaleDateString } from "./utils/datetime"; // Foris URL export { ForisURLs, REFORIS_URL_PREFIX } from "./forisUrls"; diff --git a/src/testUtils/setup.js b/src/testUtils/setup.js index 5aac2eb..4cc46dc 100644 --- a/src/testUtils/setup.js +++ b/src/testUtils/setup.js @@ -17,7 +17,7 @@ global.afterEach(() => { global._ = (str) => str; global.ngettext = (str) => str; global.babel = { format: (str) => str }; -global.ForisTranslations = {}; +global.ForisTranslations = { locale: "en" }; // Mock web sockets window.WebSocket = jest.fn(); diff --git a/src/utils/__tests__/datetime.test.js b/src/utils/__tests__/datetime.test.js new file mode 100644 index 0000000..b59478a --- /dev/null +++ b/src/utils/__tests__/datetime.test.js @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/) + * + * This is free software, licensed under the GNU General Public License v3. + * See /LICENSE for more information. + */ + +import { toLocaleDateString } from "../datetime"; + +describe("toLocaleDateString", () => { + it("should work with different locale", () => { + global.ForisTranslations = { locale: "fr" }; + expect( + toLocaleDateString("2020-02-20T12:51:36+00:00") + ).toBe("20 février 2020 12:51"); + global.ForisTranslations = { locale: "en" }; + }) + + it("should convert with default format", () => { + expect( + toLocaleDateString("2020-02-20T12:51:36+00:00") + ).toBe("February 20, 2020 12:51 PM"); + }); + + it("should convert with custom input format", () => { + expect( + toLocaleDateString( + "2020-02-20 12:51:36 +0000", + { inputFormat: "YYYY-MM-DD HH:mm:ss Z" }, + ) + ).toBe("February 20, 2020 12:51 PM"); + }); + + it("should convert with custom output format", () => { + expect( + toLocaleDateString( + "2020-02-20T12:51:36+00:00", + { outputFormat: "LL" }, + ) + ).toBe("February 20, 2020"); + }); + + it("should convert with custom input and output format", () => { + expect( + toLocaleDateString( + "2020-02-20 12:51:36 +0000", + { inputFormat: "YYYY-MM-DD HH:mm:ss Z", outputFormat: "LL" }, + ) + ).toBe("February 20, 2020"); + }); +}); diff --git a/src/utils/datetime.js b/src/utils/datetime.js new file mode 100644 index 0000000..f7e30b8 --- /dev/null +++ b/src/utils/datetime.js @@ -0,0 +1,8 @@ +import moment from "moment"; + +export function toLocaleDateString(date, { inputFormat, outputFormat = "LLL" } = {}) { + const parsedDate = inputFormat ? moment(date, inputFormat) : moment(date); + return parsedDate + .locale(ForisTranslations.locale) + .format(outputFormat); +}