Skip to content

Instantly share code, notes, and snippets.

@westc
westc / cookies.js
Created August 15, 2023 23:05
Functions to get, set and delete cookies.
/**
* Gets all of the cookies as an object.
* @returns {{[key: string]: string}}
* An object where the keys are the names of the cookies and the values are
* the corresponding values as strings.
*/
function getAllCookies() {
return Array.from(document.cookie.matchAll(/([^;\s=]*)=([^;\s=]*)/g)).reduce(
(byName, [_, key, value]) => {
byName[decodeURIComponent(key)] = decodeURIComponent(value);
@westc
westc / calcTileGrid.js
Last active August 8, 2023 05:23
Calculates the best tile grid based on the specified sizes, ratios and tile count.
/**
* Calculates the best tile grid based on the specified sizes, ratios and tile
* count.
*/
function calcTileGrid(gridWidth, gridHeight, ratioWidth, ratioHeight, tileCount) {
// Get the aspect ratio for the grid.
const gridAspect = gridWidth / gridHeight;
// Keep adding columns or rows until you have enough for the amount of
// tiles needed.
@westc
westc / nestCSS.js
Created July 17, 2023 17:22
nestCSS() - A function that makes it possible to create CSS stylesheet string based on a nested object.
/**
* Takes an object representing a nested CSS stylesheet and returns the
* corresponding normal CSS (non-nested) string.
* @param {CSSObject} objCSS
* @returns {string}
*/
function nestCSS(objCSS) {
/**
* @returns {string[]}
*/
@westc
westc / getRGBA.js
Created July 3, 2023 19:08
Takes a valid CSS color (eg. hsl(...), red, transparent, rgb(...)) and returns the corresponding rgba CSS color code while factoring the specified alpha.
var getRGBA = (() => {
const DIV = document.createElement('div');
DIV.style.display = 'none!important';
document.body.appendChild(DIV);
/**
* Takes a valid CSS color (eg. hsl(...), red, transparent, rgb(...)) and
* returns the corresponding rgba CSS color code while factoring the
* specified alpha.
* @param {string} cssColor
@westc
westc / onceReady.js
Created July 3, 2023 18:02
Determines if the DOMContentLoaded event was fired and if so it immediately calls the `callback` function, otherwise it will be called when the DOMContentLoaded event is fired off.
/**
* Determines if the DOMContentLoaded event was fired and if so it immediately
* calls the `callback` function, otherwise it will be called when the
* DOMContentLoaded event is fired off.
* @param {(immediate: boolean) => any} callback
* A function that is called with one argument which indicates if it was
* called immediately.
* @returns {boolean}
* A boolean indicating if the callback function is run immediately.
*/
@westc
westc / parseSOQLs.cls
Created June 28, 2023 21:27
Parses a SOQL query string into 0 or more SOQL query strings with all dash comments and block comments removed in Apex.
// Used by parseSOQLs() to parse SF queries by targetting strings, dash
// comments, block comments and semicolons.
static final Pattern RGX_SOQL_PARTS = Pattern.compile(
'(?s)\'(?:[^\\\\\']+|\\\\.)*\'|(--[^\\r\\n]*|/\\*.*?\\*/)|(\\s*;\\s*)'
);
/**
* Parses a SOQL query string into 0 or more SOQL query strings with all
* dash comments and block comments removed.
*/
@westc
westc / getAllRecords.cls
Created June 28, 2023 21:13
Gets all of the records of the specified SObject type.
/**
* Gets all of the records of the specified SObject type.
*/
static SObject[] getAllRecords(String sobjectName, String soqlSuffix, Map<String,Object> bindMap) {
String soql = String.join(new String[]{
'SELECT ' + String.join(new List<String>(Schema.getGlobalDescribe().get(sobjectName).getDescribe().fields.getMap().keySet()), ','),
'FROM ' + sobjectName
}, '\n');
if (!String.isBlank(soqlSuffix)) soql += '\n' + soqlSuffix;
System.debug(soql);
@westc
westc / translate.js
Created June 23, 2023 02:22
A translate function which takes a mapping and translates from the target string or RegExp match to the desired replacement.
/**
* Translates the substrings of a string from one substring or RegExp match
* another substring.
* @param {string} string
* The string to be updated.
* @param {[string|RegExp,Function|string][]|{[key: string]: Function|string}} mappings
* If this is an object it will be interpreted as if it was passed in as
* `Object.entries()` so that it is an array of arrays. For each subarray the
* first value will be searched for within string and the replacement will be
* determined by the second value of the subarray. If the second value of the
@westc
westc / copySign.js
Last active June 28, 2023 04:09
Efficient isNegative(), isPositive(), sameSign() and copySign() functions.
function copySign(toUpdate, signedNum) {
return toUpdate + 1 / toUpdate < 0 === signedNum + 1 / signedNum < 0
? toUpdate
: -toUpdate;
}
@westc
westc / chain.js
Last active June 20, 2023 14:23
Chain function calls by name.
/**
* @param {...(string|{source: (function|string), args?: any[], optional?: boolean, this?: (number|"this"), spreadThis?: boolean})} functions
* @returns {(input: any) => any}
*/
function chain(...functions) {
return input => {
for (let isFunctions = [], i = 0, l = functions.length; i < l; i++) {
let fn = functions[i];
const fnType = typeof fn;
if (fnType === 'string' || fnType === 'function') {