Skip to content

Instantly share code, notes, and snippets.

@webbower
webbower / ts-magic.ts
Created August 19, 2023 00:34
TypeScript Magic
/* Combined Union Type + Array of allowed values */
const ServerEnvironments = ['development', 'test', 'stage', 'production'] as const;
// equivalent to `type ServerEnvironments = 'development' | 'test' | 'stage' | 'production'
type ServerEnvironments = typeof ServerEnvironments[number];
const serverEnv: ServerEnvironments = 'development';
const isValidServerEnv = (env: string): env is ServerEnvironments => ServerEnvironments.include(env);
@webbower
webbower / module-directories.md
Last active June 5, 2023 18:38
General Coding Guidelines

Module Directories

@webbower
webbower / proto-boilerplate.js
Created March 14, 2023 23:46
JS Prototype boilerplate
const nodeCustomInspect = Symbol.for('nodejs.util.inspect.custom');
const denoCustomInspect = Symbol.for("Deno.customInspect");
const Ctor = () => ({});
Object.assign(Ctor, {
prototype: {
constructor: Ctor,
toString() {
@webbower
webbower / process-dot-env-types.ts
Created March 13, 2023 22:31
TypeScript boilerplate nonsense
export {};
/** @see https://bobbyhadz.com/blog/typescript-process-env-type */
declare global {
namespace NodeJS {
interface ProcessEnv {
// Added keys for process.env
}
}
}
@webbower
webbower / register.js
Last active April 16, 2024 21:27
Native JS extensions 😱
/**
* @see https://stackoverflow.com/a/3561711
*/
RegExp.xEscape = function escape(string) {
return string.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
};
Function.prototype.xPartial = function partial(...args) {
return this.bind(null, ...args);
};
@webbower
webbower / react-hooks.test.ts
Last active July 9, 2022 00:45
Webbower Standard Library
import { describe } from 'src/utils/testing';
import { renderHook } from '@testing-library/react-hooks';
//////// useConstant.test.ts ////////
import useConstant from 'src/hooks/useConstant';
describe('useConstant()', async assert => {
const testStateValue = { value: 'abc' };
@webbower
webbower / utility-types.ts
Last active August 21, 2023 23:55
Various simple utility types for improved codebase semantics
/**
* A function that converts one value of type A to another value or the same or a different type B
*
* Use with functions like:
* - @see Array.prototype.map()
* - `pipe()` or `compose()` functions
*/
export type Mapper<A = unknown, B = unknown> = (val: A) => B;
/**
@webbower
webbower / loop.js
Last active March 14, 2024 17:47
JS Utilities
// Loop function inspired by Lisp lop macro
// @see https://lispcookbook.github.io/cl-cookbook/iteration.html
const isStepLoop = config => false;
const isIterableLoop = config => false;
const isCountedLoop = config => false;
const loop = (config, onIter = x => x) => {
const {
// Loop from `from` to `to` by `step`
from,
@webbower
webbower / states_hash.json
Created September 11, 2021 23:22 — forked from mshafrir/states_hash.json
US states in JSON form
{
"AL": "Alabama",
"AK": "Alaska",
"AS": "American Samoa",
"AZ": "Arizona",
"AR": "Arkansas",
"CA": "California",
"CO": "Colorado",
"CT": "Connecticut",
"DE": "Delaware",
@webbower
webbower / 01-handle-application-response-codes.js
Last active September 24, 2021 21:23
Complex Promise handling examples
zhuLiDoTheThing(args)
// Application status codes can come from HTTP 2xx, 4xx, and 5xx responses. This `.then()` will attempt to resolve
// to Promise<codeString> or reject with an error if the code is missing from the response body.
.then(
response => {
if (response.code) {
// Forward the code from the response
return response.code;
}