Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tweak-bytes: make possible to use char literals in live session #2041

Merged
merged 2 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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