Skip to content

Instantly share code, notes, and snippets.

View leonidkuznetsov18's full-sized avatar
:electron:
stay hungry, stay foolish

Leonid Kuznetsov leonidkuznetsov18

:electron:
stay hungry, stay foolish
View GitHub Profile
@leonidkuznetsov18
leonidkuznetsov18 / contentToFlatList.js
Created July 3, 2019 12:02
flat deep structure object arrays
const contentToFlatList = ({ content }, accumulator = []) =>
content.reduce((result, item) => {
if (item.content) {
return result.concat(contentToFlatList(item));
}
return result.concat([item]);
}, accumulator);
@leonidkuznetsov18
leonidkuznetsov18 / connect.js
Created December 4, 2019 10:55 — forked from ronen-e/connect.js
Redux connect implementation
function connect(mapStateToProps, mapDispatchToProps) {
return function connector(Component) {
class ContainerComponent extends React.Component {
componentDidMount() {
const { store } = this.context;
this.unsubscribe = store.subscribe(() => this.forceUpdate());
}
componentWillUnmount() {
this.unsubscribe();
@leonidkuznetsov18
leonidkuznetsov18 / sum-curry.js
Last active December 4, 2019 11:50
Sum Curry Functions
function sum(...arg) {
function sumReturn(...arg2) {
return sum(...arg, ...arg2)
}
let total = arg.reduce((total, val) => total + val);
sumReturn.value = total;
return sumReturn;
}
@leonidkuznetsov18
leonidkuznetsov18 / redux-connect.js
Created December 4, 2019 11:53
redux connect impementation
function connect(mapStateToProps, mapDispatchToProps) {
return function(WrappedComponent) {
return class extends React.Component {
constructor() {
const { store } = this.context;
this.unsubcribe = store.subcribe(this.handleChange)
}
componentWillUnmount() {
this.unsubcribe()
function foo(a, b) {
console.log("___A___", a);
console.log("___B___", b);
return a + b + this.c;
}
function bind(fn, ctx, ...args) {
if(typeof(fn) !== 'function') {
throw new TypeError("error");
}
@leonidkuznetsov18
leonidkuznetsov18 / styled-component-multiple-props.js
Last active January 18, 2020 10:46
styled components cleaner way for multiple props
import styled, { css } from 'styled-components'
const Button = styled.button(props => css`
height: ${props.height};
width: ${props.width};
color: ${props.theme.fontColor};
background-color: ${props.theme.backgroundColor};
`)
// OR
@leonidkuznetsov18
leonidkuznetsov18 / simple_memoization.js
Created April 29, 2020 12:26
simple memoization js
const memoize = (fn) => {
let cache = {};
const cacheFn = (...args) => {
let key = JSON.stringify(args);
if (key in cache) {
return cache[key]
}
return (cache[key] = fn.call(null, ...args));
}
return cacheFn;
const delayPromise = (delay) => {
return function(data) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(data);
}, delay);
});
}
};
const debounce = (fn, wait = 1) => {
let timeout = 0;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => fn.call(this, ...args), wait)
}
};
@leonidkuznetsov18
leonidkuznetsov18 / Eyeballing-This.md
Created May 27, 2020 12:52 — forked from zcaceres/Eyeballing-This.md
Understanding Binding and 'this' in Javascript

How to Eyeball Your ‘This’ Context in Javascript

The early programmer struggles with the Javascript keyword this. But understanding your this context is easier than it seems.

This is all about where a function is invoked. Often, early programmers worry about where the function was declared. Perhaps the function was declared in a specific file or a particular object. Surely this changes it's this!

Nope.

To understand this, we need to see where it is invoked. Nothing else matters, with one exception which we'll cover in a moment.