1 |
|
2 |
|
3 | export const SI_MID = 8;
|
4 | export const SI = [{
|
5 | power: -24,
|
6 | text: 'yocto',
|
7 | value: 'y'
|
8 | }, {
|
9 | power: -21,
|
10 | text: 'zepto',
|
11 | value: 'z'
|
12 | }, {
|
13 | power: -18,
|
14 | text: 'atto',
|
15 | value: 'a'
|
16 | }, {
|
17 | power: -15,
|
18 | text: 'femto',
|
19 | value: 'f'
|
20 | }, {
|
21 | power: -12,
|
22 | text: 'pico',
|
23 | value: 'p'
|
24 | }, {
|
25 | power: -9,
|
26 | text: 'nano',
|
27 | value: 'n'
|
28 | }, {
|
29 | power: -6,
|
30 | text: 'micro',
|
31 | value: 'µ'
|
32 | }, {
|
33 | power: -3,
|
34 | text: 'milli',
|
35 | value: 'm'
|
36 | }, {
|
37 | power: 0,
|
38 | text: 'Unit',
|
39 | value: '-'
|
40 | },
|
41 | {
|
42 | power: 3,
|
43 | text: 'Kilo',
|
44 | value: 'k'
|
45 | }, {
|
46 | power: 6,
|
47 | text: 'Mill',
|
48 | value: 'M'
|
49 | },
|
50 | {
|
51 | power: 9,
|
52 | text: 'Bill',
|
53 | value: 'B'
|
54 | },
|
55 | {
|
56 | power: 12,
|
57 | text: 'Tril',
|
58 | value: 'T'
|
59 | },
|
60 | {
|
61 | power: 15,
|
62 | text: 'Peta',
|
63 | value: 'P'
|
64 | }, {
|
65 | power: 18,
|
66 | text: 'Exa',
|
67 | value: 'E'
|
68 | }, {
|
69 | power: 21,
|
70 | text: 'Zeta',
|
71 | value: 'Z'
|
72 | }, {
|
73 | power: 24,
|
74 | text: 'Yotta',
|
75 | value: 'Y'
|
76 | }];
|
77 |
|
78 | export function findSi(type) {
|
79 |
|
80 | for (let i = 0; i < SI.length; i++) {
|
81 | if (SI[i].value === type) {
|
82 | return SI[i];
|
83 | }
|
84 | }
|
85 |
|
86 | return SI[SI_MID];
|
87 | }
|
88 | export function calcSi(text, decimals, forceUnit) {
|
89 | if (forceUnit) {
|
90 | return findSi(forceUnit);
|
91 | }
|
92 |
|
93 | const siDefIndex = SI_MID - 1 + Math.ceil((text.length - decimals) / 3);
|
94 | return SI[siDefIndex] || SI[siDefIndex < 0 ? 0 : SI.length - 1];
|
95 | } |
\ | No newline at end of file |