From 3c0a0c1f92afcab5e416a845e9b3a88e41d08522 Mon Sep 17 00:00:00 2001 From: Jonathan Casey Date: Tue, 3 Oct 2023 15:45:56 +0100 Subject: [PATCH] feat(dcs): refactor apply decorator logic Signed-off-by: Jonathan Casey --- .../concerto-core/lib/decoratormanager.js | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/packages/concerto-core/lib/decoratormanager.js b/packages/concerto-core/lib/decoratormanager.js index 81b841b46..63600ee14 100644 --- a/packages/concerto-core/lib/decoratormanager.js +++ b/packages/concerto-core/lib/decoratormanager.js @@ -238,6 +238,27 @@ class DecoratorManager { } } + + /** + * Applies a new decorator to the Map element + * @private + * @param {string} element the value to test + * @param {string} target the command target + * @param {*} declaration the map declaration + * @param {string} type the command type + * @param {*} newDecorator the decorator to add + */ + static applyDecoaratorForMapElement(element, target, declaration, type, newDecorator ) { + const decl = element.toLowerCase() === 'key' ? declaration.key : declaration.value; + if (target.type) { + if (this.falsyOrEqual(target.type, decl.$class)) { + this.applyDecorator(decl, type, newDecorator); + } + } else { + this.applyDecorator(decl, type, newDecorator); + } + } + /** * Compares two arrays. If the first argument is falsy * the function returns true. @@ -305,35 +326,14 @@ class DecoratorManager { if (target.mapElement) { switch(target.mapElement.toLowerCase()) { case 'key': - if (target.type) { - if (this.falsyOrEqual(target.type, declaration.key.$class)) { - this.applyDecorator(declaration.key, type, decorator); - } - } else { - this.applyDecorator(declaration.key, type, decorator); - } + this.applyDecoaratorForMapElement(target.mapElement, target, declaration, type, decorator); break; case 'value': - if (target.type) { - if (this.falsyOrEqual(target.type, declaration.value.$class)) { - this.applyDecorator(declaration.value, type, decorator); - } - } else { - this.applyDecorator(declaration.value, type, decorator); - } + this.applyDecoaratorForMapElement(target.mapElement, target, declaration, type, decorator); break; case '*': - if (target.type) { - if (this.falsyOrEqual(target.type, declaration.key.$class)) { - this.applyDecorator(declaration.key, type, decorator); - } - if (this.falsyOrEqual(target.type, declaration.value.$class)) { - this.applyDecorator(declaration.value, type, decorator); - } - } else { - this.applyDecorator(declaration.key, type, decorator); - this.applyDecorator(declaration.value, type, decorator); - } + this.applyDecoaratorForMapElement('key', target, declaration, type, decorator); + this.applyDecoaratorForMapElement('value', target, declaration, type, decorator); break; default: