Skip to content

Commit

Permalink
Better number parsing on cli
Browse files Browse the repository at this point in the history
  • Loading branch information
sirtimid committed Sep 12, 2024
1 parent 4406f5d commit 6efdc03
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
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);
});
17 changes: 17 additions & 0 deletions packages/cli/src/number-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Parses the input and returns it as a number if it's valid, otherwise returns undefined.
*
* @param {string | number} input
* @returns {number | undefined}
*/
export const parseNumber = input => {
if (typeof input === 'number' && !Number.isNaN(input)) {
return input;
}

if (typeof input === 'string' && /^-?\d+(\.\d+)?$/.test(input.trim())) {
return Number(input);
}

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

// Test for valid number input
test('returns the number itself if input is a valid number', t => {
t.is(parseNumber(42), 42);
t.is(parseNumber(-42), -42);
t.is(parseNumber(0), 0);
t.is(parseNumber(3.14), 3.14);
t.is(parseNumber(-3.14), -3.14);
});

// Test for valid string input
test('returns the number when input is a valid numeric string', t => {
t.is(parseNumber('42'), 42);
t.is(parseNumber('-42'), -42);
t.is(parseNumber('3.14'), 3.14);
t.is(parseNumber('-3.14'), -3.14);
t.is(parseNumber(' 42 '), 42);
});

// Test for invalid string input
test('returns undefined for invalid numeric strings', t => {
t.is(parseNumber('abc'), undefined);
t.is(parseNumber('42abc'), undefined);
t.is(parseNumber(''), undefined);
t.is(parseNumber(' '), undefined);
t.is(parseNumber('42..42'), undefined);
});

// Test for NaN input
test('returns undefined for NaN', t => {
t.is(parseNumber(NaN), undefined);
});

// Test for non-string and non-number input
test('returns undefined for non-string, non-number input', t => {
t.is(parseNumber(null), undefined);
t.is(parseNumber(undefined), undefined);
t.is(parseNumber({}), undefined);
t.is(parseNumber([]), undefined);
});

0 comments on commit 6efdc03

Please sign in to comment.