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(cli): Improve number parsing #2454

Merged
merged 1 commit into from
Sep 26, 2024
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
9 changes: 6 additions & 3 deletions packages/cli/src/commands/adopt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import os from 'os';
import { E } from '@endo/far';
import { withEndoAgent } from '../context.js';
import { parsePetNamePath } from '../pet-name.js';
import { parseNumber } from '../number-parse.js';

export const adoptCommand = async ({
messageNumberText,
Expand All @@ -11,7 +12,9 @@ export const adoptCommand = async ({
agentNames,
}) =>
withEndoAgent(agentNames, { os, process }, async ({ agent }) => {
// TODO less bad number parsing.
const messageNumber = Number(messageNumberText);
await E(agent).adopt(messageNumber, edgeName, parsePetNamePath(name));
await E(agent).adopt(
parseNumber(messageNumberText),
edgeName,
parsePetNamePath(name),
);
});
5 changes: 2 additions & 3 deletions packages/cli/src/commands/reject.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
import os from 'os';
import { E } from '@endo/far';
import { withEndoAgent } from '../context.js';
import { parseNumber } from '../number-parse.js';

export const rejectCommand = async ({
requestNumberText,
message,
agentNames,
}) =>
withEndoAgent(agentNames, { os, process }, async ({ agent }) => {
// TODO less bad number parsing.
const requestNumber = Number(requestNumberText);
await E(agent).reject(requestNumber, message);
await E(agent).reject(parseNumber(requestNumberText), message);
});
5 changes: 2 additions & 3 deletions packages/cli/src/commands/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
import os from 'os';
import { E } from '@endo/far';
import { withEndoAgent } from '../context.js';
import { parseNumber } from '../number-parse.js';

export const resolveCommand = async ({
requestNumberText,
resolutionName,
agentNames,
}) =>
withEndoAgent(agentNames, { os, process }, async ({ agent }) => {
// TODO less bad number parsing.
const requestNumber = Number(requestNumberText);
await E(agent).resolve(requestNumber, resolutionName);
await E(agent).resolve(parseNumber(requestNumberText), resolutionName);
});
15 changes: 15 additions & 0 deletions packages/cli/src/number-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Parses the input and returns it as a number if it's valid, otherwise throws error.
*
* @param {string} input
* @returns {number}
*/
export const parseNumber = input => {
const result = /[0-9]/.test(input || '') ? Number(input) : NaN;

if (Number.isNaN(result)) {
throw new Error(`Invalid number: ${input}`);
}

return result;
};
36 changes: 36 additions & 0 deletions packages/cli/test/number-parse.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import test from 'ava';
import { parseNumber } from '../src/number-parse.js';

test('returns the number when input is a valid numeric string', t => {
t.is(parseNumber('42'), 42);
t.is(parseNumber('-40.0'), -40.0);
t.is(parseNumber('.5'), 0.5);
t.is(parseNumber('1.337e3'), 1337);
t.is(parseNumber(' +1 '), 1);
t.is(parseNumber('0B111'), 7);
t.is(parseNumber('0o040'), 32);
t.is(parseNumber('0xf00'), 3840);
t.is(parseNumber('0xF00'), 3840);
t.is(parseNumber(' -40.0 '), -40.0);
// Test for binary, octal and hexadecimal
t.is(parseNumber('0B111'), 7);
t.is(parseNumber('0o040'), 32);
t.is(parseNumber('0xf00'), 3840);
sirtimid marked this conversation as resolved.
Show resolved Hide resolved
});

test('throws an error when input is not a valid numeric string', t => {
const badStrings = [
'f00',
'F00',
'NaN',
'Infinity',
'-Infinity',
'7up',
' ', // U+1680 OGHAM SPACE MARK (which is whitespace)
'',
' ',
];
for (const bad of badStrings) {
t.throws(() => parseNumber(bad), { message: `Invalid number: ${bad}` });
}
});
Loading