0.1.0 • Published 3 years ago

memoize-get v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

memoize-get

Memoize functions with the "get" function signature.

Installation

yarn add memoize-get

npm install memoize-get

Usage

import {memoizeGet, Get} from 'memoize-get';

const get: Get = (
  obj: object,
  path: string | number,
  fallback?: any,
) {
  const key = typeof path === 'string' ? path.split('.') : [path];

  for (index = 0; index < key.length; index += 1 ) {
    if (!obj) break;
    obj = obj[key[index]];
  }

  return obj === undefined ? fallback : obj;
}

const memoizedGet = memoizeGet(get);