-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.ts
57 lines (51 loc) · 1.54 KB
/
code.ts
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
function floorFixed(num:number, dec:number, maxDigits:number) {
if (Math.log10(num) < maxDigits) {
const calcDec = Math.pow(10, dec);
return Math.trunc(num * calcDec) / calcDec;
} else {
return num;
}
}
function friendlyNum(numStr:string, maxDigits:number) {
numStr = removeCommas(numStr);
let nonNumericPart = numStr.match(/[^\d.-]/g)?.join('');
nonNumericPart = String(nonNumericPart) === "undefined" ? '' : nonNumericPart;
console.log(nonNumericPart);
const num = parseFloat(numStr);
const order = Math.ceil(Math.log10(num));
const decimalPoints = maxDigits - order;
let rounded = floorFixed(num, decimalPoints, maxDigits);
// return rounded;
return addCommas(String(rounded) + nonNumericPart);
}
function removeCommas(numStr:string) {
return numStr.replace(/,/g, '');
}
function addCommas(numStr:String) {
numStr += '';
var x = numStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
figma.showUI(__html__);
figma.ui.onmessage = async msg => {
if (msg.type === 'round') {
for (const node of figma.currentPage.selection) {
if (node.type === "TEXT") {
await Promise.all(
node.getRangeAllFontNames(0, node.characters.length)
.map(figma.loadFontAsync)
);
let rounded = friendlyNum(node.characters, msg.count);
node.characters = String(rounded);
}
}
figma.closePlugin();
}
figma.closePlugin();
};