Skip to content

Instantly share code, notes, and snippets.

View luizcalaca's full-sized avatar

Luiz Calaça luizcalaca

View GitHub Profile
@luizcalaca
luizcalaca / server_http.js
Created December 23, 2021 21:39
A simple http server for NodeJS
const http = require('http')
const port = 3000
const server = http.createServer((req, res) => {
res.statusCode = 200
res.setHeader('Contenty-Type', 'text/html')/
res.end('Hi, one messagem from Node with HTML')
})
@luizcalaca
luizcalaca / database.test.js
Created December 27, 2021 18:47
Testing a database with Jest
const db = require('./database');
beforeAll(async () => {
await db.sequelize.sync({ force: true });
});
test('create person', async () => {
expect.assertions(1);
const person = await db.Person.create({
id: 1,
@luizcalaca
luizcalaca / pure_impure.js
Last active January 4, 2022 06:30
Functional programming - impure and pure function
//Referential transparency in computer science
// Function is pure if the return value is got just trough its own entry values, without side effects.
// Above is a impure function because depends on PI value (something extern)
const PI = 3.14
function calculator_area(radius){
return radius * radius * PI
}
@luizcalaca
luizcalaca / currying.js
Created December 31, 2021 20:19
Javascript Currying
//returning many functions result
function sum(a){
return function(b){
return function(c){
return a + b + c
}
}
}
@luizcalaca
luizcalaca / memoization.js
Created January 1, 2022 14:52
Memoization technique on Javascript
//Memoization is an optimization technique used primarily to speed up computer programs by storing the results
//of expensive function calls and returning the cached result when the same inputs occur again
//That's can be applied for calculating Fibonacci and Factorial
const memoizedSum = () => {
let cache = {};
return (n) => {
if (n in cache) {
@luizcalaca
luizcalaca / boolean_lambda_calculus.js
Last active January 4, 2022 06:16
Javascript boolean type - using Lambda Calculus
//True returns the first parameter
const True = a => b => a
//False returns the second parameter
const False = a => b => b
//messages to show represeting each state
const isTrue = () => "it's true!"
const isFalse = () => "it's false!"
@luizcalaca
luizcalaca / lambda_and_or.js
Last active January 4, 2022 06:30
Lambda Calculus - AND and OR operators - Javascript
//True returns the first parameter
const True = a => b => a
//False returns the second parameter
const False = a => b => b
//messages to show represeting each state
const isTrue = () => "it's true!"
const isFalse = () => "it's false!"
@luizcalaca
luizcalaca / parallel_promise.js
Last active January 4, 2022 06:49
Parallel processing - Promise - Javascript
//Make all the queries in parallel (asynchronously). Resulting in each query firing at the same time.
let promise1 = new Promise((resolve) => resolve(executeQuery(...))
let promise2 = new Promise((resolve) => resolve(executeQuery(...))
let promise3 = new Promise((resolve) => resolve(executeQuery(...))
let responses = await Promise.all([promise1, promise2, promise3, ...])
for(let response of responses) {
// some processing
}
@luizcalaca
luizcalaca / iife.js
Created January 5, 2022 12:04
IIFE - Immediately Invoked Function Expression
//IIFE (Immediately Invoked Function Expression) é uma função em JavaScript que é executada assim que definida.
/* É um Design Pattern também conhecido como Self-Executing Anonymous Function e contém duas partes principais.
A primeira é a função anônima cujo escopo léxico é encapsulado entre parênteses. Isso previne o acesso externo
às variáveis declaradas na IIFE, bem como evita que estas variáveis locais poluam o escopo global.
*/
//A segunda parte corresponde à criação da expressão (), por meio da qual o interpretador JavaScript avaliará e
//executará a função.
@luizcalaca
luizcalaca / multer.js
Last active January 19, 2022 23:46
Javascript - multer uploading file - rename file
const express = require('express');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
// Configuramos o upload como um middleware que