Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍💻
Exploring...

Kiril Nedelev kiruh

👨‍💻
Exploring...
View GitHub Profile
@kiruh
kiruh / defaultdict.js
Last active Aug 6, 2020
JavaScript implementation of Python's defaultdict using Proxy and Reflect
View defaultdict.js
const defaultdict = (getDefault) =>
new Proxy(
{},
{
get: (target, key) => {
if (!Reflect.has(target, key))
Reflect.set(
target,
key,
typeof getDefault === "function" ? getDefault() : getDefault
@kiruh
kiruh / memoize.js
Last active Aug 14, 2019
Memoize decorator for ES6
View memoize.js
let disabled = false;
let disabledMap = new WeakMap();
let storage = new WeakMap();
const hasMemoizedProperty = (obj, propName) => {
if (!storage.has(obj)) storage.set(obj, {});
const props = storage.get(obj);
if (propName in props) return true;
return false;
};
const getMemoizedProperty = (obj, propName) => {
@kiruh
kiruh / combineRootWithReducers.js
Created Aug 2, 2019
Combine root reducer with nested reducers
View combineRootWithReducers.js
const combineRootWithReducers = ({
root = () => ({}), // reducer which should be on the root level of global state
reducers = {}, // other reducers
initial = {}, // initial state of global state
rootLast = false // if true, root reducer will be called after other reducers
}) => {
/*
this method allows you to combine basic reducers with a reducer which
manages state on the root level of global state
{
@kiruh
kiruh / AxiosTokenHelper.js
Last active Jul 14, 2019
This helper allows you to cancel old api calls, if new one occurs
View AxiosTokenHelper.js
export class AxiosTokenHelper {
/*
This helper allows you to cancel old api calls, if new
one occurs.
const helper = new AxiosTokenHelper();
const callToApi = async (...) => {
const source = helper.create();
try {
const response = await axios.get(
You can’t perform that action at this time.