-
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improves macro params quoting and encoding
Signed-off-by: Pedro Lamas <pedrolamas@gmail.com>
- Loading branch information
1 parent
55d3802
commit 286ebcd
Showing
3 changed files
with
114 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { gcodeCommandBuilder } from '../gcode-command-builder' | ||
|
||
describe('gcodeCommandBuilder', () => { | ||
it.each([ | ||
[{ command: 'M117', params: { Message: '' } }, 'M117'], | ||
[{ command: 'M117', params: { Message: 'ab' } }, 'M117 ab'], | ||
[{ command: 'M117', params: { Message: { value: 'ab' } } }, 'M117 ab'], | ||
[{ command: 'G1', params: { } }, 'G1'], | ||
[{ command: 'G1', params: { X: '' } }, 'G1'], | ||
[{ command: 'G1', params: { X: 10 } }, 'G1 X10'], | ||
[{ command: 'G1', params: { X: { value: 10 } } }, 'G1 X10'], | ||
[{ command: 'TEST', params: { } }, 'TEST'], | ||
[{ command: 'TEST', params: { X: '' } }, 'TEST'], | ||
[{ command: 'TEST', params: { X: 'abc' } }, 'TEST X=abc'], | ||
[{ command: 'TEST', params: { X: 'a b c' } }, 'TEST X="a b c"'], | ||
[{ command: 'TEST', params: { X: ' a b c ' } }, 'TEST X=" a b c "'], | ||
[{ command: 'TEST', params: { X: 'a\'b\'c' } }, 'TEST X="a\'b\'c"'], | ||
[{ command: 'TEST', params: { X: 'a"b"c' } }, 'TEST X="a\\"b\\"c"'], | ||
[{ command: 'TEST', params: { X: 'a#b#c' } }, 'TEST X="a#b#c"'], | ||
[{ command: 'TEST', params: { X: 'a;b;c' } }, 'TEST X="a;b;c"'], | ||
[{ command: 'TEST', params: { X: 'a=b=c' } }, 'TEST X="a=b=c"'], | ||
[{ command: 'TEST', params: { X: 'a\\b\\c' } }, 'TEST X="a\\\\b\\\\c"'], | ||
])('Expects params of "%s", to be %s', ({ command, params }, expected) => { | ||
expect(gcodeCommandBuilder(command, params)).toStrictEqual(expected) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import isKeyOf from './is-key-of' | ||
|
||
const quotableCharsRegexp = /[ '"#;=\\]/ | ||
|
||
export type ParamValue = number | string | { value: string | number } | ||
|
||
export const rawGcodeCommands = { | ||
M117: 'Message', | ||
M118: 'Message', | ||
M23: 'Filename' | ||
} | ||
|
||
export const getParamNameForRawGcodeCommand = (commandName: string) => { | ||
return ( | ||
isKeyOf(commandName, rawGcodeCommands) && | ||
rawGcodeCommands[commandName] | ||
) | ||
} | ||
|
||
export const isBasicGcodeCommand = (commandName: string) => { | ||
return /^[gm]\d+$/i.test(commandName) | ||
} | ||
|
||
export const gcodeCommandBuilder = (commandName: string, params: Record<string, ParamValue>) => { | ||
commandName = commandName.toUpperCase() | ||
|
||
const basicGcodeCommand = isBasicGcodeCommand(commandName) | ||
const paramNameForRawGcodeCommand = getParamNameForRawGcodeCommand(commandName) | ||
|
||
const paramsString = paramNameForRawGcodeCommand | ||
? getParamValue(params[paramNameForRawGcodeCommand]) | ||
: Object.entries(params) | ||
.map(([key, param]) => { | ||
key = key.toUpperCase() | ||
|
||
const value = getParamValue(param) | ||
|
||
if (!value) { | ||
return null | ||
} | ||
|
||
if (basicGcodeCommand) { | ||
return `${key}${value}` | ||
} | ||
|
||
if (quotableCharsRegexp.test(value)) { | ||
const encodedValue = value | ||
.replace(/\\/g, '\\\\') | ||
.replace(/"/g, '\\"') | ||
|
||
return `${key}="${encodedValue}"` | ||
} | ||
|
||
return `${key}=${value}` | ||
}) | ||
.filter(x => x != null) | ||
.join(' ') | ||
|
||
if (paramsString) { | ||
return `${commandName} ${paramsString}` | ||
} | ||
|
||
return commandName | ||
} | ||
|
||
export const getParamValue = (param: ParamValue) => { | ||
return typeof param === 'object' | ||
? param.value.toString() | ||
: param.toString() | ||
} |