plexis-js / plexis

Lo-fi, powerful, community-driven string manipulation library.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: `cache`

vorillaz opened this issue · comments

A caching mechanism is vital for string operations. Similar to lodash/memoize the caching mechanism can significantly improve the dev and user's experience and performance.

@param {Function} [func] The function to have its output memoized.
@param {Function} [resolver] The function to resolve the caching mechanism.
@returns {Function} Returns the new memoized function.

Example usage

import cache from '@plexis/cache';

const toLower = x => x.toLowerCase()
const toLowerCache = cache(toLower); 

// We are adding a dummy resolver function which returns the same cache every single time
const toLowerCacheWithResolver = cache(toLower, x => 'a'); 

toLowerCache('A'); // returns a;
toLowerCache('B'); // returns b;

toLowerCacheWithResolver('A'); // returns a;
toLowerCache('B'); // returns a !!;

Notes
The API should expose the caching layer per function, eg:

import cache from '@plexis/cache';

const toLower = x => x.toLowerCase()
const toLowerCache = cache(toLower); 

toLowerCache('A'); // returns a;
toLowerCache('B'); // returns b;

console.log(toLowerCache.cache) // outputs the cache object

Hint
Map works great for modern environments

Aliases

import cache from '@plexis/cache';
import {cache, memoize} from 'plexis';

Can I take a stab at this?