Skip to content

Instantly share code, notes, and snippets.

@westc
westc / isPrimitive.js
Created December 6, 2023 18:37
isPrimitive() - Determines if `input` is a primitive value or not.
/**
* Determines if `input` is a primitive value or not.
* @param {any} input
* The input value to test.
* @returns {boolean}
* Returns `true if `input` is a primitive, otherwise `false` is returned.
*/
function isPrimitive(input) {
if (input == null) {
// This is here to correctly handle document.all.
@westc
westc / Searcher.js
Last active November 29, 2023 05:53
A JavaScript class that can be used in a rudimentary search engine to test and find all matches for search terms entered.
class Searcher {
/** @type {string} */
#terms;
/** @type {boolean} */
#matchWordStart;
/** @type {boolean} */
#matchWordEnd;
/** @type {RegExp} */
#rgxFullNeg;
/** @type {RegExp} */
@westc
westc / FlowUtils.cls
Created November 7, 2023 21:20
Utility functions that can be called in Salesforce Apex flows.
public class FlowUtils {
@InvocableMethod(label='Format Date/Time')
public static String[] format(FormatInput[] inputs) {
String[] outputs = new String[0];
for (FormatInput input : inputs) {
outputs.add(input.dt.format(input.format, input.tz));
}
return outputs;
}
@westc
westc / KeySorter.cls
Created November 7, 2023 00:51
Apex - Makes it easier to sort an array of values by allowing you to specify 1 or more key values for each array value.
/**
* Makes it easier to sort an array of values by allowing you to specify 1 or
* more key values for each array value.
*/
public class KeySorter {
/**
* Represents a key that will be used to sort a value passed into `KeySorter`.
* This class is only addressable internally.
*/
private class ComparableKey implements Comparable {
@westc
westc / PatternReplacer.cls
Last active November 2, 2023 14:36
Makes it possible to easily loop through all of the instances of a pattern within a string and replace the matches with something different.
/**
* Author: Chris West
* Description:
* Makes it possible to easily loop through all of the instances of a pattern
* within a string and replace the matches with something different.
*/
public class PatternReplacer {
public Matcher matcher {get; private set;}
public String match {
get { return p_match; }
@westc
westc / JSONValue.cls
Last active October 28, 2023 02:41
Apex class that interprets JSON values and allows for easy traversal and type casting.
public class JSONValue {
private Object objValue;
public JSONValue(String serialized) {
this.objValue = JSON.deserializeUntyped(serialized);
}
private JSONValue(Object unserialized) {
this.objValue = unserialized;
}
@westc
westc / localizeJSON.js
Created October 20, 2023 14:09
Takes a date and returns it as a string in the format `YYYY-MM-DDTHH:mm:ss.SSSZ`. If the `timeZone` given is `"UTC"` or `"GMT"` the `Z` will remain as `"Z"`, otherwise it will be replaced with the timezone offset (eg. `"-05:00"`).
/**
* Takes a date and returns it as a string in the format
* `YYYY-MM-DDTHH:mm:ss.SSSZ`. If the `timeZone` given is `"UTC"` or `"GMT"`
* the `Z` will remain as `"Z"`, otherwise it will be replaced with the timezone
* offset (eg. `"-05:00"`).
* @param {Date} inputDate
* The date to convert to a JSON format.
* @param {?string=} timeZone
* Optional, defaults to the user's time zone. The time zone to use when
* getting the YMD format (eg. `"America/Los_Angeles"`).
@westc
westc / formatIntlDate.js
Created October 18, 2023 02:45
Uses the new Intl API to format a date as a string.
var formatIntlDate = (() => {
const CODE_TO_PROP_NAME = {
Y: ['year', 'numeric'],
M: ['month', 'long'],
D: ['day', 'numeric'],
WD: ['weekday', 'long'],
h: ['hour', '2-digit'],
m: ['minute', '2-digit'],
s: ['second', '2-digit'],
ms: ['fractionalSecondDigits', 3],
@westc
westc / readGSheetValues.js
Last active October 16, 2023 20:29
Reads the row values of the specified Google Sheet that was published to the web without needing an API key.
/**
* Reads the row values of the specified Google Sheet that was published to the
* web without needing an API key.
* @param {string} publishedURL
* The URL of the sheet that you want to read from as copied from the "Publish
* to the Web" modal in Google Sheets. To get the contents of a sheet other
* than the first one you must use the URL of that sheet when selecting it in
* the "Publish to the Web" modal in Google Sheets.
* @returns {Promise<(boolean|null|number|string)[][]>}
*/
@westc
westc / getComparer.js
Created October 14, 2023 04:14
Creates comparer functions that can be passed to an array's `sort` function to sort the values by the criteria specified.
/**
* Creates comparer functions that can be passed to an array's `sort` function
* to sort the values by the criteria specified.
* @param {...GetComparerPath} paths
* Each path indicates the path to traverse starting at `a` and `b` to find
* which underlying values should be compared to each other.
* @returns {(a: any, b: any) => (-1|0|1)}
* The comparer function with the path data baked in. The return function
* when called will return `-1` if `a` is less than `b`, it will return `1` if
* `a` is greater than `b` and in all other cases it will return `0`.