I made a Node.js colors library in 192 bytes minzipped (+ code explanation!)

Siddharth - Mar 6 '22 - - Dev Community

A few minutes ago, I published PlanckColors. It's probably the smallest node module for colors on the internet, at just 119 bytes minzipped! It's so small, here's the source code:

let p=['black','red','green','yellow','blue','magenta','cyan','white'],x={},q=['reset','bold','dim','italic','underline','blink',,'reverse','hide','strike'],s=(S='',A=30,T=p)=>T.map((a,i)=>x[a+S]=t=>`\x1b[${i+A}${B}m${t}\x1b[0m`)&&s;s()('Bg',40)('',0,q);export default x;
Enter fullscreen mode Exit fullscreen mode

That's it! 🤯

Planck?

The Planck length is the smallest possible distance between two things. Anything closer than that is considered to be at the same place.

The next smallest name, yoctocolors was already taken, so I settled for PlanckColors 😎

WTF is going on in this code?!?!

let p = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'],
        // colors
        // the colors are positioned to line up with their ansi escape;
        // black is code 30 (0), red is 31 (1), etc.

    q = ['reset', 'bold', 'dim', 'italic', 'underline', 'blink', , 'reverse', 'hide', 'strike'],
        // styles                                              A
        //                                                     |
        // same here; reset is 0, bold is 1, etc               |
        // notice the double comma. It's used to skip items in an array,
        // so we can skip index 6 which has no style associated with it.

    x = {},
        // the final object containing all the functions

    // This function is used to add new colors.
    s = (
        S = '', // a suffix to add to the color's name
        A = 30, // the number to add to each color,
                // for example, black is at index 0. 30 + 0 = 30, which is the escape code for black
        T = p   // the array from which to add colors. The default is p
    ) => T
        .map(   // using map, as it is shorter than `forEach`
                (
                    a, // color
                    i  // index
                ) => x[a + S] = // set the color to...
                        t => `\x1b[${i + A}${B}m${t}\x1b[0m`) // a function which returns colored text...
                        && s; // and return the function itself, so we can chain calls

s()          // call with all the default arguments
 ('Bg', 40)  // call with bg prefix, and add 40 so we get background escape codes
 ('', 0, q); // call with the styles array

export default x; // finally, export our colors
Enter fullscreen mode Exit fullscreen mode

Should I ditch chalk and use this?

You can!

The only thing to consider is the fact that this library doesn't provide any color detection out of the box. However, since this module only provides 16 colors, it may not be such a big issue. Most terminals support 16 colors.

If you do end up using this, let me know!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .