Skip to content

Instantly share code, notes, and snippets.

@westc
westc / dedupe.js
Created May 5, 2026 22:35
dedupe() - Takes an array and removes all of the duplicate values determined by the specified hash function. This can be used to make an array of unique values but the uniqueness should rely more on underlying values or properties.
/**
* Takes an array and removes all of the duplicate values determined by the
* specified hash function.
* @template {any[]} T
* @param {T} array
* @param {(value: T[number], index: number, array: T) => any} hash
* @returns {T}
*/
function dedupe(array, hash) {
const hashedValues = new Set();
@westc
westc / sortJSON.js
Last active May 5, 2026 22:28
sortJSON() - Converts a JS value to canonical a JSON string that will be the same every time regardless of the order in which the keys are defined in the underlying objects.
/**
* @license Copyright 2026 - Chris West - MIT Licensed
* @see https://gist.github.com/westc/f1bf5453b74a055bb4013f4e23da96eb
*
* Converts a JS value to a canonical JSON string that will be the same every
* time regardless of the order in which the keys are defined in the underlying
* objects.
* @param {any} value
* A JavaScript value, usually an object or array, to be converted.
* @param {Parameters<typeof JSON.stringify>[1]} replacer
@westc
westc / random.js
Last active February 23, 2026 15:19
A simple & complete implementation of random which can also generate cryptographically sound numbers.
var random = (() => {
const uints = new Uint32Array(2);
/**
* @typedef $randomOptions
* @property {boolean} [returnInt=false]
* Defaults to `false`. If `true` then the returned number will be an
* integer. Floats will be rounded down if `includedLimit` is less than
* `excludedLimit`, otherwise they will be rounded up.
* @property {boolean} [useCrypto=false]
@westc
westc / favicon-character.js
Last active August 2, 2025 04:09
Sets a single character (eg. a unicode emoji as the favicon for the page.
/**
* Sets the character stored in FAVICON_CHARACTER as the favicon for this page.
*/
addEventListener('DOMContentLoaded', () => {
// The single character that will appear as the favicon for this page.
const FAVICON_CHARACTER = '\u{1F510}';
// Remove all `<link>` elements that are for icons.
document.querySelectorAll("link[rel~='icon']").forEach(el => el.remove());
@westc
westc / marquee-favicon.js
Last active August 1, 2025 21:48
Use JavaScript to set the favicon on a page so that it cycles through the characters (code points) in a string in similar way to how a marquee would.
/**
* Sets the favicon for the page to show one letter per second of the
* MARQUEE_TEXT constant. The background will be a solid color that slowly
* changes, cycling through different hues.
*/
addEventListener('DOMContentLoaded', () => {
const MARQUEE_TEXT = 'A TEST ';
// The canvas that will be used to generate the image that will show as the
// favicon once every second.
@westc
westc / order.js
Last active October 28, 2025 15:27
order() - Sorts an array based on one or more criteria.
/**
* A getter that produces the value to compare for a given item.
*
* @template T
* @callback order__Getter
* @param {T} item The current item from the array.
* @param {number} index The index of the current item.
* @param {T[]} array The entire array being ordered.
* @param {number} criterionIndex The index of the current criterion.
* @returns {number|string|bigint|boolean|Date} A value that can be ordered with < and >.
@westc
westc / copyJSON.js
Last active June 26, 2025 20:50
copyJSON() - An easy way to copy values while allowing circular references to be omitted.
/**
* @template T
* @param {T} value
* @param {boolean} [omitCircularRefs=false]
* @returns {T}
*/
function copyJSON(value, omitCircularRefs = false) {
const ancestors = new WeakSet();
function recurse(value) {
@westc
westc / getFieldNames-and-queryAllFields.cls
Last active June 10, 2025 16:34
getFieldNames() and queryAllFields() Apex functions that can be used to query all of the fields of an sobject.
public static String[] getFieldNames(String sobjectName) {
String[] fieldNames = new String[]{};
Schema.SObjectType sobjectType = Schema.getGlobalDescribe().get(sobjectName);
if (sobjectType != null) {
Map<String, Schema.SObjectField> fieldsMap = sobjectType.getDescribe().fields.getMap();
fieldNames.addAll(fieldsMap.keySet());
}
return fieldNames;
}
@westc
westc / split.py
Last active March 9, 2025 17:54
split() - Splits the input string based on a specified separator while allowing exclusions and optional trimming.
from typing import Optional
def split(
input_string: str,
sep: Optional[str] = None,
max_split: int = -1,
exclusions: Optional[list[str]] = None,
strip_items: bool = False
) -> list[str]:
@westc
westc / toAlphaNumber.js
Created January 10, 2025 15:28
toAlphaNumber() - Converts a positive integer into an alphabetic representation, similar to ordered list lettering and Excel column naming.
/**
* Converts a positive integer into an alphabetic representation, similar to
* ordered list lettering and Excel column naming.
*
* @param {number} int
* The positive integer to convert. Non-integers will be floored.
* @param {boolean} [returnLowerCase=false]
* If `true`, returns lowercase letters (e.g., 'a', 'b', ...).
* If `false`, returns uppercase letters (e.g., 'A', 'B', ...).
* @returns {string}