jouni-kantola / ruin

A CSS post-processor that saves bytes over the wire by shortening names of CSS classes.
MIT License
2 stars 0 forks source link

generate hash #6

Closed jouni-kantola closed 8 years ago

jouni-kantola commented 8 years ago

Now that I started, I came to think about the hash. I took one from SO, but I realized the class name cannot start with a numerical value.

This should be ok I guess: For a class list of less than 9999 classes, the fastest way should be to just enumerate. I.e: {'c1': 'a-class', 'c2': 'another-class', 'cNNNN': 'a-9999-class'}

For class lists larger than 9999 classes, either enumerate the first 9999 classes and hash the rest, or hash all. Either way a check has to be made whether the hash has already been created to not get class name collisions.

davidxcheng commented 8 years ago

According to this SO question we have 64 valid chars to play with (a-zA-Z0-9 plus hyphen and underscore). 64^3 gives us roughly 260 000 valid class names* so we should not have to use more than 3 chars to have unique identification, right? For class lists just under* 4000 items we can get away with 2 chars.

* As you mentioned they can't begin with a number and there seems to be a couple of more exceptions too

jouni-kantola commented 8 years ago

Less is better == 3 is better than 4. We could make it configurable, since more chars => more time.

davidxcheng commented 8 years ago

My idea was to generate all valid 1-3 class names and put them in an array sorted by length. And then we .shift() them out like out of a PEZ dispenser as we replace the original class names.

jouni-kantola commented 8 years ago

Aye, seems like a good idea. Let's start off there.

(You can close this issue, if you want to.)

davidxcheng commented 8 years ago

Ok, I'll close it and merge my own pull request that contains a module that generates class names.