From 52538ede16e8956e208f6042ddaf5faa0069f629 Mon Sep 17 00:00:00 2001 From: Pedro Lamas Date: Tue, 27 Aug 2024 09:51:15 +0100 Subject: [PATCH] refactor: regular expressions Signed-off-by: Pedro Lamas --- src/components/widgets/console/ConsoleItem.vue | 2 +- src/components/widgets/diagnostics/StateExplorer.vue | 2 +- src/components/widgets/filesystem/setupMonaco.ts | 2 +- src/store/console/actions.ts | 4 ++-- src/store/console/getters.ts | 2 +- src/store/helpers.ts | 2 +- src/store/printer/getters.ts | 5 ++--- src/util/link-external-urls.ts | 2 +- src/workers/parseGcode.ts | 4 ++-- 9 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/components/widgets/console/ConsoleItem.vue b/src/components/widgets/console/ConsoleItem.vue index ae0cf9a757..c1047698fc 100644 --- a/src/components/widgets/console/ConsoleItem.vue +++ b/src/components/widgets/console/ConsoleItem.vue @@ -31,7 +31,7 @@ export default class ConsoleItem extends Vue { get itemMessage () { let message = this.value.message if (this.value.type === 'response') { - message = this.value.message.replace(/([A-Z0-9_]{2,})/gm, (match, command) => { + message = this.value.message.replace(/([A-Z0-9_]{2,})/g, (match, command) => { if (command in this.knownCommands) return `${command.toUpperCase()}` return match }) diff --git a/src/components/widgets/diagnostics/StateExplorer.vue b/src/components/widgets/diagnostics/StateExplorer.vue index 7968ff5f1b..42ed8e37b2 100644 --- a/src/components/widgets/diagnostics/StateExplorer.vue +++ b/src/components/widgets/diagnostics/StateExplorer.vue @@ -31,7 +31,7 @@ export default class StateExplorer extends Mixins(StateMixin) { handleClick (path: string) { const sanitizedPath = path .replace('$.', '') - .replace(/\.(\w*[^\w\S.]+\w*)/g, (_, match) => { + .replace(/\.(\w*\s+\w*)/g, (_, match) => { if (isNaN(match)) return `['${match}']` return `[${match}]` }) diff --git a/src/components/widgets/filesystem/setupMonaco.ts b/src/components/widgets/filesystem/setupMonaco.ts index 318bd33a18..5c8f9d1c14 100644 --- a/src/components/widgets/filesystem/setupMonaco.ts +++ b/src/components/widgets/filesystem/setupMonaco.ts @@ -172,7 +172,7 @@ async function setupMonaco () { const sectionBlocks = linesContent .reduce((state, lineContent, index) => { - const isSection = /^\[([^\]]+)\]/.test(lineContent) + const isSection = /^\[[^\]]+\]/.test(lineContent) if (isSection) { state.result.push(state.current = { diff --git a/src/store/console/actions.ts b/src/store/console/actions.ts index ad2f43fb9e..4a956af9b3 100644 --- a/src/store/console/actions.ts +++ b/src/store/console/actions.ts @@ -43,7 +43,7 @@ export const actions: ActionTree = { * Add a console entry */ async onAddConsoleEntry ({ commit, dispatch }, payload: ConsoleEntry) { - payload.message = DOMPurify.sanitize(payload.message).replace(/(?:\r\n|\r|\n)/g, '
') + payload.message = DOMPurify.sanitize(payload.message).replace(/\r\n|\r|\n/g, '
') if (!payload.time || payload.time <= 0) { payload.time = Date.now() / 1000 | 0 } @@ -68,7 +68,7 @@ export const actions: ActionTree = { .map((entry, index: number) => { entry.message = Globals.CONSOLE_RECEIVE_PREFIX + entry.message - entry.message = DOMPurify.sanitize(entry.message).replace(/(?:\r\n|\r|\n)/g, '
') + entry.message = DOMPurify.sanitize(entry.message).replace(/\r\n|\r|\n/g, '
') entry.id = index diff --git a/src/store/console/getters.ts b/src/store/console/getters.ts index d969ca5534..a160a919d4 100644 --- a/src/store/console/getters.ts +++ b/src/store/console/getters.ts @@ -2,7 +2,7 @@ import type { GetterTree } from 'vuex' import type { ConsoleState, GcodeHelp } from './types' import type { RootState } from '../types' -const _tempWaitExpr = /^(?:ok\s+)?(b|t\d+):\d+\.\d+ \/\d+\.+\d+/i +const _tempWaitExpr = /^(?:ok\s+)?(?:b|t\d+):\d+\.\d+ \/\d+\.+\d+/i export const getters: GetterTree = { /** diff --git a/src/store/helpers.ts b/src/store/helpers.ts index b020ff08af..cc6d212d30 100644 --- a/src/store/helpers.ts +++ b/src/store/helpers.ts @@ -9,7 +9,7 @@ export const handleTrinamicDriversChange = (payload: any, state: RootState, disp const [type, nameFromSplit] = item.split(' ', 2) if ( - /^tmc\d{4}$/.exec(type) && + /^tmc\d{4}$/.test(type) && payload[item]?.drv_status?.otpw != null && state.printer.printer?.[item]?.drv_status?.otpw == null ) { diff --git a/src/store/printer/getters.ts b/src/store/printer/getters.ts index 3918fe044d..8b78cf6b9e 100644 --- a/src/store/printer/getters.ts +++ b/src/store/printer/getters.ts @@ -39,7 +39,6 @@ export const getters: GetterTree = { }, getKlippyStateMessage: (state, getters, rootState, rootGetters): string => { - const regex = /(?:\r\n|\r|\n)/g // If there's absolutely no connection to klipper, then // say so. const serverInfo = rootGetters['server/getInfo'] as ServerInfo @@ -54,13 +53,13 @@ export const getters: GetterTree = { state.printer.info.state_message && state.printer.info.state_message !== '' ) { - return state.printer.info.state_message.trim().replace(regex, '
') + return state.printer.info.state_message.trim().replace(/\r\n|\r|\n/g, '
') } if ( state.printer.webhooks.state_message && state.printer.webhooks.state_message !== '' ) { - return state.printer.webhooks.state_message.trim().replace(regex, '
') + return state.printer.webhooks.state_message.trim().replace(/\r\n|\r|\n/g, '
') } return 'Unknown' }, diff --git a/src/util/link-external-urls.ts b/src/util/link-external-urls.ts index f000c11607..03b0a757df 100644 --- a/src/util/link-external-urls.ts +++ b/src/util/link-external-urls.ts @@ -1,4 +1,4 @@ -const externalUrlRegExp = /(https?:\/\/[^\s]+)/gi +const externalUrlRegExp = /(https?:\/\/\S+)/gi const linkExternalUrls = (text: string) => text.replace(externalUrlRegExp, '$1') diff --git a/src/workers/parseGcode.ts b/src/workers/parseGcode.ts index a74f1a5e53..fc3dc331fa 100644 --- a/src/workers/parseGcode.ts +++ b/src/workers/parseGcode.ts @@ -7,7 +7,7 @@ import shlex from 'shlex' const getArgsFromGcodeCommandArgs = (gcodeCommandArgs: string) => { const args: Record = {} - for (const [, key, value] of gcodeCommandArgs.matchAll(/([a-z])[ \t]*(-?(?:\d+(?:\.\d+)?|\.\d+))/ig)) { + for (const [, key, value] of gcodeCommandArgs.matchAll(/([a-z])[ \t]*(-?(?:\d+(?:\.\d+)?|\.\d+))/gi)) { args[key.toLowerCase()] = +value } @@ -33,7 +33,7 @@ const parseLine = (line: string) => { .split(';', 2)[0] const [, gcodeCommand, gcodeCommandArgs = ''] = clearedLine - .split(/^([gm][0-9]+)\s*/i) + .split(/^([gm]\d+)\s*/i) if (gcodeCommand) { return {