-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (49 loc) · 1.67 KB
/
index.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const path = require('path');
const convert = require('convert-units');
const getPluginItem = ({ inputStr }) => {
const items = [];
if (!inputStr) return { items: [] };
const [targetNumAndUnit, ...others] = inputStr.split(' ');
const num = Number(targetNumAndUnit.match(/\d+/g));
if (!num) return { items: [] };
const unit = targetNumAndUnit.split(num)[1];
if (!unit) return { items: [] };
const possibleUnits = convert().from(unit).possibilities();
if (!others || others.length === 0) {
possibleUnits.map((possibleUnit) => {
const converted = String(convert(num).from(unit).to(possibleUnit)) + possibleUnit;
items.push({
title: converted,
subtitle: `${num}${unit} = ${converted}`,
icon: {
path: `${__dirname}${path.sep}icon.png`,
},
});
});
} else if (others.length === 1 && others[0] && possibleUnits.includes(others[0])) {
const targetUnit = others[0];
const converted = String(convert(num).from(unit).to(targetUnit)) + targetUnit;
items.push({
title: converted,
subtitle: `${num}${unit} = ${converted}`,
icon: {
path: `${__dirname}${path.sep}icon.png`,
},
});
} else if (others.length > 1 && others[0] === 'to' && possibleUnits.includes(others[1])) {
const targetUnit = others[1];
const converted = String(convert(num).from(unit).to(targetUnit)) + targetUnit;
items.push({
title: converted,
subtitle: `${num}${unit} = ${converted}`,
icon: {
path: `${__dirname}${path.sep}icon.png`,
},
});
}
return {
items,
};
};
// Export a function that has inputStr as a argument.
module.exports = getPluginItem;