Skip to content

Instantly share code, notes, and snippets.

@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
@westc
westc / getNextMultiple.js
Created October 8, 2024 19:47
getNextMultiple() — Finds the smallest multiple of `multipleOf` that is greater than or equal to `baseValue + offset`.
/**
* Finds the smallest multiple of `multipleOf` that is greater than or equal to
* `baseValue + offset`.
*
* @param {number} multipleOf
* The number whose multiple needs to be found.
* @param {number} baseValue
* The base value to start from.
* @param {number} [offset=0]
* The optional value to add to `baseValue` (default is 0).
@westc
westc / destruct.js
Last active September 16, 2024 22:39
Destructures values from a `root` object based on one or more `nestedMaps` structures.
/**
* Destructures values from a `root` object based on one or more `nestedMaps`
* structures.
* @param {Object} root
* The root object or array containing the data to destructure.
* @param {...NestedMap} nestedMaps
* One or more nested mapping objects. Each key in the `NestedMap` is a
* string, and each value is either:
* - A string (which specifies the key in the result object), or
* - A `NestedMap` (which specifies further nested extraction).
@westc
westc / cwest-dark-mode.bookmarklet.js
Last active June 6, 2024 01:46
Bookmarklet - Dark mode code.
(()=>{
const docElem = document.documentElement;
const FILTERS = [
"",
"invert(1) hue-rotate(180deg) contrast(0.75)",
"invert(1) contrast(0.75)",
"invert(1) hue-rotate(180deg)"
];
const VAR_NAME = "cwestDarkMode";
const DATA_VAR_NAME = `data-${VAR_NAME.replace(/[A-Z]/g, '-$&').toLowerCase()}`;
@westc
westc / formatIntlDate.js
Last active April 20, 2024 03:48
A more intuitive version of a JS date formatter. This function leverages Intl.DateTimeFormat().
var formatIntlDate = (() => {
const codeToOpts = [...'Y=year M=month D=day H=hour h=hour m=minute s=second S:1 SS:2 SSS:3 MMM=month:short MMMM=month:long DDD=weekday:short DDDD=weekday:long A=hour:2D a=hour:2D Z:Offset ZZ:Offset ZZZ:Generic ZZZZ: J=year:numeric,month:2D,day:2D,hourCycle:h23,hour:2D,minute:2D,second:2D,fractionalSecondDigits:3,timeZoneName:longOffset'.replace(/(\w)=\w+(?= |$)/g, '$&:numeric $1$&:2D').replace(/hour/g, 'hourCycle:h23,$&').replace(/Z:/g, 'Z=timeZoneName:long').replace(/S:/g, 'S=fractionalSecondDigits:').replace(/2D/g, '2-digit').matchAll(/(\w+)=(\S+)/g)]
.reduce(
(codeToOpts, [_, code, strOpts]) => {
codeToOpts[code] = [...strOpts.matchAll(/(\w+):([^,]+)/g)].reduce(
(opts, [_, key, value]) => {
opts[key] = value;
return opts;
},
{}
@westc
westc / zip.cls
Last active March 28, 2024 22:14
Two implementations of zip() and zipKeys() in Apex (Salesforce).
/**
* Merges together the values of each of the arrays with the values at the
* corresponding position. Useful when you have separate data sources that are
* coordinated through matching array indexes.
*/
public static Object[][] zip(Object[][] listOfArrays) {
Object[][] rows = new List<Object[]>();
Integer itemsCount;
for (Object[] items : listOfArrays) {
Boolean isFirstPass = itemsCount == null;