Skip to content

Instantly share code, notes, and snippets.

@westc
westc / sort.js
Last active November 17, 2023 02:35
Sorts an array of values using the criteria specified in a function. This is similar to the Python sort function in that `criteria` is run once for each value and then the return is used to sort the original values. Quick sort is used to sort the values.
/**
* Sorts an array of values using the criteria specified in a function. This is
* similar to the Python sort function in that `criteria` is run once for each
* value and then the return is used to sort the original values. Quick sort is
* used to sort the values.
* @template T
* @param {T[]} values
* The array of values that will be directly sorted and returned.
* @param {(value:T,index:number,values:T[])=>any} criteria
* A function that will return a value for each value in `values` indicating
@westc
westc / quickSort.js
Created October 10, 2023 15:56
A quick implementation of quick sort.
/**
* A quick implementation of quick sort.
* @template T
* @param {T[]} values
* The values to sort.
* @returns {T[]}
* The sorted array of values.
*/
function quickSort(values) {
const segments = [{start: 0, end: values.length - 1}];
@westc
westc / parseArray.js
Created October 6, 2023 18:03
Takes a value and turns it into an array if it isn't already.
/**
* Takes a value and turns it into an array if it isn't already.
* @param {*} value
* The value that should be turned into an array. If this is already an
* array just return it. If this is a non-iterable value or is a string
* literal the returned value will be an array containing this value.
* Otherwise if this is iterable it will be returned as a new array.
* @returns {any[]}
* If `value` is already an array it will be returned, otherwise return a
* new array representing `value`.
.addSelect({
name: 'Value Field',
path: 'valueField',
category: ['Display'],
description: 'The field containing the values that will be represented by the bars on the bar chart.',
settings: {
options: [],
getOptions(context) {
const results = context.data[0]?.fields.map(f => ({value: f.name, label: `${f.name} (${f.type})`})) ?? [];
return results as any as Promise<SelectableValue[]>;
@westc
westc / request.js
Last active October 13, 2023 15:10
Sends a request to the specified URL.
/**
* Sends a request to the specified URL.
* @param {string} url
* The URL to which the request will be sent.
* @param {?request__Options=} options
* The various options to set on the request.
* @returns {Promise<XMLHttpRequest>}
* A promise of the corresponding `XMLHttpRequest`.
*/
function request(url, options) {
@westc
westc / pollChange.js
Last active September 7, 2023 16:05
Continually polls for a change in a simple value (eg. primitive) returned by a function. Good for setting up a fake listener on something doesn't have another way to add a real event listener.
/**
* Continually calls `getValue()` with a state object that can be modified and
* the returned value will be compared to what was previously returned. If the
* return value is different than the previous, the `callback()` function will
* be called.
* @param {(currValue: any, state: State) => Value} getValue
* A function that is called after every interval (as specified by
* `msInterval`). The arguments passed will be (1) the previous value
* returned by this function and (2) the state object (which on the initial
* call starts off as an empty object but can be augmented). The return value
@westc
westc / diff.js
Created September 3, 2023 00:36
Determines the difference between 2 simple values.
function diff(left, right) {
function recurse(left, right, leftSeen, rightSeen, path) {
if (left === right || (left !== left && right !== right)) return [];
// If the constructors are different or if left and right are null and
// undefined but they are not the same...
if (left.constructor !== right?.constructor || left == null) {
return [{left, right, path, message: 'The values are not of the same type.'}];
}
@westc
westc / infer.js
Last active August 23, 2023 20:50
Tries to turn a string value that looks like a boolean, number, null, undefined or BigInt into one of those types.
/**
* Tries to turn a string value that looks like a boolean, number, null,
* undefined or BigInt into one of those types.
* @template {string} T
* @param {T} input
* @param {boolean} parseUrlBools
* @return {T|boolean|number|null|undefined|BigInt}
*/
function infer(input, parseUrlBools) {
if (input && 'string' === typeof input) {
@westc
westc / RegExp.cls
Last active August 22, 2023 19:45
A wrapper class for making it easier to work with regular expressions and also for accessing named capture groups (which wasn't when this solution was written).
/**
* Author: Chris West
* Source: https://gist.github.com/westc/a921e805036707d7c5f818a32124789a#file-regexp-cls
* Description:
* A wrapper class for making it easier to work with regular expressions and
* also for accessing named capture groups (which wasn't when this solution
* was written).
*/
public class RegExp {
private Pattern pat;
@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);