Skip to content

Commit

Permalink
refactor: regular expressions
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Lamas <[email protected]>
  • Loading branch information
pedrolamas committed Aug 27, 2024
1 parent c1dc2ae commit 52538ed
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/components/widgets/console/ConsoleItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<a class="primary--text text--lighten-1">${command.toUpperCase()}</a>`
return match
})
Expand Down
2 changes: 1 addition & 1 deletion src/components/widgets/diagnostics/StateExplorer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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}]`
})
Expand Down
2 changes: 1 addition & 1 deletion src/components/widgets/filesystem/setupMonaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions src/store/console/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const actions: ActionTree<ConsoleState, RootState> = {
* Add a console entry
*/
async onAddConsoleEntry ({ commit, dispatch }, payload: ConsoleEntry) {
payload.message = DOMPurify.sanitize(payload.message).replace(/(?:\r\n|\r|\n)/g, '<br />')
payload.message = DOMPurify.sanitize(payload.message).replace(/\r\n|\r|\n/g, '<br />')
if (!payload.time || payload.time <= 0) {
payload.time = Date.now() / 1000 | 0
}
Expand All @@ -68,7 +68,7 @@ export const actions: ActionTree<ConsoleState, RootState> = {
.map((entry, index: number) => {
entry.message = Globals.CONSOLE_RECEIVE_PREFIX + entry.message

entry.message = DOMPurify.sanitize(entry.message).replace(/(?:\r\n|\r|\n)/g, '<br />')
entry.message = DOMPurify.sanitize(entry.message).replace(/\r\n|\r|\n/g, '<br />')

entry.id = index

Expand Down
2 changes: 1 addition & 1 deletion src/store/console/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConsoleState, RootState> = {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/store/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down
5 changes: 2 additions & 3 deletions src/store/printer/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export const getters: GetterTree<PrinterState, RootState> = {
},

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
Expand All @@ -54,13 +53,13 @@ export const getters: GetterTree<PrinterState, RootState> = {
state.printer.info.state_message &&
state.printer.info.state_message !== ''
) {
return state.printer.info.state_message.trim().replace(regex, '<br />')
return state.printer.info.state_message.trim().replace(/\r\n|\r|\n/g, '<br />')
}
if (
state.printer.webhooks.state_message &&
state.printer.webhooks.state_message !== ''
) {
return state.printer.webhooks.state_message.trim().replace(regex, '<br />')
return state.printer.webhooks.state_message.trim().replace(/\r\n|\r|\n/g, '<br />')
}
return 'Unknown'
},
Expand Down
2 changes: 1 addition & 1 deletion src/util/link-external-urls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const externalUrlRegExp = /(https?:\/\/[^\s]+)/gi
const externalUrlRegExp = /(https?:\/\/\S+)/gi

const linkExternalUrls = (text: string) => text.replace(externalUrlRegExp, '<a target="_blank" href="$1">$1</a>')

Expand Down
4 changes: 2 additions & 2 deletions src/workers/parseGcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import shlex from 'shlex'
const getArgsFromGcodeCommandArgs = (gcodeCommandArgs: string) => {
const args: Record<string, number> = {}

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
}

Expand All @@ -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 {
Expand Down

0 comments on commit 52538ed

Please sign in to comment.