@react-foundry/memoize 0.1.0
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.
- package/LICENSE +22 -0
- package/README.md +62 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/dist/index.mjs +15 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2019-2025 Crown Copyright
|
|
4
|
+
Copyright (C) 2019-2026 Daniel A.C. Martin
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
7
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
8
|
+
the Software without restriction, including without limitation the rights to
|
|
9
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
10
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
11
|
+
so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
React Foundry - Memoize
|
|
2
|
+
=======================
|
|
3
|
+
|
|
4
|
+
Higher-order functions for memoizing pure functions.
|
|
5
|
+
|
|
6
|
+
[Memoization] is a crude optimisation whereby the results of a function
|
|
7
|
+
are store in memory. It only works for functions that offer
|
|
8
|
+
[referential transparency], meaning that they return the same value for
|
|
9
|
+
the same input.
|
|
10
|
+
|
|
11
|
+
**Note:** Currently, only single-parameter functions are supported.
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
Using this package
|
|
15
|
+
------------------
|
|
16
|
+
|
|
17
|
+
First install the package into your project:
|
|
18
|
+
|
|
19
|
+
```shell
|
|
20
|
+
npm install -S @react-foundry/memoize
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Then use it in your code as follows:
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
import memoize from '@react-foundry/memoize';
|
|
27
|
+
|
|
28
|
+
const myPureFunction = str => str.length;
|
|
29
|
+
|
|
30
|
+
const memoizedFn = memoize(myPureFunction);
|
|
31
|
+
|
|
32
|
+
memoizedFn('foo'); // 3
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
Working on this package
|
|
37
|
+
-----------------------
|
|
38
|
+
|
|
39
|
+
Before working on this package you must install its dependencies using
|
|
40
|
+
the following command:
|
|
41
|
+
|
|
42
|
+
```shell
|
|
43
|
+
pnpm install
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
### Building
|
|
48
|
+
|
|
49
|
+
```shell
|
|
50
|
+
npm run build
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
### Clean-up
|
|
55
|
+
|
|
56
|
+
```shell
|
|
57
|
+
npm run clean
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
[Memoization]: https://en.wikipedia.org/wiki/Memoization
|
|
62
|
+
[referential transparency]: https://en.wikipedia.org/wiki/Referential_transparency
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.memoize = void 0;
|
|
4
|
+
const memoize = (f) => {
|
|
5
|
+
const memo = new Map();
|
|
6
|
+
return (a) => {
|
|
7
|
+
const cached = memo.get(a);
|
|
8
|
+
if (cached === undefined) {
|
|
9
|
+
const result = f(a);
|
|
10
|
+
memo.set(a, result);
|
|
11
|
+
return result;
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
return cached;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
exports.memoize = memoize;
|
|
19
|
+
exports.default = exports.memoize;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const memoize = (f) => {
|
|
2
|
+
const memo = new Map();
|
|
3
|
+
return (a) => {
|
|
4
|
+
const cached = memo.get(a);
|
|
5
|
+
if (cached === undefined) {
|
|
6
|
+
const result = f(a);
|
|
7
|
+
memo.set(a, result);
|
|
8
|
+
return result;
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
return cached;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export default memoize;
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@react-foundry/memoize",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Higher-order functions for memoizing pure functions.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.mjs",
|
|
10
|
+
"require": "./dist/index.js",
|
|
11
|
+
"default": "./dist/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"/dist"
|
|
16
|
+
],
|
|
17
|
+
"author": "Daniel A.C. Martin <npm@daniel-martin.co.uk> (http://daniel-martin.co.uk/)",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=12.0.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@jest/globals": "30.2.0",
|
|
24
|
+
"jest": "30.2.0",
|
|
25
|
+
"jest-environment-jsdom": "30.2.0",
|
|
26
|
+
"ts-jest": "29.4.6",
|
|
27
|
+
"typescript": "5.9.3"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
|
|
31
|
+
"build": "npm run build:esm && npm run build:cjs",
|
|
32
|
+
"build:esm": "tsc -m es2022 && find dist -name '*.js' -exec sh -c 'mv \"$0\" \"${0%.js}.mjs\"' {} \\;",
|
|
33
|
+
"build:cjs": "tsc",
|
|
34
|
+
"clean": "rm -rf dist tsconfig.tsbuildinfo"
|
|
35
|
+
},
|
|
36
|
+
"module": "dist/index.mjs",
|
|
37
|
+
"typings": "dist/index.d.ts"
|
|
38
|
+
}
|