From 910c9388788b8f5ccb1633db9e466b70d2698e10 Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Wed, 24 May 2023 18:52:25 +0200 Subject: [PATCH] feat: new datetimeformat --- application/source/data/transformer.mjs | 37 +++++++++++---- .../playground/custom-element/index.html | 12 +++++ .../playground/custom-element/main.mjs | 46 +++++++++++++++++++ .../playground/custom-element/main.pcss | 0 development/test/cases/data/transformer.mjs | 3 ++ 5 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 development/playground/custom-element/index.html create mode 100644 development/playground/custom-element/main.mjs create mode 100644 development/playground/custom-element/main.pcss diff --git a/application/source/data/transformer.mjs b/application/source/data/transformer.mjs index c0cafd468..a7cfa53ee 100644 --- a/application/source/data/transformer.mjs +++ b/application/source/data/transformer.mjs @@ -5,12 +5,12 @@ * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html */ -import { getLocaleOfDocument } from "../dom/locale.mjs"; -import { Base } from "../types/base.mjs"; -import { getGlobal, getGlobalObject } from "../types/global.mjs"; -import { ID } from "../types/id.mjs"; -import { isArray, isObject, isString, isPrimitive } from "../types/is.mjs"; -import { getDocumentTranslations, Translations } from "../i18n/translations.mjs"; +import {getLocaleOfDocument} from "../dom/locale.mjs"; +import {Base} from "../types/base.mjs"; +import {getGlobal, getGlobalObject} from "../types/global.mjs"; +import {ID} from "../types/id.mjs"; +import {isArray, isObject, isString, isPrimitive} from "../types/is.mjs"; +import {getDocumentTranslations, Translations} from "../i18n/translations.mjs"; import { validateFunction, validateInteger, @@ -19,10 +19,10 @@ import { validateString, validateBoolean, } from "../types/validate.mjs"; -import { clone } from "../util/clone.mjs"; -import { Pathfinder } from "./pathfinder.mjs"; +import {clone} from "../util/clone.mjs"; +import {Pathfinder} from "./pathfinder.mjs"; -export { Transformer }; +export {Transformer}; /** * The transformer class is a swiss army knife for manipulating values. especially in combination with the pipe, processing chains can be built up. @@ -673,6 +673,25 @@ function transform(value) { throw new Error(`unsupported locale or missing format (${e.message})`); } + case "datetimeformat": + + date = new Date(value); + if (isNaN(date.getTime())) { + throw new Error("invalid date"); + } + + const options = { + dateStyle: args.shift() || "medium", + timeStyle: args.shift() || "medium", + }; + + try { + locale = getLocaleOfDocument(); + return new Intl.DateTimeFormat(locale, options).format(date) + } catch (e) { + throw new Error(`unsupported locale or missing format (${e.message})`); + } + case "datetime": date = new Date(value); if (isNaN(date.getTime())) { diff --git a/development/playground/custom-element/index.html b/development/playground/custom-element/index.html new file mode 100644 index 000000000..1826cb71d --- /dev/null +++ b/development/playground/custom-element/index.html @@ -0,0 +1,12 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>Popper Button</title> + <script src="main.mjs" type="module"></script> +</head> +<body id="mybody"> + <h1>Monday</h1> + <monster-monday data-monster-script-host="mybody" id="monday" data-monster-option-a-b-c="ddddddd"></monster-monday> +</body> diff --git a/development/playground/custom-element/main.mjs b/development/playground/custom-element/main.mjs new file mode 100644 index 000000000..2b4862532 --- /dev/null +++ b/development/playground/custom-element/main.mjs @@ -0,0 +1,46 @@ +import {CustomElement,registerCustomElement} from "../../../application/source/dom/customelement.mjs" + + +class Monday extends CustomElement { + +static getTag() { + return "monster-monday" +} + + get defaults() { + return Object.assign({}, super.defaults, { + templates: { + main: myHtml() + }, + a : { + b : { + + c:"hallo" + } + + }})} + + +} + +function myHtml() { + + return '<div data-monster-replace="path:a.b.c "></div>' + +} + +const o=document.querySelector("monster-monday") +o.initCustomControlCallback = (elem) => { + +elem.setOption("a.b.c","xxxx") + + +} + + + +registerCustomElement(Monday) + + + + diff --git a/development/playground/custom-element/main.pcss b/development/playground/custom-element/main.pcss new file mode 100644 index 000000000..e69de29bb diff --git a/development/test/cases/data/transformer.mjs b/development/test/cases/data/transformer.mjs index 559bea5de..6ad0d73c5 100644 --- a/development/test/cases/data/transformer.mjs +++ b/development/test/cases/data/transformer.mjs @@ -28,6 +28,9 @@ describe('Transformer', function () { describe('Transformer.run()', function () { [ + ['datetimeformat', "2023-02-04 08:02:01", "04.02.2023, 08:02:01"], + ['datetimeformat:long:short', "2023-02-04 08:02:01","4. Februar 2023 um 08:02"], + ['datetimeformat:short:short', "2023-02-04 08:02:01", "04.02.23, 08:02"], ['equals:a', "a", true], ['equals:a', "b", false], ['equals:3', 3, true], -- GitLab