@chriscdn/memoize 1.0.5 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +30 -18
  3. package/package.json +1 -1
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2024 Christopher Meyer
3
+ Copyright (c) 2024-2025 Christopher Meyer
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -18,21 +18,22 @@ yarn add @chriscdn/memoize
18
18
 
19
19
  ## Usage
20
20
 
21
- The package comes with two functions: `Memoize` and `MemoizeAsync`.
21
+ The package provides two functions: `Memoize` and `MemoizeAsync`.
22
22
 
23
23
  ```ts
24
24
  import { Memoize, MemoizeAsync } from "@chriscdn/memoize";
25
25
  ```
26
26
 
27
- The `Memoize` function can be used to memoize a _synchronous_ function. The `MemoizeAsync` function can be used to memoize an _asynchronous_ function. The cache is based [quick-lru](https://www.npmjs.com/package/quick-lru).
27
+ - **`Memoize`** is used to memoize a _synchronous_ function.
28
+ - **`MemoizeAsync`** is used to memoize an _asynchronous_ function.
28
29
 
29
- Each call to `Memoize` or `MemoizeAsync` will create a new cache instance.
30
+ The cache is powered by [quick-lru](https://www.npmjs.com/package/quick-lru). Each call to `Memoize` or `MemoizeAsync` creates a new cache instance.
30
31
 
31
- The `MemoizeAsync` function prevents duplicate evaluations by ensuring multiple calls with an identical cache key is only processed once.
32
+ The `MemoizeAsync` function prevents duplicate evaluations by ensuring that multiple calls with the same cache key are processed only once.
32
33
 
33
- By default, the cache key is generated by calling `JSON.stringify()` on the function arguments. This can be customized (see below).
34
+ By default, the cache key is generated by calling `JSON.stringify()` on the function arguments. This behavior can be customized (see below).
34
35
 
35
- **Example (synchronous)**
36
+ ### Example (Synchronous)
36
37
 
37
38
  To memoize a function:
38
39
 
@@ -49,9 +50,15 @@ const result = add(5, 7);
49
50
  // 12
50
51
  ```
51
52
 
52
- **Example (asynchronous)**
53
+ You can also define the function in a single line:
53
54
 
54
- The asynchronous case is similar:
55
+ ```ts
56
+ const add = Memoize((x: number, y: number) => x + y);
57
+ ```
58
+
59
+ ### Example (Asynchronous)
60
+
61
+ Memoizing an asynchronous function is similar:
55
62
 
56
63
  ```ts
57
64
  const _add = async (x: number, y: number) => x + y;
@@ -64,44 +71,47 @@ const result = await add(5, 7);
64
71
 
65
72
  ## Options
66
73
 
67
- The `Memoize` and `MemoizeAsync` functions accept an `Options` parameter to control the behaviour of the cache.
74
+ Both `Memoize` and `MemoizeAsync` accept an `options` parameter to control the cache behavior:
68
75
 
69
76
  ```ts
70
77
  const add = MemoizeAsync(_add, options);
71
78
  ```
72
79
 
73
- The available options are as follows, with their defaults:
80
+ Available options (with defaults):
74
81
 
75
82
  ```ts
76
83
  const options = {
77
- // maximum number of items in the cache
84
+ // Maximum number of items in the cache
78
85
  maxSize: 1000,
79
86
 
80
- // maximum number of milliseconds an item is to remain in the cache, undefined implies forever
87
+ // Maximum lifespan of an item in milliseconds; undefined means items never expire
81
88
  maxAge: undefined,
82
89
 
83
- // a synchronous function for generating a cache key (must return a String)
90
+ // A synchronous function to generate cache keys (must return a string)
84
91
  resolver: (...args) => JSON.stringify(args),
85
92
  };
86
93
  ```
87
94
 
88
95
  ## Cache
89
96
 
90
- The underlying [quick-lru](https://www.npmjs.com/package/quick-lru) instance can be accessed with the `.cache` property on the memoized function.
97
+ The underlying [quick-lru](https://www.npmjs.com/package/quick-lru) instance is accessible via the `.cache` property on the memoized function:
91
98
 
92
99
  ```ts
93
100
  const add = MemoizeAsync(_add);
94
101
  const result = await add(5, 7);
95
102
 
96
- console.log(addCachedAsync.cache.size === 1);
103
+ console.log(add.cache.size === 1);
97
104
  // true
105
+
106
+ // Clear the cache
107
+ add.cache.clear();
98
108
  ```
99
109
 
100
110
  ## Class Methods
101
111
 
102
- Class methods can also be memoized, though this requires overriding the method within the constructor. When doing so, it's important to bind the method to the instance to ensure it maintains the correct context.
112
+ Class methods can also be memoized, but this requires overriding the method within the constructor. Ensure you bind the method to the instance to maintain the correct context.
103
113
 
104
- Consider the following example, where the `add` method is memoized by reassigning it within the constructor after binding it to the current instance. This ensures the memoized version retains access to instance properties like `count` and keeps TypeScript happy by preserving the method's type signature.
114
+ Here's an example where the `add` method is memoized by reassigning it within the constructor. This approach preserves access to instance properties (like `count`) and maintains the correct method signature in TypeScript:
105
115
 
106
116
  ```ts
107
117
  class AddClass {
@@ -118,10 +128,12 @@ class AddClass {
118
128
  }
119
129
  ```
120
130
 
121
- Keep in mind that each instance of a class will maintain its own separate cache.
131
+ Each memoized method in each class instance maintains its own cache.
122
132
 
123
133
  ## Tests
124
134
 
135
+ Run the tests using:
136
+
125
137
  ```bash
126
138
  yarn test
127
139
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chriscdn/memoize",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "Memoize a synchronous or asynchronous function.",
5
5
  "repository": "https://github.com/chriscdn/memoize",
6
6
  "author": "Christopher Meyer <chris@schwiiz.org>",