From c1dc2ae461815cb12ec1cf5183b44e43b9e2b99b Mon Sep 17 00:00:00 2001 From: Pedro Lamas Date: Tue, 20 Aug 2024 15:45:54 +0100 Subject: [PATCH] refactor: handling of macro parameters "_" prefix Signed-off-by: Pedro Lamas --- src/components/widgets/macros/MacroBtn.vue | 2 +- src/util/__tests__/gcode-macro-params.spec.ts | 4 ++-- src/util/gcode-macro-params.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/widgets/macros/MacroBtn.vue b/src/components/widgets/macros/MacroBtn.vue index 9a7de484b2..bfb89ea596 100644 --- a/src/components/widgets/macros/MacroBtn.vue +++ b/src/components/widgets/macros/MacroBtn.vue @@ -155,7 +155,7 @@ export default class MacroBtn extends Mixins(StateMixin) { this.$set(this.params, 'message', { value: '', reset: '' }) } else { for (const { name, value } of gcodeMacroParams(this.macro.config.gcode)) { - if (!this.params[name]) { + if (!name.startsWith('_') && !this.params[name]) { this.$set(this.params, name, { value, reset: value }) } } diff --git a/src/util/__tests__/gcode-macro-params.spec.ts b/src/util/__tests__/gcode-macro-params.spec.ts index fcf0dfe9c7..5be03027d8 100644 --- a/src/util/__tests__/gcode-macro-params.spec.ts +++ b/src/util/__tests__/gcode-macro-params.spec.ts @@ -4,8 +4,8 @@ describe('gcodeMacroParams', () => { it.each([ ['{% set ENABLE = params.ENABLE %}', [{ name: 'ENABLE', value: '' }]], ['{% set ENABLE = params.ENABLE | default(1) %}', [{ name: 'ENABLE', value: '1' }]], - ['{% set ENABLE = params._ENABLE %}', []], - ['{% set ENABLE = params._ENABLE | default(1) %}', []] + ['{% set ENABLE = params._ENABLE %}', [{ name: '_ENABLE', value: '' }]], + ['{% set ENABLE = params._ENABLE | default(1) %}', [{ name: '_ENABLE', value: '1' }]] ])('Expects params of "%s", to be %s', (param, expected) => { expect(gcodeMacroParams(param)).toStrictEqual(expected) }) diff --git a/src/util/gcode-macro-params.ts b/src/util/gcode-macro-params.ts index 755e65ef8e..afaa577047 100644 --- a/src/util/gcode-macro-params.ts +++ b/src/util/gcode-macro-params.ts @@ -1,4 +1,4 @@ -const paramRegExp = /params\.([^_]\w*)(.*)/gi +const paramRegExp = /params\.(\w+)(.*)/gi const defaultValueRegExp = /\|\s*default\s*\(\s*((["'])(?:\\\2|.)*?\2|-?\d[^,)]*)/i export const gcodeMacroParamDefault = (param: string) => {