Skip to content

Instantly share code, notes, and snippets.

Avatar
💻
null !== undefined but null == undefined ¯\_(ツ)_/¯

Leo Lanese leolanese

💻
null !== undefined but null == undefined ¯\_(ツ)_/¯
View GitHub Profile
View function-pipe.js
const pipe = (...functions) => args => functions.reduce((arg, fn) => fn(arg), args);
// const league = [
// { heroe: "superman", power: 'fly' },
// { heroe: "batman", power: 'millionare'},
// { heroe: "flash", power: 'speed' },
// ]
// const filter = f => arr => arr.filter(f);
// const map = m => arr => arr.map(m);
View function-compose.js
const compose = (...functions) => args => functions.reduceRight((arg, fn) => fn(arg), args);
// Example:
// const league = [
// { heroe: "superman", power: 'fly' },
// { heroe: "batman", power: 'millionare'},
// { heroe: "flash", power: 'speed' },
//]
//const filter = f => arr => arr.filter(f);
//const map = m => arr => arr.map(m);
@leolanese
leolanese / Debounce_decorator.js
Created Aug 19, 2020
Debounce decorator Angular
View Debounce_decorator.js
/**
* Debounce a method
*/
function debounce(ms) {
return function(target: any, key: any, descriptor: any) {
const oldFunc = descriptor.value
const newFunc = _debounce(oldFunc,ms)
descriptor.value = function() {
return newFunc.apply(this,arguments)
@leolanese
leolanese / fetch-typeScript-async-await.ts
Created Aug 16, 2020
fetch API + async-await + TypeScript
View fetch-typeScript-async-await.ts
// declaring fetch + async-await + TypeScript
export async function get<T>(
path: string,
args: RequestInit = { method: "get" }
): Promise<HttpResponse<T>> {
return await http<T>(new Request(path, args));
};
export async function post<T>(
path: string,
View debounce.js
const debounce = (fn, time) => {
let timerId;
return function(...args) {
const functionCall = () => fn.apply(this, args);
clearTimeout(timerId);
timerId = setTimeout(functionCall, time);
};
};
View gist:7eeb38b700059630ccf71c60d6f071af
function startCountingForHideAndSeek() {
// State for managing cleanup and cancelling
let finished = false;
let cancel = () => finished = true;
const promise = new Promise((resolve, reject) => {
//
// Custom Promise Logic
//
// NOTE: This countdown not finish in exactly 10 seconds, don't build timers this way!
View quick_sort.js
'use strict';
function swap(arr, ind1, ind2) {
let temp = arr[ind1];
arr[ind1] = arr[ind2];
arr[ind2] = temp;
}
function quickSortInPlace(arr, left, right) {
left = left || 0;
@leolanese
leolanese / gist:dd5a196c183d9fb21942ae4f39a0911c
Created Jul 2, 2020
Depth First Search Data Structures algorithm (DFS)
View gist:dd5a196c183d9fb21942ae4f39a0911c
## DFS & BFS:
DFS = storing the vertices "stacks", stacks are LIFO (push, pop).
BFS = storing the vertices "queue", queues are FIFO (shift, unshift or enqueue and dequeue).
## Why to use BFS and DFS?
#### DFS = memory requirements than BFS. It will take a lot of memory to traverse deeper trees while it works well with wider trees.
commonly used when you need to search the entire tree.
#### BFS = is better for deeper trees since it will take a lot more memory to operate on wider trees.
## When to use BFS and DFS?
@leolanese
leolanese / create-react-app-scss
Created Jun 27, 2020
Create React App with SCSS (out of the box)
View create-react-app-scss
npx create-react-app my-app
cd my-app
yarn add node-sass -S
// Change .css files to .scss
// Change any imports to use .scss
yarn start
@leolanese
leolanese / delayPromise,js
Last active May 18, 2020
delay a Promise and log the time it takes to fulfill (legacy way)
View delayPromise,js
function delayPromise(delay, value) {
console.time();
// using built-in Promise constructor-function/class.
return new Promise(function(resolve) {
setTimeout(function() {
console.timeEnd();
resolve(value);
}, delay);
});
}