-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSum.js
35 lines (29 loc) · 1.07 KB
/
Sum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: plus; share-sheet-inputs: plain-text;
// @ts-ignore
// eslint-disable-next-line
try { require; } catch(e) { require = importModule; }
const { getInput, string, paste, output } = require('./lib/lib.js');
const getCommaGroups = /,(\d{3})/g;
const getNumbers = /-?\d+(\.\d+)?(e-?\d+)?/g;
const main = async () => {
const input = await getInput({
name: 'Sum',
help: 'Sums a space separated list of numbers.',
inScriptable: false,
args: [{
name: 'numbers',
shortName: 'n',
type: 'string',
share: true,
help: 'The list of (US-formatted) numbers.'
}]
});
if (!input) { return; }
const rawNumbers = string(input.numbers) || paste() || '';
const noCommas = rawNumbers.replace(getCommaGroups, '$1');
const numbers = (noCommas.match(getNumbers) || []).map(x => Number(x) || 0);
output('Sum', numbers.reduce((sum, x) => sum + x, 0));
};
main();