Skip to content

Instantly share code, notes, and snippets.

@sandgraham
sandgraham / findLexicalMiddleString.ts
Created December 17, 2021 20:55
Find string which is lexically sorted mid point between two other strings
// Algorithm adapted from an amazing StackOverflow answer:
// https://stackoverflow.com/questions/38923376/return-a-new-string-that-sorts-between-two-given-strings/38927158#38927158
const A = 97;
const B = 98;
const Z = 122;
const START = A - 1;
const END = Z + 1;
function findLexicalMiddleString(prev: string, next: string) {
@sandgraham
sandgraham / toHexColour.js
Created September 10, 2021 17:19
Convert string to hex code
// Hash any string into an integer value
// Then we'll use the int and convert to hex.
function hashCode(str) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
return hash;
}
@sandgraham
sandgraham / themes.md
Created August 22, 2019 02:03
Randoms thoughts on theme systems

Themes

A theme is the static configuration of a theme-able system. This requires a way to express the configuration and a theme-able system that is responsible for using the configuration to apply the expected styling.

I would argue that a good theme system has two key properties:

Interface

The interface consists of high level semantic variables that can be reused. This is where you assign values to variables such as primary, accent, etc. This is a convenient abstraction over specific assignments of values to style properties. It makes it easy to quickly customize a system with a different set of tokens.

@sandgraham
sandgraham / iso2FlagEmoji.js
Created June 4, 2019 03:18
Convert a country's ISO-2 string to a flag emoji in a single line
const iso2FlagEmoji = iso => String.fromCodePoint(...[...iso.toUpperCase()].map(char => char.charCodeAt(0) + 127397));
iso2FlagEmoji("US"); // "🇺🇸"