Skip to content

Instantly share code, notes, and snippets.

View theScottyJam's full-sized avatar

Scotty Jamison theScottyJam

View GitHub Profile
@theScottyJam
theScottyJam / sortedUniqBenchmarks.js
Created February 18, 2023 17:39
sorted uniq benchmarks
const _ = require('lodash');
// Alter these values to see how it affects the overall performance.
const NUMBER_OF_UNIQUE_ITEMS = 100;
const NUMBER_OF_DUPLICATES_PER_ITEM = 10000;
const TRIALS = 100;
function buildArray() {
const result = [];
for (let i = 0; i < NUMBER_OF_UNIQUE_ITEMS; ++i) {
@theScottyJam
theScottyJam / exampleData.ts
Created January 31, 2023 02:00
Example of using the return-your-exceptions pattern
import type { User } from './userManager';
export const users: Map<string, User> = new Map(Object.entries({
'1': {
id: '1',
username: 'admin',
permissions: ['VIEW', 'UPDATE'],
},
'2': {
id: '2',
@theScottyJam
theScottyJam / strategyPattern.js
Last active November 10, 2021 15:40
Article: Composition Alone Can't Replace Inheritance - strategy pattern
class Monster {
#onAttack
constructor({ dropLoot, onAttack, customBehaviors = {} }) {
this.dropLoot = dropLoot
this.#onAttack = onAttack ?? (() => {})
this.behaviors = customBehaviors
}
attack() {
playHurtAnimation()
@theScottyJam
theScottyJam / runtimeHierarchyCheck.js
Created October 25, 2021 13:59
Article: Composition Alone Can't Replace Inheritance - runtime hierarchy check
const monsterSentinel = {}
export const isMonster = obj => obj.typeSentinel === monsterSentinel
export class Slime {
typeSentinel = monsterSentinel
...
}
export class Skeleton {
@theScottyJam
theScottyJam / publicHelpers.js
Created October 25, 2021 13:58
Article: Composition Alone Can't Replace Inheritance - public helpers
// Public helpers
export const monsterBehaviors = {
kill(monster) {
userInventory.add(monster.dropLoot())
removeFromUi(this)
}
}
// private helpers
const monsterHelpers = {
@theScottyJam
theScottyJam / restrictedInheritance.js
Last active November 10, 2021 15:16
Article: Composition Alone Can't Replace Inheritance - restricted inheritance
const monsterHelpers = {
attack() {
playHurtAnimation()
}
}
class MonsterBehaviors {
#id = Math.random()
#dropLoot
@theScottyJam
theScottyJam / classicComposition.js
Last active November 10, 2021 15:14
Article: Composition Alone Can't Replace Inheritance - classic composition
class MonsterBehaviors {
#dropLoot
constructor({ dropLoot }) {
this.#dropLoot = dropLoot
}
attack() {
playHurtAnimation()
}
@theScottyJam
theScottyJam / helperFunctions.js
Last active July 19, 2022 14:44
Article: Composition Alone Can't Replace Inheritance - helper functions
const monsterBehaviors = {
attack() {
playHurtAnimation()
},
die({ monsterId, dropLoot }) {
userInventory.add(dropLoot())
removeFromUi(monsterId)
},
}
@theScottyJam
theScottyJam / originalInheritanceExample.js
Last active October 25, 2021 14:02
Article: Composition Alone Can't Replace Inheritance - original inheritance example
export class Monster {
constructor() {
if (this.constructor === Monster) {
throw new Error('This is an abstract class. Do not instantiate it directly.');
}
}
attack() {
playHurtAnimation()
}
@theScottyJam
theScottyJam / exceptions.js
Last active October 13, 2021 23:08
Light version of the explicit-exceptions API. Designed for those would rather maintain a simplified version of the library than add it as a dependency. This gist is licensed under the MIT license.
'use strict'
// This module provides a light version of the explicit-exceptions API.
// You can find the original package this module was modeled after
// at this URL: https://www.npmjs.com/package/explicit-exceptions
/**
* An exception that can be thrown (or rethrown) within a function decorated with wrap().
*/
class Exception extends Error {