Skip to content

Instantly share code, notes, and snippets.

View simonespa's full-sized avatar

Simone Spaccarotella simonespa

View GitHub Profile
@simonespa
simonespa / event-emitter.js
Created June 8, 2020 11:28
Node's Event Emitter and "bind" function
const { EventEmitter } = require('events');
const em = new EventEmitter();
function eventListener(response) {
console.log(`Response Status: ${response.status}`);
}
function middleware(response) {
em.on('finish', eventListener.bind(null, response));
@simonespa
simonespa / filter.js
Created July 31, 2020 14:26
Dynamically filter object's properties in Javascript
function reducer(object, accumulator, current) {
// check if the object contains a property that matches the current filter
const hasOwnProperty = Object.prototype.hasOwnProperty.call(object, current);
// add it to the accumulator if it has, by dynamically selecting the computed property
return {
...accumulator,
...(hasOwnProperty && { [current]: object[current] })
};
}
@simonespa
simonespa / index.js
Created August 10, 2020 16:03
Catbox test
const { Client } = require("@hapi/catbox");
const CatboxRedis = require("@hapi/catbox-redis");
const RMSClient = require("@bbc/rms-client");
const stats = {
increment: (metric) => {
console.log(`stats.increment("${metric}")`);
},
timing: (metric, value) => {
console.log(`stats.timing("${metric}", ${value})`);
@simonespa
simonespa / basic-auth.js
Created August 10, 2020 16:51
Express JS
const express = require('express')
const basicAuth = require('express-basic-auth')
const app = express()
app.use('/secret', basicAuth({
users: { 'sounds': 'supersecret' },
unauthorizedResponse: (req) => {
return req.auth ? ('Credentials rejected') : 'No credentials provided'
},
challenge: true,
@simonespa
simonespa / scope-visibility.js
Created August 10, 2020 16:53
Scope vs. Visibility in Javascript
/**
* The scope of the first "one" variable is the region of the function a, included function b.
* Its visibility though doesn't coincide, because the second variable called "one" within the function "b"
* hides it. This doesn't mean that the scope is reduced.
*/
function a() {
const one = 1;
function b() {
const one = -1;
@simonespa
simonespa / validation.js
Created August 10, 2020 16:54
JSON schema validation
const Validator = require('jsonschema').Validator;
const v = new Validator();
// Address, to be embedded on Person
const addressSchema = {
id: '/SimpleAddress',
type: 'object',
properties: {
lines: {
type: 'array',
@simonespa
simonespa / main.cpp
Created August 10, 2020 17:32
Vector test in C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void vectorTest();
int main() {
vectorTest();
@simonespa
simonespa / Main.java
Created August 10, 2020 17:39
Kruskal
package prova;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@simonespa
simonespa / main.cpp
Created August 10, 2020 17:40
Merge C++
#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;
void readGroups(char** g1, char** g2, int& n, int& m) {
cout << "Numero di parole: ";
cin >> n;
cout << "Numero di caratteri: ";
cin >> m;
@simonespa
simonespa / op1.cpp
Last active August 10, 2020 17:49
Il cablatore
#include <iostream>
#include <cstring>
using namespace std;
struct Arco {
string nodo1;
string nodo2;
int peso;
};