Skip to content

Instantly share code, notes, and snippets.

View marekdano's full-sized avatar
😁
coding for 🚀

Marek Dano marekdano

😁
coding for 🚀
View GitHub Profile
function debounce(callback, timer) {
let timeoutId;
return (...args) => {
// save the current context (this)
const context = this;
// clear the existing timeout
clearTimeout(timeoutId);
@marekdano
marekdano / countries.js
Last active May 18, 2022 10:18
List of countries with country code
const countries = [
{ value: 'AFG', name: 'Afghanistan' },
{ value: 'ALA', name: 'Aland Island' },
{ value: 'ALB', name: 'Albania' },
{ value: 'DZA', name: 'Algeria' },
{ value: 'ASM', name: 'American Samoa' },
{ value: 'AND', name: 'Andorra' },
{ value: 'AGO', name: 'Angola' },
{ value: 'AIA', name: 'Anguilla' },
{ value: 'ATG', name: 'Antigua and Barbuda' },
@marekdano
marekdano / conditional-props.ts
Last active April 10, 2021 19:15
React TypeScript conditional props - props that depend on other props
// all credits for this gist goes to Bruno's video
// https://www.youtube.com/watch?v=vXh4PFwZFGI
type DrawerProps = { fullName: string } & (
| { shape: 'circle'; radius: number }
| { shape: 'square'; width: number }
| { shape: 'rectangle'; width: number; height: number }
);
<Drawer fullName="Marek Dano" shape="rectangle" width={5} height={4} />
const factorial = num => (num > 1 ? num * factorial(num - 1) : 1)
const memoize = fn => {
const cache = {}
// with multiple arguments
// return (...args) => {
// const key = JSON.stringify(args)
// return key in cache ? cache[key] : cache[key] = fn.apply(null, args)
// }
@marekdano
marekdano / curry.js
Created May 19, 2020 11:33
Curry in Javascript
const curry = (fn) => {
const curried = (...args) => {
if (args.length >= fn.length) {
return fn(...args)
} else {
return (...innerArgs) => curried(...args, ...innerArgs)
}
}
return curried
}
@marekdano
marekdano / partial.js
Created May 13, 2020 17:50
Partial Application
const multiply = (a, b) => a * b
function prefillFunction (fn, prefilledValue) {
const inner = liveInput => {
const output = fn(liveInput, prefilledValue)
return output
}
return inner
}
@marekdano
marekdano / generics.ts
Last active May 13, 2020 11:36
TS samples
function fail(arr: never): never {
return new Error;
}
function pluck<T, K extends keyof T>(obj: T, propertyNames: K[]): T[K][] {
return propertyNames.map(key => obj[key])
}
interface Author {
name: string;
@marekdano
marekdano / todolist.js
Created March 31, 2020 17:17
Simple Todo list app using React Hooks with CRUD functions edit, delete a todo and/or mark a todo as done.
import React, { useState, useRef, useEffect } from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faCheck } from "@fortawesome/free-solid-svg-icons";
import { library } from "@fortawesome/fontawesome-svg-core";
import "./styles.css";
library.add(faCheck);
/*
@marekdano
marekdano / array-functions.js
Created November 7, 2019 13:24
Create map, filter and reduce array functions
// MAP
function map(array, callbackFunction) {
var newArray = [];
for (var i = 0; i < array.length; i++) {
newArray[i] = callbackFunction(array[i]);
}
return newArray;
}
// Test
// write function for fibonacci numbers where
// F[1]=F[2]=1; F[3]=2; F[4]=3; => 1,1,2,3,5,8,...
const fibRec = n => {
if (n === 1 || n === 2) {
return 1
}
return fibRec(n-1) + fibRec(n-2)
}
const fibImp = n => {