Skip to content

Instantly share code, notes, and snippets.

@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 June 18, 2025 19:36
order() - Sorts an array based on one or more criteria.
/**
* Sorts an array based on one or more criteria.
*
* @template T
* @param {T[]} array - The array to sort.
* @param {...(((item: T, index: number) => any) | ({getter: (item: T, index: number) => any, reverse: boolean}))} criterion -
*   One or more sorting criteria. Each can be a getter function or an object with a getter and a reverse flag.
* @returns {T[]} A new array sorted based on the provided criteria.
*/
function order(array, ...criterion) {
@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}
@westc
westc / jwsites-copy-sharepoint-list.js
Created December 18, 2024 21:30
This is how I copied one sharepoint list's contents to another sharepoint list that had the exact same structure. There must be an easier way to copy items from one list to another but this is what I have for now.
await(async() => {
const contextInfoURL = location.href.replace(/(^\w+:\/\/[^/]+(?:\/sites\/[^/]+)?).*/i, '$1/_api/contextinfo');
const digest = (await (await fetch(contextInfoURL, {
method: 'POST',
headers: {
Accept: 'application/json'
}
})).json()).FormDigestValue;
const sourceListName = 'TrainingVideoLinks'; // Replace with your source list name
@westc
westc / selectFile.js
Last active November 27, 2024 01:38
selectFile() - Shows the file selector dialog and returns an array of the selected files.
/**
* Shows the file selector dialog and returns an array of the selected files.
* @param {?selectFile__Options=} options
* @returns {Promise<File[]>}
*/
function selectFile(options) {
return new Promise(resolve => {
function onClose(e) {
resolve([...e.target.files]);
}
@westc
westc / getMonacoEditorCompDefs.js
Last active November 5, 2024 03:53
Get a <code-editor> component in Vue3 that uses Monaco.
/**
* @param {Record<"codeEditor"|"diffEditor",boolean|string>} defsToInclude
* @returns {Record<string,any>}
*/
function getMonacoEditorCompDefs(defsToInclude) {
/**
* @param {(script?: HTMLScriptElement) => T} getter
* Function that will be called to get the result of a script running. This
* function will initially be called without any arguments to determine if
* the desired results already exist but if not the script tag will be added