UNPKG

3.5 kBMarkdownView Raw
1# `@shopify/function-enhancers`
2
3[![Build Status](https://travis-ci.org/Shopify/quilt.svg?branch=master)](https://travis-ci.org/Shopify/quilt)
4[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE.md) [![npm version](https://badge.fury.io/js/%40shopify%2Ffunction-enhancers.svg)](https://badge.fury.io/js/%40shopify%2Ffunction-enhancers.svg) [![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/@shopify/function-enhancers.svg)](https://img.shields.io/bundlephobia/minzip/@shopify/function-enhancers.svg)
5
6A set of helpers to enhance functions.
7
8## Installation
9
10```bash
11$ yarn add @shopify/function-enhancers
12```
13
14## Usage
15
16### `memoize`
17
18The memoize decorator creates a function that memoizes the results of the function it is decorating.
19The cache key for storing the results is based on the first argument provided to the memoized function.
20If the memoization key cannot be inferred from the first argument alone, a `resolver` should be passed in to ensure a unique key. (ex: the unique key is in the second argument, or the unique key is a combination of a few arguments)
21
22Know that memoization will be skipped on server process and the cached results have a maximum limit of 50 entries on a first in first out basis.
23
24#### Memoizing a simple function
25
26```ts
27import {memoize} from '@shopify/function-enhancers';
28
29const addOne = (number: number) => {
30 return number + 1;
31};
32
33const addOneMemoized = memoize(addOne);
34
35addOneMemoized(1); // -> 2, addOne is executed
36addOneMemoized(1); // -> 2, result is from cache
37```
38
39#### Memoizing a function with object as argument
40
41When memoizing a function with object as first argument, make sure the object is immutable.
42
43```ts
44import {memoize} from '@shopify/function-enhancers';
45
46const getValues = (someObject: {one: string; two: string}) => {
47 return;
48};
49
50const getValuesMemoized = memoize(getValues);
51
52const testObject1 = {one: 1, two: 2};
53getValuesMemoized(testObject1); // -> [1, 2], getValues is executed
54getValuesMemoized(testObject1); // -> [1, 2], result is from cache
55
56testObject1.two = 3;
57getValuesMemoized(testObject1); // -> [1, 2], result is from cache, BAD
58```
59
60#### Memoizing a function while providing a resolver
61
62The resolver takes in the same arguments as the function it is enhancing.
63Be sure that the resolver returns a unique identifer.
64
65```ts
66import {memoize} from '@shopify/function-enhancers';
67
68const getByCommand = (command: string, value: string) => {
69 // implementation for getByCommand
70};
71
72const getByCommandMemoized = memoize(
73 getByCommand,
74 (command: string, value: string) => `${command}${value}`,
75);
76
77getByCommandMemoized('command name 1', 'command value 1'); // runCommand is executed
78getByCommandMemoized('command name 1', 'command value 2'); // runCommand is executed
79```
80
81Next let's fix the example from [above](#memoizing-a-function-with-object-as-argument) so the results will always be correct.
82
83```ts
84import {memoize} from '@shopify/function-enhancers';
85
86const getByCommand = (command: string, value: string) => {
87 // implementation for getByCommand
88};
89
90const getByCommandMemoized = memoize(
91 getByCommand,
92 (command: string, value: string) => `${command}${value}`,
93);
94
95const testObject1 = {id: 1, value: 2};
96getByCommandMemoized(testObject1); // -> [1, 2], getValues is executed
97getByCommandMemoized(testObject1); // -> [1, 2], result is from cache
98
99testObject1.value = 3;
100getByCommandMemoized(testObject1); // -> [1, 3], getValues is executed
101```