swc_ecma_minifier/util/
sort.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use std::cmp::Ordering;

pub(crate) fn is_sorted_by<I, T, F>(mut items: I, mut compare: F) -> bool
where
    I: Iterator<Item = T>,
    T: Copy,
    F: FnMut(&T, &T) -> Option<Ordering>,
{
    let last = match items.next() {
        Some(e) => e,
        None => return true,
    };

    items
        .try_fold(last, |last, curr| {
            if let Some(Ordering::Greater) | None = compare(&last, &curr) {
                return Err(());
            }
            Ok(curr)
        })
        .is_ok()
}