Skip to content

Commit

Permalink
Merge pull request #33 from avencera/sort-all-variants
Browse files Browse the repository at this point in the history
Sort all variants
  • Loading branch information
praveenperera authored Jun 13, 2021
2 parents eb8419d + ee7e832 commit 9fa0f18
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

- Fix not sorting half classes properly `ex: mt-0.5`
- Sort all variant classes

## [0.11.0] - 2021-06-11

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ ignore = "0.4"
# utils
itertools = "0.10"
lazy_static = "1.4.0"

# string matching
aho-corasick = "0.7"
regex = "1"

# parrelel
# parallel
rayon = "1.5"

[dev-dependencies]
Expand Down
37 changes: 35 additions & 2 deletions src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
use aho_corasick::{AhoCorasick, AhoCorasickBuilder, MatchKind};
use lazy_static::lazy_static;

lazy_static! {
pub static ref RESPONSIVE_SIZES: Vec<&'static str> =
vec!["sm", "md", "lg", "xl", "2xl", "3xl", "4xl", "5xl", "6xl"];
pub static ref VARIANTS: Vec<&'static str> = vec![
"sm",
"md",
"lg",
"xl",
"2xl",
"3xl",
"4xl",
"5xl",
"6xl",
"dark",
"first",
"last",
"odd",
"even",
"visited",
"checked",
"group-hover",
"group-focus",
"hover",
"focus",
"focus-visible",
"focus-within",
"active",
"disabled"
];
}

lazy_static! {
pub static ref VARIANT_SEARCHER: AhoCorasick = AhoCorasickBuilder::new()
.anchored(true)
.match_kind(MatchKind::LeftmostLongest)
.auto_configure(&VARIANTS)
.build(VARIANTS.iter());
}
51 changes: 13 additions & 38 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod consts;
pub mod defaults;
pub mod options;

use consts::RESPONSIVE_SIZES;
use consts::{VARIANTS, VARIANT_SEARCHER};
use defaults::{RE, SORTER};
use options::Options;

Expand Down Expand Up @@ -52,41 +52,16 @@ fn sort_classes_vec<'a>(classes: impl Iterator<Item = &'a str>) -> Vec<&'a str>
for (class, maybe_size) in enumerated_classes {
match maybe_size {
Some(size) => tailwind_classes.push((class, size)),
None => match class.as_bytes() {
[b's', b'm', b':', ..] => responsive.entry("sm").or_insert(Vec::new()).push(class),

[b'm', b'd', b':', ..] => responsive.entry("md").or_insert(Vec::new()).push(class),

[b'l', b'g', b':', ..] => responsive.entry("lg").or_insert(Vec::new()).push(class),

[b'x', b'l', b':', ..] => responsive.entry("xl").or_insert(Vec::new()).push(class),

[b'2', b'x', b'l', b':', ..] => responsive
.entry("2xl")
.or_insert_with(|| vec![])
.push(class),

[b'3', b'x', b'l', b':', ..] => responsive
.entry("3xl")
.or_insert_with(|| vec![])
.push(class),

[b'4', b'x', b'l', b':', ..] => responsive
.entry("4xl")
.or_insert_with(|| vec![])
.push(class),

[b'5', b'x', b'l', b':', ..] => responsive
.entry("5xl")
.or_insert_with(|| vec![])
.push(class),

[b'6', b'x', b'l', b':', ..] => responsive
.entry("6xl")
.or_insert_with(|| vec![])
.push(class),

_ => custom_classes.push(class),
None => match VARIANT_SEARCHER.find(&class) {
Some(prefix_match) => {
let prefix = VARIANTS[prefix_match.pattern()];
responsive
.entry(prefix)
.or_insert_with(Vec::new)
.push(class)
}

None => custom_classes.push(class),
},
}
}
Expand All @@ -100,9 +75,9 @@ fn sort_classes_vec<'a>(classes: impl Iterator<Item = &'a str>) -> Vec<&'a str>

let mut sorted_responsive_classes = vec![];

for key in RESPONSIVE_SIZES.iter() {
for key in VARIANTS.iter() {
let (mut sorted_classes, new_custom_classes) = sort_responsive_classes(
responsive.remove(key).unwrap_or_else(|| vec![]),
responsive.remove(key).unwrap_or_else(Vec::new),
custom_classes,
key.len() + 1,
);
Expand Down
25 changes: 25 additions & 0 deletions tests/rustywind_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,28 @@ fn test_sort_file_contents_with_responsive_classes() {
expected_outcome
)
}

#[test]
fn test_sort_file_contents_with_variant_classes() {
let file_contents = r#"
<div>
<div class='even:inline 4xl:inline-block focus-visible:flex absolute xl:relative relative focus:flex flex active:flex disabled:flex visited:flex inline-block dark:absolute sm:relative sm:flex inline random-class justify-items another-random-class
sm:absolute 4xl:flex xl:random-class sm:inline-block'>
<ul class='flex items-center md:pr-4 lg:pr-6 xl:flex'>
</div>
</div>
"#;

let expected_outcome = r#"
<div>
<div class='inline-block inline flex absolute relative sm:inline-block sm:flex sm:absolute sm:relative xl:relative 4xl:inline-block 4xl:flex dark:absolute even:inline visited:flex focus:flex focus-visible:flex active:flex disabled:flex random-class justify-items another-random-class xl:random-class'>
<ul class='flex items-center md:pr-4 lg:pr-6 xl:flex'>
</div>
</div>
"#.to_string();

assert_eq!(
rustywind::sort_file_contents(file_contents, &default_options_for_test()),
expected_outcome
)
}

0 comments on commit 9fa0f18

Please sign in to comment.