Questionably useful functions and other gists

Also available at: my Github Gists

This is a collection of short snippets of code I've written or stolen. Some more useful than others; some more satirical than other; some are just a learning experience.

Most are in JS or C

Order is chronological, newest first.

JavsScript object to HTML table

Mimics console.table. Converts two layer deep objects to table. First layer on rows, second one on columns; this can be trivially switched.

There is a test object in the gist to test the function and see its behavior. As long as no first layer value is null or undefined the function should work. There is also a function, sinkShallowValues, to sink shallow values to a _value column.

Convert to and from Roman numerals

There is no standard Roman numberal but this implementation uses the more commonly used notation that represents values up to 3999 (MMMCMXCIX). Additional symbols can be added easily.

arabicToRoman is a digit by digit lookup then concatenated. Really shows how useful zero is and the ingenuity of ancient India.

romanToArabic test whether subtractive notation is used then sum values.

Each function has a .validate function to test if the input is legal.

Music note to frequency

Gists link

Function that converts scientific pitch notation (American standard pitch notation or international pitch notation) to a frequency. Input must pass noteToPitch.validate else function may not work as intended.

First character must be an uppercase letter of the note A-G. Follows is '#' for sharp and 'b' for flat. Although not standard, these flat and sharp modifiers may be stacked infinitely. The octave number follows; it can be any integer. All pitch is equal temperament at A440. The pitch of standard A can be trivially changed.

For example:

Random word to random floating point

Multiplying and adding _01 should be enough unless this function is a performance bottleneck or whatever operation that you're doing decreases the entropy of the mantissa too much.

random64() can be the function below.

xorshift128+ (pseudorandom number generator)

I stole this from the V8 blog. I am not really sure how it works but it's very useful and my programs have probably called this function over a trillion times.

You should probably change the seed, or not.

Raw data to BMP image

This function takes an array and outputs a 8 bit per channel bitmap image. I don't really remember how it works so good luck.

JS integer to english words

This one converts a javascript Number to english words. It is badly written but kinda fun.

It uses of javascript's slippery type conversion and string manipulation. Where "and" is inserted can be changed to whatever you like on line 11.

Showcase

Whether to use a black or white text given background color

equivalently...

There are lots of occasions where one needs to determine if text should be black or white based on the background of it. This function calculates whether black text should be used using a method that is fully W3C compliant. All 16.7 million RGB values are tested against another function that is by definition compliant.

This function was written to look nice and not very readable.

Loosely based on: link1 and link2. Also see actual guideline.

Showcase

Click for a new color. Colors are random, displayed text color is based on the above function

Up with the white and gold
Down with the red and black
Georgia Tech is out for a victory
We'll drop a battle axe on georgia's head

Black. White.
Happy families are all alike; every unhappy family is unhappy in its own way.

Black. White.

Sample text: Frank Roman's Up with the White and Gold; Leo Tolstoy's Anna Karenina. Both public domain.