Skip to content

Instantly share code, notes, and snippets.

@Dammmien
Dammmien / index.js
Last active May 24, 2019
Minimalist static file server
View index.js
#!/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 Apr 4, 2018
JavaScript Strategy design pattern
View Strategy.js
class ShoppingCart {
constructor(discount) {
this.discount = new discount();
this.amount = 0;
}
checkout() {
return this.discount.apply(this.amount);
}
@Dammmien
Dammmien / State.js
Created Mar 29, 2018
JavaScript State design pattern
View State.js
class OrderStatus {
constructor(name, nextStatus) {
this.name = name;
this.nextStatus = nextStatus;
}
doSomething() {
console.log('Do nothing by default');
}
@Dammmien
Dammmien / Prototype.js
Created Mar 24, 2018
JavaScript Prototype design pattern
View Prototype.js
class Sheep {
constructor(name, weight) {
Object.assign(this, { name, weight });
}
clone() {
return new Sheep(this.name, this.weight);
}
@Dammmien
Dammmien / Singleton.js
Created Mar 23, 2018
JavaScript Singleton design pattern
View Singleton.js
class Singleton {
constructor() {
if (typeof Singleton.instance === 'object') {
return Singleton.instance;
}
Singleton.instance = this;
return this;
@Dammmien
Dammmien / Builder.js
Last active Jul 7, 2018
JavaScript Builder design pattern
View Builder.js
class MealBuilder {
init() {
this.meal = new Meal();
}
addMain(type) {
this.meal.main = new MainItem(type);
}
@Dammmien
Dammmien / Factory.js
Last active Mar 23, 2018
JavaScript Factory design pattern
View Factory.js
class Car {
constructor(size, price, maxSpeed) {
this.size = size;
this.price = price;
this.maxSpeed = maxSpeed;
}
}
class CarFactory {
@Dammmien
Dammmien / euclidean.js
Created Mar 10, 2018
Get euclidean distance between two points A and B
View euclidean.js
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 Mar 9, 2018
Find unused files in a project (SVG in this example)
View files.sh
for FILE in $(find . -name "*.svg"); do
grep -qR $(basename "$FILE") * || echo "you can remove $FILE"
done
@Dammmien
Dammmien / mustache.md
Last active Aug 30, 2021
Mustache cheatsheet
View mustache.md

Basic tag

  Hello {{name}} !!

Comments

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