Skip to content

Commit

Permalink
chore: simplify anidation
Browse files Browse the repository at this point in the history
  • Loading branch information
dubisdev committed Mar 9, 2024
1 parent bc46ba9 commit f09cc51
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions extensions/math/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,39 @@ const run: ExtensionModule['run'] = async (ctx) => {
const { term, display, actions } = ctx;
const match = term.match(MATH_REGEXP);

if (match) {
try {
const calculateTerm = term.replace(/,/g, '.');

// eslint-disable-next-line no-eval
const result = eval(calculateTerm) as number;

if (Number.isNaN(result)) {
const indeterminateItem = new ScriptItem({
title: term + ' = indeterminate',
run: () => {
actions.copyToClipboard('indeterminate');
}
});
// When user tries to devide 0 by 0
display([indeterminateItem]);
return;
}
const stringResult = result.toLocaleString();
if (!match) return;

try {
const calculateTerm = term.replace(/,/g, '.');

// eslint-disable-next-line no-eval
const result = eval(calculateTerm);

if (typeof result !== 'number') return;

const resultItem = new ScriptItem({
title: term + ' = ' + stringResult,
if (Number.isNaN(result)) {
const indeterminateItem = new ScriptItem({
title: term + ' = indeterminate',
run: () => {
actions.copyToClipboard(stringResult);
actions.copyToClipboard('indeterminate');
}
});

display([resultItem]);
} catch (err) {
// Do nothing when eval failed
// When user tries to devide 0 by 0
display([indeterminateItem]);
return;
}
const stringResult = result.toLocaleString();

const resultItem = new ScriptItem({
title: term + ' = ' + stringResult,
run: () => {
actions.copyToClipboard(stringResult);
}
});

display([resultItem]);
} catch (err) {
// Do nothing when eval failed
}
};

Expand Down

0 comments on commit f09cc51

Please sign in to comment.