Skip to content

Commit

Permalink
Merge pull request #2041 from xodio/fix-1864-char-literals-in-tweak-b…
Browse files Browse the repository at this point in the history
…ytes

Fix tweak-bytes: make possible to use char literals in live session
  • Loading branch information
brusherru authored Sep 17, 2020
2 parents a98e98c + 864269a commit ce8f9cb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 19 additions & 1 deletion packages/xod-arduino/src/formatTweakMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ import { def } from './types';

import { byteLiteralToDecimal } from './templates';

// Convert char literals to decimal byte literals
// E.G. `'a'` -> `97d`
const charLiteralToByteLiteral = R.when(
XP.isLikeCharLiteral,
R.compose(
R.concat(R.__, 'd'),
R.toString,
a => a.charCodeAt(0),
R.nth(1),
R.match(XP.charLiteralRegExp)
)
);

const formatByteLiteral = R.compose(
byteLiteralToDecimal,
charLiteralToByteLiteral
);

export default def(
'formatTweakMessage :: PatchPath -> NodeId -> DataValue -> String',
(nodeType, nodeId, value) => {
Expand All @@ -17,7 +35,7 @@ export default def(
case XP.PIN_TYPE.BOOLEAN:
return `${prefix}:${value === 'True' ? '1' : '0'}\r\n`;
case XP.PIN_TYPE.BYTE:
return `${prefix}:${byteLiteralToDecimal(value)}\r\n`;
return `${prefix}:${formatByteLiteral(value)}\r\n`;
case XP.PIN_TYPE.PULSE:
return `${prefix}\r\n`;
case XP.PIN_TYPE.STRING:
Expand Down
4 changes: 3 additions & 1 deletion packages/xod-project/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,11 @@ export const isValidNumberDataValue = R.test(numberDataTypeRegExp);

export const isValidPortLiteral = R.test(/^(P[A-F]|A|D)\d{0,3}$/g);

export const charLiteralRegExp = /^'\\?(.)'$/;

export const isLikeCharLiteral = def(
'isLikeCharLiteral :: String -> Boolean',
R.test(/^'\\?.'$/)
R.test(charLiteralRegExp)
);

const unescapedCharLiterals = ["'''", "'\\'"];
Expand Down

0 comments on commit ce8f9cb

Please sign in to comment.