Skip to content

Instantly share code, notes, and snippets.

Avatar

Chris West westc

View GitHub Profile
@westc
westc / orderLike.py
Last active Aug 5, 2020
Order (or sort) one list the same way that a second list is ordered. Orders multiple identical values correctly.
View orderLike.py
def orderLike(unordered:list, ordered:list):
"""
Takes 2 lists where the first list is the unordered list that should be
ordered similar to the second list which is in the correct order. This
solution handles identical values appearing in the ordered list. Returns a
new list without modifying the lists that were passed in.
"""
result = []
unordered = unordered[:]
for ordered_value in ordered:
@westc
westc / apexify.js
Last active Jul 23, 2020
Convert a JSONifiable value into valid Apex (Salesforce).
View apexify.js
var apexify;
(function() {
apexify = function(value, indent) {
indent = indent || 0;
let indentation = 'string' === typeof indent
? indent
: new Array(indent + 1).join(' ');
let newLine = indentation ? '\n' : '';
let allSpacing = indentation ? newLine + indentation : '';
let space = indentation ? ' ' : '';
@westc
westc / flattenKeys.js
Last active Jun 10, 2020
Assuming that the value passed to flattenKeys() is not recursive, this function will recursively flatten all of the underlying keys for objects and arrays.
View flattenKeys.js
var flattenKeys;
(function() {
function recurseFlat(value, path) {
var iterType = Array.isArray(value)
? 'A'
: (value && 'object' === typeof value);
if (iterType) {
var keys = iterType === 'A'
? value.map(function(v,i){return i})
: Object.keys(value);
@westc
westc / testing-strings.js
Created Jun 2, 2020
Regular Expressions - JavaScript Examples & Python Examples
View testing-strings.js
let rgx = /[aeiou]{2,}/;
let numbers = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"];
let filteredNumbers = numbers.filter(n => rgx.test(n)); // ["Three", "Four"]
@westc
westc / MIN_SAFE_INTEGER-MAX_SAFE_INTEGER.inc.php
Created May 31, 2020
Calculates the minimum and the maximum safe integer values which can vary depending on the environment where the code is run. This can be used to have constants similar to JavaScript's Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER.
View MIN_SAFE_INTEGER-MAX_SAFE_INTEGER.inc.php
<?php
// Similar to JavaScript's Number.MAX_SAFE_INTEGER
define("MAX_SAFE_INTEGER", (function() {
for ($e = 1; pow(2, $e) - ($MAX_SAFE_INTEGER = (pow(2, $e - 1) - 1) * 2 + 1) == 1; $e++) {
if ($e > 999) {
throw new Exception("Maximum integer couldn't be found.");
}
}
return $MAX_SAFE_INTEGER;
})());
@westc
westc / YourJS.divMod.js
Last active May 25, 2020
Definition for divMod() in JavaScript. Differs from python's version due to how modulo is defined by the language.
View YourJS.divMod.js
function divMod(a, b) {
return [Math.trunc(a / b), a % b];
}
@westc
westc / encodeStub-decodeStub.js
Last active May 22, 2020
Can create stubs that look random to the average Joe.
View encodeStub-decodeStub.js
var encodeStub, decodeStub;
(function() {
function encodeNumber(number, minNumber, maxNumber, maxPerSeries) {
var offset = minNumber;
var limitNumber = maxNumber - offset + 1;
var maxMaxPerSeries = Math.ceil(limitNumber / 2);
maxPerSeries = Math.floor(maxPerSeries);
if (!(1 <= maxPerSeries && maxPerSeries <= maxMaxPerSeries)) {
throw new Error('Since there are ' + limitNumber + ' possible values the maxPerSeries must be between 1 and ' + maxMaxPerSeries + '.');
@westc
westc / Get-First-Externally-Called-Name.py
Created May 11, 2020
Gets the first function that was externally called. In other words get the name of the first function in the chain of calls that is not defined in this file.
View Get-First-Externally-Called-Name.py
import inspect
def getFirstExternallyCalledName() -> str:
"""
Gets the name of the first function called externally in a chain of
called functions within this file.
"""
# Loop through and get the first caller function not originating
# from this file.
frame = inspect.currentframe()
@westc
westc / CWestUtils.apxc.java
Last active Jul 23, 2020
Some Salesforce Apex utility functions that I can use to retrieve objects via their ID and more to come.
View CWestUtils.apxc.java
public class CWestUtils {
/**
* Gets the sObject with all of its fields from just the ID.
* @param oId
* The ID of the object to retrieve.
* @return
* The object with the specified ID. All of its fields will be included.
*/
public static sObject getSObjectById(ID oId) {
return getSObjectById(oId, null);
@westc
westc / getMultiPromise.js
Last active Apr 6, 2020
Gets a single promise for multiple function returning their own promises. Allows for sequential promise execution and asynchronous promise execution.
View getMultiPromise.js
/**
* Gets a single promise for multiple function returning their own promises.
* Allows for sequential promise execution and asynchronous promise execution.
* @param {Array<(function():Promise|Array<function():Promise>)>} promiseMakers
* An array which can contain functions and arrays of functions. Each
* top-level function when called must return a promise that will be waited
* for sequentially. In the case of an item in the top-level array being an
* array of functions, each of thise functions must return promises but all of
* these promises will be executed asynchronously. Each item in this
* top-level array will wait for the previous item. If a returned promise
You can’t perform that action at this time.