Skip to content

Instantly share code, notes, and snippets.

@Dammmien
Dammmien / index.js
Last active May 24, 2019 16:46
Minimalist static file server
#!/usr/bin/env node
const http = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');
const [ runtime, scriptName, port = 3000, folder = process.cwd() ] = process.argv;
class Server {
@Dammmien
Dammmien / Strategy.js
Created April 4, 2018 21:22
JavaScript Strategy design pattern
class ShoppingCart {
constructor(discount) {
this.discount = new discount();
this.amount = 0;
}
checkout() {
return this.discount.apply(this.amount);
}
@Dammmien
Dammmien / State.js
Created March 29, 2018 15:19
JavaScript State design pattern
class OrderStatus {
constructor(name, nextStatus) {
this.name = name;
this.nextStatus = nextStatus;
}
doSomething() {
console.log('Do nothing by default');
}
@Dammmien
Dammmien / Prototype.js
Created March 24, 2018 00:09
JavaScript Prototype design pattern
class Sheep {
constructor(name, weight) {
Object.assign(this, { name, weight });
}
clone() {
return new Sheep(this.name, this.weight);
}
@Dammmien
Dammmien / Singleton.js
Created March 23, 2018 23:09
JavaScript Singleton design pattern
class Singleton {
constructor() {
if (typeof Singleton.instance === 'object') {
return Singleton.instance;
}
Singleton.instance = this;
return this;
@Dammmien
Dammmien / Builder.js
Last active July 7, 2018 23:14
JavaScript Builder design pattern
class MealBuilder {
init() {
this.meal = new Meal();
}
addMain(type) {
this.meal.main = new MainItem(type);
}
@Dammmien
Dammmien / Factory.js
Last active March 23, 2018 23:06
JavaScript Factory design pattern
class Car {
constructor(size, price, maxSpeed) {
this.size = size;
this.price = price;
this.maxSpeed = maxSpeed;
}
}
class CarFactory {
@Dammmien
Dammmien / euclidean.js
Created March 10, 2018 14:03
Get euclidean distance between two points A and B
const getDistance = (A, B) => Math.sqrt( Math.pow( B.x - A.x, 2 ) + Math.pow( B.y - A.y, 2 ) );
@Dammmien
Dammmien / files.sh
Created March 9, 2018 19:38
Find unused files in a project (SVG in this example)
for FILE in $(find . -name "*.svg"); do
grep -qR $(basename "$FILE") * || echo "you can remove $FILE"
done
@Dammmien
Dammmien / mustache.md
Last active April 27, 2023 22:41
Mustache cheatsheet

Basic tag

  Hello {{name}} !!

Comments

 {{! This is a comment, and it won't be rendered }}