Skip to content

Instantly share code, notes, and snippets.

View SpenceDiNicolantonio's full-sized avatar

Spence DiNicolantonio SpenceDiNicolantonio

View GitHub Profile
@SpenceDiNicolantonio
SpenceDiNicolantonio / stop-propagation.ts
Created April 30, 2024 22:11
Sop Propagation directive #svelte #typescript #directive
export default function stopPropagation(fn: (e: Event) => void) {
return function (this: (e: Event) => void, event: Event) {
event.stopPropagation();
fn.call(this, event);
};
}
@SpenceDiNicolantonio
SpenceDiNicolantonio / click-outside.ts
Created April 30, 2024 22:06
Click Outside directive #svelte #typescript #directive
export default function clickOutside(node: HTMLElement) {
const handleClick = (event: MouseEvent) => {
if (!node.contains(event.target as Node)) {
node.dispatchEvent(new CustomEvent('outsideclick'));
}
};
// The node has been mounted to the DOM
window.addEventListener('click', handleClick);
@SpenceDiNicolantonio
SpenceDiNicolantonio / firebase.ts
Last active April 29, 2024 14:15
Typesafe Firestore collection #firebase #firestore #typescript
// A generic Firestore converter that provides type inference
export const genericConverter = <T>() => ({
toFirestore: (data: PartialWithFieldValue<T>) => data ?? {},
fromFirestore: (snap: QueryDocumentSnapshot) => snap.data() as T,
});
// A wrapper of collection() that uses a generic converter to provide type inference
export const typedCollection = <T>(db: Firestore, name: string) =>
collection(db, name).withConverter<T>(genericConverter<T>());
@SpenceDiNicolantonio
SpenceDiNicolantonio / .prettierrc
Last active April 28, 2024 04:15
Prettier config #config #template
{
"bracketSameLine": true,
"printWidth": 80,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all",
"useTabs": true
}
@SpenceDiNicolantonio
SpenceDiNicolantonio / tsv-to-json.js
Created May 27, 2021 13:14
[TSV to JSON] Convert a tab-separated-values file's contents to a set of JSON objects #json #tsv #lodash
import _ from 'lodash';
/**
* Converts a tab-separted-values (TSV) file to a set of JSON objects
* where each value is keyed according to the heading row
*/
const tsvToJson = tsv => {
// Split lines and filter any empties
const lines = tsv
.split('\n') // Split lines
@SpenceDiNicolantonio
SpenceDiNicolantonio / TestUtil.cls
Last active December 20, 2023 19:29
[Salesforce ID generator] Generates 15-character Salesforce IDs for testing #salesforce #apex
/**
* A collection of static utility methods for unit testing.
*/
@IsTest
public class TestUtil {
// ID generator configuration
private static final Integer ID_RESERVED_CHARACTERS = 7;
private static final Integer ID_INCREMENTER_PREFIX_LENGTH = 1;
private static final String BASE62_ALPHABET = '0123456789abcdefghijklmnopqrstuvwqyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
@SpenceDiNicolantonio
SpenceDiNicolantonio / HttpMockRegistry.cls
Last active July 29, 2022 18:03
[More Robust Mocking in Apex] A dynamic HTTP mock registry and a configurable Stub class to simplify and enhance mocking for Apex unit tests #salesforce #apex
/**
* A registry built around the Salesforce Mocking API that allows declarative mocking of HTTP callouts. Mocks responses
* can be registered either for a specific endpoint and path or for all paths on an endpoint, with the former taking
* precedence.
*/
@IsTest
public class HttpMockRegistry {
// Default mock response for HTTP requests
public static final HttpResponse DEFAULT_MOCK_RESPONSE = createSuccessResponse('Default mock response');
@SpenceDiNicolantonio
SpenceDiNicolantonio / Uuid.cls
Created July 5, 2019 19:04
[UUID Generator in Apex] #salesforce #apex
/**
* A UUID generator that can construct unique IDs in a variety of formats.
*
* A UUID is generated immediately upon instantiation of this class. The UUID can be retrieved in its normal form
* (e.g. f111b8c5-ca2f-4a1a-8d0d-a8dd5f37c05f) or as a shortened web-safe form (e.g. jp64hwPZ-Lh7vY8INQA7ImQPbQE), which
* is constructed by converting the UUID to Base64 and replacing '/' and '+' with '-' and '_', respectively.
*/
public class Uuid {
private static final String HEX_PREFIX = '0x';
@SpenceDiNicolantonio
SpenceDiNicolantonio / JsonNode.cls
Created July 5, 2019 18:41
[Traversable JSON (and XML) in Apex] Simulated tree structure to represent JSON or XML in a traversable and queriable way #salesforce #apex
/*
* A traversable JSON data structure.
*
* Conceptually, JSON is a general tree, or array of general trees. It is represented in Apex using Map<String, Object>
* for objects, List<Object> for arrays, String for strings, and either Integer or Decimal for numbers. This class wraps
* these data types in order to provide an interface for traversiing and querying the entire JSON structure.
*
* Each node contains a single value that must be a valid JSON value. That is, Map<String, Object>, List<Object>,
* String, Decimal, Integer, or null. When traversing or querying a JSON structure, any value returned is always wrapped
* in a JsonData instance to allow subsequent traversal or query.
@SpenceDiNicolantonio
SpenceDiNicolantonio / getType.cls
Created April 3, 2019 16:52
[Get instance type in Apex] A hack to determine the type of an instance in Apex #salesforce #apex
/*
* Returns the type of a given object. This is a hack around the fact that we can't access the type of an object.
* @param {Object} obj An object
* @returns {Type} the type of the object
*/
private static Type getType(Object obj) {
String typeName = 'Date';
// Attempt to cast the object to Datetime
// If it succeeds, the object is a date