Skip to content

Instantly share code, notes, and snippets.

View munkacsitomi's full-sized avatar

Tamás Munkácsi munkacsitomi

  • Amsterdam, Netherlands
View GitHub Profile
@munkacsitomi
munkacsitomi / main.js
Created February 29, 2020 17:59
A Node.js application using Express.js
const port = 3000;
const express = require('express');
const app = express();
const getJSONString = (obj) => JSON.stringify(obj, null, 2);
app
.get('/', (req, res) => {
console.log(`Method: ${getJSONString(req.method)}`);
console.log(`URL: ${getJSONString(req.url)}`);
@munkacsitomi
munkacsitomi / log.js
Last active February 28, 2020 22:05
Simple Node.js Server
const http = require('http');
const httpStatus = require('http-status-codes');
const port = 3000;
const app = http.createServer();
const getJSONString = (obj) => JSON.stringify(obj, null, 2);
app.on('request', (req, res) => {
var body = [];
@munkacsitomi
munkacsitomi / messages.js
Created February 28, 2020 21:24
Module exports
exports.messages = ['Hello', 'This is a message', 'And another message'];
@munkacsitomi
munkacsitomi / add.js
Last active February 26, 2020 20:08
<3 JS
const add = (a) => (b) => a + b;
const addMore = (a) => (b) => (c) => add(a)(b) + c;
console.log(add(2)(4));
console.log(addMore(2)(4)(6));
// 6
// 12
@munkacsitomi
munkacsitomi / player-class.js
Last active February 26, 2020 19:37
Unit testing examples
class Player {
play(song) {
this.currentlyPlayingSong = song;
this.isPlaying = true;
}
pause() {
this.isPlaying = false;
}
@munkacsitomi
munkacsitomi / ninja-set.js
Last active February 26, 2020 20:47
Set example in JS
const ninjas = new Set(['Kuma', 'Hattori', 'Yagyu']);
const samurai = new Set(['Hattori', 'Oda', 'Tomoe']);
const pureNinjas = new Set([...ninjas].filter(ninja => !samurai.has(ninja)));
const everyone = new Set([...ninjas, ...samurai]);
console.log(pureNinjas.size === 2, 'There’s only one ninja samurai');
console.log(pureNinjas.has('Kuma'), 'Kuma is a true ninja');
console.log(pureNinjas.has('Yagyu'), 'Yagyu is a true ninja');
console.log(everyone.size === 5, 'Do not count twice Hattori, the ninja samurai');
@munkacsitomi
munkacsitomi / class-getter-setter.js
Last active February 26, 2020 17:54
Getter/Setter methods in JS
class NinjaCollection {
constructor() {
this.ninjas = ['Yoshi', 'Kuma', 'Hattori'];
}
get firstNinja() {
console.log('Getting firstNinja');
return this.ninjas[0];
}
set firstNinja(value) {
console.log('Setting firstNinja');
@munkacsitomi
munkacsitomi / class-inheritance.js
Created February 26, 2020 17:43
ES6 Class inheritance example
class Person {
constructor(name) {
this.name = name;
}
dance() {
return true;
}
}
@munkacsitomi
munkacsitomi / id-generator.js
Last active February 25, 2020 20:07
Generators
function* IdGenerator() {
let id = 0;
while (true) {
yield ++id;
}
}
const idIterator = IdGenerator();
const ninja1 = { id: idIterator.next().value };
@munkacsitomi
munkacsitomi / multi-max.js
Created February 25, 2020 19:28
How to use rest parameters
const multiMax = (first, ...remainingNumbers) => {
const max = remainingNumbers.sort((a, b) => b - a).find(a => a);
return first * max;
}
console.log(multiMax(3, 1, 2, 5, 4)); // 3 * 5 = 15