swc_css_minifier/compressor/
frequency.rs

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
use swc_css_ast::*;

use super::Compressor;

impl Compressor {
    pub(super) fn compress_frequency(&mut self, frequency: &mut Frequency) {
        match &*frequency.unit.value {
            "hz" if frequency.value.value > 0.0 && frequency.value.value % 1000.0 == 0.0 => {
                let new_value = frequency.value.value / 1000.0;

                frequency.value = Number {
                    span: frequency.value.span,
                    value: new_value,
                    raw: None,
                };
                frequency.unit = Ident {
                    span: frequency.unit.span,
                    value: "khz".into(),
                    raw: None,
                };
            }
            "khz"
                if frequency.value.value == 0.0
                    || (frequency.value.value > 0.0 && frequency.value.value < 0.1) =>
            {
                let new_value = frequency.value.value * 1000.0;

                frequency.value = Number {
                    value: new_value,
                    raw: None,
                    span: frequency.span,
                };
                frequency.unit = Ident {
                    span: frequency.unit.span,
                    value: "hz".into(),
                    raw: None,
                };
            }
            _ => {}
        }
    }
}