This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
############################################################################# | |
### 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Context { | |
[key: string]: any | |
} | |
const engine = pipeline<Context>((ctx, next) => { | |
ctx.foobar="baz"; | |
next(); | |
}) | |
engine.use((ctx, next) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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>; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = (req, res, next) => { | |
req.property = value; | |
next(); | |
} |
NewerOlder