Skip to content

Instantly share code, notes, and snippets.

@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 / interview-coderpad.js
Last active March 4, 2024 01:06
Interviewing utilities
const { isEqual } = require('lodash');
const { bold, yellow, red, green } = require('chalk');
/**
* List of test outcomes
* @type {[('PASS' | 'FAIL'), string][]}
*/
const outcomes = [];
/**
@webbower
webbower / useConstant.js
Last active November 1, 2023 17:56
Custom hooks
import { useState, useRef } from 'react';
// Can't use React State because setting the constant to a function will execute that function in `useState`
// const useConstant = value => useState(value)[0];
const useConstant = value => useRef(value).current;
const myConst = useConstant('foo');
@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 / 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 / register.js
Last active March 13, 2023 22:50
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 / 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 / gist:c58c7bc6060be62d3fd2
Last active March 10, 2023 01:40
Dense Array Generation
Array.apply(null, Array(n));
Array.from({length: 10}, (_, i) => i + 1)