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
import React from "react";
import { useState, useEffect } from "react";
const render = data => match =>
data.pending ? match.pending()
: data.error ? match.error(data.error)
: data.data ? match.data(data.data)
: null // prettier-ignore
export const useMatchFetch = url => {
// referency https://www.interviewcake.com/concept/javascript/memoization
class Fibber {
constructor() {
this.memo = {};
}
fib(n) {
if (n < 0) {
throw new Error('Index was negative. No such thing as a negative index in a series.');
@marekdano
marekdano / reduce_with_asyncs.js
Created May 31, 2019 15:32
Run asynchronous functions in sequence
// Run asynchronous functions in sequence
const peopleArr = [
{
username: 'glestrade',
displayname: 'Inspector Lestrade',
email: '[email protected]',
authHash: 'bdbf9920f42242defd9a7f76451f4f1d',
lastSeen: '2019-05-13T11:07:22+00:00',
},
@marekdano
marekdano / index.js
Created February 13, 2019 10:12
logger with node eventEmitter
const Logger = require('./logger');
const logger = new Logger();
logger.on('message', data => console.log('Called Listener', data));
logger.log('Hello World');
logger.log('Hi');
logger.log('Hello');
<!-- The issue: alert displays 'undefined' instead of an item from prizes -->
<button id="btn-0">Button 1</button>
<button id="btn-1">Button 2</button>
<button id="btn-2">Button 3</button>
<script type="text/javascript">
const prizes = ['A Unicorn!', 'A Hug!', 'Fresh Laundry!'];
for (var btnNum = 0; btnNum < prizes.length; btnNum++) {
// For each of our buttons, when the user clicks it...
function deepClone(obj) {
var copy;
// Handle the 3 simple types, and null or undefined
if (null == obj || "object" != typeof obj) return obj;
// Handle Date
if (obj instanceof Date) {
copy = new Date();
copy.setTime(obj.getTime());
/**
* Function finds the node you are searching for and expand it and its parent.
*
* @param {TreeNode} node primeng tree node data
* @param {string} propField name of the property you are searching for ex: label
* @param {any} searchValue value you are searching for
*/
filterExpandRecursive(
node: TreeNode,
propField: string,
const flatten = filter => {
const filters = filter.filters;
if (filters) {
return filters.reduce((acc, curr) => acc.concat(curr.filters ? flatten(curr) : [curr]), []);
}
return [];
};
const rootFilters = {
filters: [
// reference https://medium.com/javascript-scene/master-the-javascript-interview-what-is-function-composition-20dfb109a1a0
const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);
const fn1 = s => s.toLowerCase();
const fn2 = s => s.split('').reverse().join('');
const fn3 = s => s + '!'
const newFunc = pipe(fn1, fn2, fn3);
const result = newFunc('Time'); // emit!
@marekdano
marekdano / app.js
Created May 29, 2018 22:23
js-the-best-practice (nodeschool)
var vendingMachine = require('./vendingMachine');
vendingMachine.insertCoin('q');
vendingMachine.insertCoin('q');
vendingMachine.insertCoin('q');
vendingMachine.insertCoin('q');
console.log("Insert 100");
console.log("Get product: ", vendingMachine.vendProduct('A1'));
console.log("Release: ", vendingMachine.releaseChange());