Skip to content

Instantly share code, notes, and snippets.

View lcanady's full-sized avatar

Lem Canady lcanady

  • DigiBear Creative
  • Salem, OR
  • 03:57 (UTC -07:00)
  • X @lcanady
View GitHub Profile
class ListNode {
/**
* Create a new node
* @param {*} value The value to assign to the linked list.
* @param {ListNode | null} next The pointer to the next node in the chain
*/
constructor(value, next = null) {
// The value of the node. Can be anything.
this.value = value;
// the pointer to the next node in the chain.
// stairs = 4
// fun(stairs=4, curr = stairs)
// Start recursion
// we need to check for base case of recursion.
// if we hit the base case, then return a empty string.
// > if hits 0 return blank string
// base case: curr < 1 return ""
/*
#############################################################################
### Convenient mush-format #defines #########################################
This archive is my collection of shortcuts as a lazy MUSHCoder.
Mostly they are patterns I've run into over and over again through the
years that I finally got tired of typing.
Use anything you find here lightly. You don't want your code becoming
completely unreadable because it's full of #defines that noone can
@lcanady
lcanady / Graph.ts
Created March 29, 2021 22:23
A Simple Graph class, written with Typescript
export class Graph<T> {
private _adjList: Map<T, Set<T>>;
constructor() {
this._adjList = new Map<T, Set<T>>();
}
/**
* Add a Vert to the Graph
* @param v Vert to add
interface Context {
[key: string]: any
}
const engine = pipeline<Context>((ctx, next) => {
ctx.foobar="baz";
next();
})
engine.use((ctx, next) => {
@lcanady
lcanady / pipline.ts
Created December 7, 2020 22:49
The whole pipeline code
// First we declare our types.
export type Next = () => Promise<any> | any;
export type Middleware<T> = (
context: T,
next: Next
) => Promise<void>;
export type Pipe<T> = {
use: (...middlewares: Middleware<T>[]) => void;
execute: (context: T) => Promise<void | T>;
};
const execute: Pipe<T>["execute"] = async (context) => {
let prevIndex = -1;
/**
* Programatically go through each middleware and apply it to context before
* either moving on to the next middleware, or returning the final context
* object.
* @param index The current count of middleware executions.
* @param context The context object to send through the
* middleware pipeline.
@lcanady
lcanady / pipeline-use.ts
Last active December 7, 2020 20:55
A Middleware engine written in typescript
/**
* Declare a new middleware pipeline.
* @param middlewares A list of middlewares to add to the pipeline on
* instantiation.
*/
export function pipeline<T>(...middlewares: Middleware<T>[]): Pipe<T> {
const stack: Middleware<T>[] = middlewares;
/**
* Add middlewares to the pipeline.
export type Next = () => void | Promise<void>;
export type Middleware<T> = (context: T, next: Next) => Promise<void> | void;
export type Pipe<T> = {
use: (...middlewares: Middleware<T>[]) => void;
execute: (context: T) => Promise<void | T>;
};
@lcanady
lcanady / example.js
Created December 7, 2020 14:13
middleware tutorial example
module.exports = (req, res, next) => {
req.property = value;
next();
}