Skip to content
Snippets Groups Projects
Verified Commit 798ac427 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat: new transformer commands #292

parent e95ee3d1
No related branches found
No related tags found
No related merge requests found
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>new transformer command for lists #292</title>
<script src="./292.mjs" type="module"></script>
</head>
<body>
<h1>new transformer command for lists #292</h1>
<p></p>
<ul>
<li><a href="https://gitlab.schukai.com/oss/libraries/javascript/monster/-/issues/292">Issue #292</a></li>
<li><a href="/">Back to overview</a></li>
</ul>
<main>
<p> look at the console for the output</p>
<!-- Write your code here -->
</main>
</body>
</html>
/**
* @file development/issues/open/292.mjs
* @url https://gitlab.schukai.com/oss/libraries/javascript/monster/-/issues/292
* @description new transformer command for lists
* @issue 292
*/
import "../../../source/components/style/property.pcss";
import "../../../source/components/style/link.pcss";
import "../../../source/components/style/color.pcss";
import "../../../source/components/style/theme.pcss";
import "../../../source/components/style/normalize.pcss";
import "../../../source/components/style/typography.pcss";
import {Transformer} from "../../../source/data/transformer.mjs";
const transformer = new Transformer("listtoarray");
console.log(transformer.run("1,2,3,4"));
const transformer2 = new Transformer("arraytolist");
console.log(transformer2.run([1,2,3,4]));
const transformer3 = new Transformer("default::array");
console.log(transformer3.run(undefined));
...@@ -23,6 +23,7 @@ import { ...@@ -23,6 +23,7 @@ import {
} from "../i18n/translations.mjs"; } from "../i18n/translations.mjs";
import { import {
validateFunction, validateFunction,
validateArray,
validateInteger, validateInteger,
validateObject, validateObject,
validatePrimitive, validatePrimitive,
...@@ -286,6 +287,30 @@ function transform(value) { ...@@ -286,6 +287,30 @@ function transform(value) {
validateInteger(n); validateInteger(n);
return n; return n;
case "to-array":
case "toarray":
if (isArray(value)) {
return value;
}
if (isObject(value)) {
return Object.values(value);
}
return [value];
case "listtoarray":
case "list-to-array":
validateString(value);
const listDel = args.shift() || ",";
return value.split(listDel);
case "arraytolist":
case "array-to-list":
validateArray(value);
const listDel2 = args.shift() || ",";
return value.join(listDel2);
case "to-json": case "to-json":
case "tojson": case "tojson":
return JSON.stringify(value); return JSON.stringify(value);
...@@ -398,7 +423,10 @@ function transform(value) { ...@@ -398,7 +423,10 @@ function transform(value) {
case "debug": case "debug":
if (isObject(console)) { if (isObject(console)) {
console.log(value); console.groupCollapsed("Transformer Debug");
console.log("Value", value);
console.log("Transformer", this);
console.groupEnd();
} }
return value; return value;
...@@ -581,6 +609,11 @@ function transform(value) { ...@@ -581,6 +609,11 @@ function transform(value) {
defaultValue === "true" || defaultValue === "true" ||
defaultValue === "true" defaultValue === "true"
); );
case "array":
if (defaultValue === "") {
return [];
}
return defaultValue.split(",");
case "string": case "string":
return `${defaultValue}`; return `${defaultValue}`;
case "object": case "object":
......
...@@ -29,7 +29,13 @@ import { ...@@ -29,7 +29,13 @@ import {
} from "./constants.mjs"; } from "./constants.mjs";
import { Base } from "../types/base.mjs"; import { Base } from "../types/base.mjs";
import {isArray, isInteger, isString, isInstance, isIterable} from "../types/is.mjs"; import {
isArray,
isInteger,
isString,
isInstance,
isIterable,
} from "../types/is.mjs";
import { Observer } from "../types/observer.mjs"; import { Observer } from "../types/observer.mjs";
import { ProxyObserver } from "../types/proxyobserver.mjs"; import { ProxyObserver } from "../types/proxyobserver.mjs";
import { validateArray, validateInstance } from "../types/validate.mjs"; import { validateArray, validateInstance } from "../types/validate.mjs";
...@@ -46,10 +52,8 @@ import {getWindow} from "./util.mjs"; ...@@ -46,10 +52,8 @@ import {getWindow} from "./util.mjs";
import { DeadMansSwitch } from "../util/deadmansswitch.mjs"; import { DeadMansSwitch } from "../util/deadmansswitch.mjs";
import { addErrorAttribute, removeErrorAttribute } from "./error.mjs"; import { addErrorAttribute, removeErrorAttribute } from "./error.mjs";
export { Updater, addObjectWithUpdaterToElement }; export { Updater, addObjectWithUpdaterToElement };
/** /**
* @private * @private
* @type {symbol} * @type {symbol}
...@@ -404,7 +408,6 @@ function retrieveAndSetValue(element) { ...@@ -404,7 +408,6 @@ function retrieveAndSetValue(element) {
const type = element.getAttribute(ATTRIBUTE_UPDATER_BIND_TYPE); const type = element.getAttribute(ATTRIBUTE_UPDATER_BIND_TYPE);
switch (type) { switch (type) {
case "integer?": case "integer?":
case "int?": case "int?":
case "number?": case "number?":
...@@ -426,12 +429,12 @@ function retrieveAndSetValue(element) { ...@@ -426,12 +429,12 @@ function retrieveAndSetValue(element) {
case "boolean": case "boolean":
case "bool": case "bool":
case "checkbox": case "checkbox":
value = value === "true" || value === "1" || value === "on" || value === true; value =
value === "true" || value === "1" || value === "on" || value === true;
break; break;
case "string[]": case "string[]":
if (isString(value)) { if (isString(value)) {
if (value.trim() === "") { if (value.trim() === "") {
value = []; value = [];
} else { } else {
...@@ -451,33 +454,34 @@ function retrieveAndSetValue(element) { ...@@ -451,33 +454,34 @@ function retrieveAndSetValue(element) {
case "int[]": case "int[]":
case "integer[]": case "integer[]":
if (isString(value)) { if (isString(value)) {
if (value.trim() === "") { if (value.trim() === "") {
value = []; value = [];
} else { } else {
value = value.split(",").map((v) => { value = value
.split(",")
.map((v) => {
try { try {
return parseInt(v, 10); return parseInt(v, 10);
} catch (e) { } catch (e) {}
}
return -1; return -1;
}).filter((v) => v !== -1); })
.filter((v) => v !== -1);
} }
} else if (isInteger(value)) { } else if (isInteger(value)) {
value = [value]; value = [value];
} else if (value === undefined || value === null) { } else if (value === undefined || value === null) {
value = []; value = [];
} else if (isArray(value)) { } else if (isArray(value)) {
value = value.split(",").map((v) => { value = value
.split(",")
.map((v) => {
try { try {
return parseInt(v, 10); return parseInt(v, 10);
} catch (e) { } catch (e) {}
}
return -1; return -1;
}).filter((v) => v !== -1); })
.filter((v) => v !== -1);
} else { } else {
throw new Error("unsupported value"); throw new Error("unsupported value");
} }
...@@ -486,7 +490,6 @@ function retrieveAndSetValue(element) { ...@@ -486,7 +490,6 @@ function retrieveAndSetValue(element) {
case "[]": case "[]":
case "array": case "array":
case "list": case "list":
if (isString(value)) { if (isString(value)) {
if (value.trim() === "") { if (value.trim() === "") {
value = []; value = [];
...@@ -505,7 +508,6 @@ function retrieveAndSetValue(element) { ...@@ -505,7 +508,6 @@ function retrieveAndSetValue(element) {
break; break;
case "object": case "object":
case "json": case "json":
if (isString(value)) { if (isString(value)) {
value = JSON.parse(value); value = JSON.parse(value);
} else { } else {
...@@ -735,7 +737,7 @@ function appendNewDocumentFragment(container, key, ref, path) { ...@@ -735,7 +737,7 @@ function appendNewDocumentFragment(container, key, ref, path) {
*/ */
function applyRecursive(node, key, path) { function applyRecursive(node, key, path) {
if (!(node instanceof HTMLElement)) { if (!(node instanceof HTMLElement)) {
return return;
} }
if (node.hasAttribute(ATTRIBUTE_UPDATER_REPLACE)) { if (node.hasAttribute(ATTRIBUTE_UPDATER_REPLACE)) {
...@@ -759,7 +761,6 @@ function applyRecursive(node, key, path) { ...@@ -759,7 +761,6 @@ function applyRecursive(node, key, path) {
applyRecursive(child, key, path); applyRecursive(child, key, path);
} }
} }
} }
/** /**
...@@ -1051,13 +1052,16 @@ function addObjectWithUpdaterToElement(elements, symbol, object, config = {}) { ...@@ -1051,13 +1052,16 @@ function addObjectWithUpdaterToElement(elements, symbol, object, config = {}) {
if (typeof callback === "function") { if (typeof callback === "function") {
updaterCallbacks.push([name, callback]); updaterCallbacks.push([name, callback]);
} else { } else {
addErrorAttribute(this, `onUpdaterPipeCallbacks: ${name} is not a function`); addErrorAttribute(
this,
`onUpdaterPipeCallbacks: ${name} is not a function`,
);
} }
} }
} else { } else {
addErrorAttribute( addErrorAttribute(
this, this,
`onUpdaterPipeCallbacks do not return an object with functions` `onUpdaterPipeCallbacks do not return an object with functions`,
); );
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment