This commit is contained in:
2025-05-12 05:38:44 +09:00
parent dced21c3f8
commit 6d78bfa46e
8120 changed files with 1161564 additions and 0 deletions

21
book/node_modules/memoize-one/LICENSE generated vendored Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 Alexander Reardon
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

268
book/node_modules/memoize-one/README.md generated vendored Normal file
View File

@ -0,0 +1,268 @@
# memoize-one
A memoization library that only caches the result of the most recent arguments.
> Also [async version](https://github.com/microlinkhq/async-memoize-one).
[![Build Status](https://travis-ci.org/alexreardon/memoize-one.svg?branch=master)](https://travis-ci.org/alexreardon/memoize-one)
[![npm](https://img.shields.io/npm/v/memoize-one.svg)](https://www.npmjs.com/package/memoize-one)
![types](https://img.shields.io/badge/types-typescript%20%7C%20flow-blueviolet)
[![dependencies](https://david-dm.org/alexreardon/memoize-one.svg)](https://david-dm.org/alexreardon/memoize-one)
[![minzip](https://img.shields.io/bundlephobia/minzip/memoize-one.svg)](https://www.npmjs.com/package/memoize-one)
[![Downloads per month](https://img.shields.io/npm/dm/memoize-one.svg)](https://www.npmjs.com/package/memoize-one)
## Rationale
Unlike other memoization libraries, `memoize-one` only remembers the latest arguments and result. No need to worry about cache busting mechanisms such as `maxAge`, `maxSize`, `exclusions` and so on, which can be prone to memory leaks. `memoize-one` simply remembers the last arguments, and if the function is next called with the same arguments then it returns the previous result.
## Usage
```js
// memoize-one uses the default import
import memoizeOne from 'memoize-one';
const add = (a, b) => a + b;
const memoizedAdd = memoizeOne(add);
memoizedAdd(1, 2); // 3
memoizedAdd(1, 2); // 3
// Add function is not executed: previous result is returned
memoizedAdd(2, 3); // 5
// Add function is called to get new value
memoizedAdd(2, 3); // 5
// Add function is not executed: previous result is returned
memoizedAdd(1, 2); // 3
// Add function is called to get new value.
// While this was previously cached,
// it is not the latest so the cached result is lost
```
## Installation
```bash
# yarn
yarn add memoize-one
# npm
npm install memoize-one --save
```
## Function argument equality
By default, we apply our own _fast_ and _naive_ equality function to determine whether the arguments provided to your function are equal. You can see the full code here: [are-inputs-equal.ts](https://github.com/alexreardon/memoize-one/blob/master/src/are-inputs-equal.ts).
(By default) function arguments are considered equal if:
1. there is same amount of arguments
2. each new argument has strict equality (`===`) with the previous argument
3. **[special case]** if two arguments are not `===` and they are both `NaN` then the two arguments are treated as equal
What this looks like in practice:
```js
import memoizeOne from 'memoize-one';
// add all numbers provided to the function
const add = (...args = []) =>
args.reduce((current, value) => {
return current + value;
}, 0);
const memoizedAdd = memoizeOne(add);
```
> 1. there is same amount of arguments
```js
memoizedAdd(1, 2);
// the amount of arguments has changed, so underlying add function is called
memoizedAdd(1, 2, 3);
```
> 2. new arguments have strict equality (`===`) with the previous argument
```js
memoizedAdd(1, 2);
// each argument is `===` to the last argument, so cache is used
memoizedAdd(1, 2);
// second argument has changed, so add function is called again
memoizedAdd(1, 3);
// the first value is not `===` to the previous first value (1 !== 3)
// so add function is called again
memoizedAdd(3, 1);
```
> 3. **[special case]** if the arguments are not `===` and they are both `NaN` then the argument is treated as equal
```js
memoizedAdd(NaN);
// Even though NaN !== NaN these arguments are treated as equal
memoizedAdd(NaN);
```
## Custom equality function
You can also pass in a custom function for checking the equality of two sets of arguments
```js
const memoized = memoizeOne(fn, isEqual);
```
The equality function needs to conform to this `type`:
```ts
type EqualityFn = (newArgs: any[], lastArgs: any[]) => boolean;
// You can import this type from memoize-one if you like
// typescript
import { EqualityFn } from 'memoize-one';
// flow
import type { EqualityFn } from 'memoize-one';
```
An equality function should return `true` if the arguments are equal. If `true` is returned then the wrapped function will not be called.
A custom equality function needs to compare `Arrays`. The `newArgs` array will be a new reference every time so a simple `newArgs === lastArgs` will always return `false`.
Equality functions are not called if the `this` context of the function has changed (see below).
Here is an example that uses a [dequal](https://github.com/lukeed/dequal) deep equal equality check
> `dequal` correctly handles deep comparing two arrays
```js
import memoizeOne from 'memoize-one';
import { dequal as isDeepEqual } from 'dequal';
const identity = (x) => x;
const shallowMemoized = memoizeOne(identity);
const deepMemoized = memoizeOne(identity, isDeepEqual);
const result1 = shallowMemoized({ foo: 'bar' });
const result2 = shallowMemoized({ foo: 'bar' });
result1 === result2; // false - different object reference
const result3 = deepMemoized({ foo: 'bar' });
const result4 = deepMemoized({ foo: 'bar' });
result3 === result4; // true - arguments are deep equal
```
## `this`
### `memoize-one` correctly respects `this` control
This library takes special care to maintain, and allow control over the the `this` context for **both** the original function being memoized as well as the returned memoized function. Both the original function and the memoized function's `this` context respect [all the `this` controlling techniques](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md):
- new bindings (`new`)
- explicit binding (`call`, `apply`, `bind`);
- implicit binding (call site: `obj.foo()`);
- default binding (`window` or `undefined` in `strict mode`);
- fat arrow binding (binding to lexical `this`)
- ignored this (pass `null` as `this` to explicit binding)
### Changes to `this` is considered an argument change
Changes to the running context (`this`) of a function can result in the function returning a different value even though its arguments have stayed the same:
```js
function getA() {
return this.a;
}
const temp1 = {
a: 20,
};
const temp2 = {
a: 30,
};
getA.call(temp1); // 20
getA.call(temp2); // 30
```
Therefore, in order to prevent against unexpected results, `memoize-one` takes into account the current execution context (`this`) of the memoized function. If `this` is different to the previous invocation then it is considered a change in argument. [further discussion](https://github.com/alexreardon/memoize-one/issues/3).
Generally this will be of no impact if you are not explicity controlling the `this` context of functions you want to memoize with [explicit binding](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md#explicit-binding) or [implicit binding](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md#implicit-binding). `memoize-One` will detect when you are manipulating `this` and will then consider the `this` context as an argument. If `this` changes, it will re-execute the original function even if the arguments have not changed.
## When your result function `throw`s
> There is no caching when your result function throws
If your result function `throw`s then the memoized function will also throw. The throw will not break the memoized functions existing argument cache. It means the memoized function will pretend like it was never called with arguments that made it `throw`.
```js
const canThrow = (name: string) => {
console.log('called');
if (name === 'throw') {
throw new Error(name);
}
return { name };
};
const memoized = memoizeOne(canThrow);
const value1 = memoized('Alex');
// console.log => 'called'
const value2 = memoized('Alex');
// result function not called
console.log(value1 === value2);
// console.log => true
try {
memoized('throw');
// console.log => 'called'
} catch (e) {
firstError = e;
}
try {
memoized('throw');
// console.log => 'called'
// the result function was called again even though it was called twice
// with the 'throw' string
} catch (e) {
secondError = e;
}
console.log(firstError !== secondError);
const value3 = memoized('Alex');
// result function not called as the original memoization cache has not been busted
console.log(value1 === value3);
// console.log => true
```
## Performance 🚀
### Tiny
`memoize-one` is super lightweight at [![min](https://img.shields.io/bundlephobia/min/memoize-one.svg?label=)](https://www.npmjs.com/package/memoize-one) minified and [![minzip](https://img.shields.io/bundlephobia/minzip/memoize-one.svg?label=)](https://www.npmjs.com/package/memoize-one) gzipped. (`1KB` = `1,024 Bytes`)
### Extremely fast
`memoize-one` performs better or on par with than other popular memoization libraries for the purpose of remembering the latest invocation.
**Results**
- [simple arguments](https://www.measurethat.net/Benchmarks/ShowResult/4452)
- [complex arguments](https://www.measurethat.net/Benchmarks/ShowResult/4488)
The comparisons are not exhaustive and are primarily to show that `memoize-one` accomplishes remembering the latest invocation really fast. The benchmarks do not take into account the differences in feature sets, library sizes, parse time, and so on.
## Code health 👍
- Tested with all built in [JavaScript types](https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/types%20%26%20grammar/ch1.md).
- 100% code coverage
- [Continuous integration](https://travis-ci.org/alexreardon/memoize-one) to run tests and type checks.
- Written in `Typescript`
- Correct typing for `Typescript` and `flow` type systems
- No dependencies

View File

@ -0,0 +1 @@
export default function areInputsEqual(newInputs: readonly unknown[], lastInputs: readonly unknown[]): boolean;

51
book/node_modules/memoize-one/dist/memoize-one.cjs.js generated vendored Normal file
View File

@ -0,0 +1,51 @@
'use strict';
var safeIsNaN = Number.isNaN ||
function ponyfill(value) {
return typeof value === 'number' && value !== value;
};
function isEqual(first, second) {
if (first === second) {
return true;
}
if (safeIsNaN(first) && safeIsNaN(second)) {
return true;
}
return false;
}
function areInputsEqual(newInputs, lastInputs) {
if (newInputs.length !== lastInputs.length) {
return false;
}
for (var i = 0; i < newInputs.length; i++) {
if (!isEqual(newInputs[i], lastInputs[i])) {
return false;
}
}
return true;
}
function memoizeOne(resultFn, isEqual) {
if (isEqual === void 0) { isEqual = areInputsEqual; }
var lastThis;
var lastArgs = [];
var lastResult;
var calledOnce = false;
function memoized() {
var newArgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
newArgs[_i] = arguments[_i];
}
if (calledOnce && lastThis === this && isEqual(newArgs, lastArgs)) {
return lastResult;
}
lastResult = resultFn.apply(this, newArgs);
calledOnce = true;
lastThis = this;
lastArgs = newArgs;
return lastResult;
}
return memoized;
}
module.exports = memoizeOne;

View File

@ -0,0 +1,8 @@
// @flow
export type EqualityFn = (newArgs: mixed[], lastArgs: mixed[]) => boolean;
// default export
declare export default function memoizeOne<ResultFn: (...any[]) => mixed>(
fn: ResultFn,
isEqual?: EqualityFn,
): ResultFn;

3
book/node_modules/memoize-one/dist/memoize-one.d.ts generated vendored Normal file
View File

@ -0,0 +1,3 @@
export declare type EqualityFn = (newArgs: any[], lastArgs: any[]) => boolean;
declare function memoizeOne<ResultFn extends (this: any, ...newArgs: any[]) => ReturnType<ResultFn>>(resultFn: ResultFn, isEqual?: EqualityFn): ResultFn;
export default memoizeOne;

49
book/node_modules/memoize-one/dist/memoize-one.esm.js generated vendored Normal file
View File

@ -0,0 +1,49 @@
var safeIsNaN = Number.isNaN ||
function ponyfill(value) {
return typeof value === 'number' && value !== value;
};
function isEqual(first, second) {
if (first === second) {
return true;
}
if (safeIsNaN(first) && safeIsNaN(second)) {
return true;
}
return false;
}
function areInputsEqual(newInputs, lastInputs) {
if (newInputs.length !== lastInputs.length) {
return false;
}
for (var i = 0; i < newInputs.length; i++) {
if (!isEqual(newInputs[i], lastInputs[i])) {
return false;
}
}
return true;
}
function memoizeOne(resultFn, isEqual) {
if (isEqual === void 0) { isEqual = areInputsEqual; }
var lastThis;
var lastArgs = [];
var lastResult;
var calledOnce = false;
function memoized() {
var newArgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
newArgs[_i] = arguments[_i];
}
if (calledOnce && lastThis === this && isEqual(newArgs, lastArgs)) {
return lastResult;
}
lastResult = resultFn.apply(this, newArgs);
calledOnce = true;
lastThis = this;
lastArgs = newArgs;
return lastResult;
}
return memoized;
}
export default memoizeOne;

57
book/node_modules/memoize-one/dist/memoize-one.js generated vendored Normal file
View File

@ -0,0 +1,57 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.memoizeOne = factory());
}(this, (function () { 'use strict';
var safeIsNaN = Number.isNaN ||
function ponyfill(value) {
return typeof value === 'number' && value !== value;
};
function isEqual(first, second) {
if (first === second) {
return true;
}
if (safeIsNaN(first) && safeIsNaN(second)) {
return true;
}
return false;
}
function areInputsEqual(newInputs, lastInputs) {
if (newInputs.length !== lastInputs.length) {
return false;
}
for (var i = 0; i < newInputs.length; i++) {
if (!isEqual(newInputs[i], lastInputs[i])) {
return false;
}
}
return true;
}
function memoizeOne(resultFn, isEqual) {
if (isEqual === void 0) { isEqual = areInputsEqual; }
var lastThis;
var lastArgs = [];
var lastResult;
var calledOnce = false;
function memoized() {
var newArgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
newArgs[_i] = arguments[_i];
}
if (calledOnce && lastThis === this && isEqual(newArgs, lastArgs)) {
return lastResult;
}
lastResult = resultFn.apply(this, newArgs);
calledOnce = true;
lastThis = this;
lastArgs = newArgs;
return lastResult;
}
return memoized;
}
return memoizeOne;
})));

View File

@ -0,0 +1 @@
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e="undefined"!=typeof globalThis?globalThis:e||self).memoizeOne=n()}(this,(function(){"use strict";var e=Number.isNaN||function(e){return"number"==typeof e&&e!=e};function n(n,t){if(n.length!==t.length)return!1;for(var r=0;r<n.length;r++)if(i=n[r],o=t[r],!(i===o||e(i)&&e(o)))return!1;var i,o;return!0}return function(e,t){var r;void 0===t&&(t=n);var i,o=[],f=!1;return function(){for(var n=[],u=0;u<arguments.length;u++)n[u]=arguments[u];return f&&r===this&&t(n,o)||(i=e.apply(this,n),f=!0,r=this,o=n),i}}}));

90
book/node_modules/memoize-one/package.json generated vendored Normal file
View File

@ -0,0 +1,90 @@
{
"name": "memoize-one",
"version": "5.2.1",
"description": "A memoization library which only remembers the latest invocation",
"main": "dist/memoize-one.cjs.js",
"types": "dist/memoize-one.d.ts",
"module": "dist/memoize-one.esm.js",
"sideEffects": false,
"author": "Alex Reardon <alexreardon@gmail.com>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/alexreardon/memoize-one.git"
},
"files": [
"/dist",
"/src"
],
"size-limit": [
{
"path": "dist/memoize-one.min.js",
"limit": "214B"
},
{
"path": "dist/memoize-one.js",
"limit": "216B"
},
{
"path": "dist/memoize-one.cjs.js",
"limit": "213B"
},
{
"path": "dist/memoize-one.esm.js",
"limit": "218B"
}
],
"keywords": [
"memoize",
"memoization",
"cache",
"performance"
],
"dependencies": {},
"devDependencies": {
"@size-limit/preset-small-lib": "^4.10.2",
"@types/jest": "^26.0.22",
"@types/lodash.isequal": "^4.5.5",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"benchmark": "^2.1.4",
"cross-env": "^7.0.3",
"eslint": "7.24.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-jest": "^24.3.5",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.3",
"lodash.isequal": "^4.5.0",
"prettier": "2.2.1",
"rimraf": "3.0.2",
"rollup": "^2.45.1",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript": "^1.0.1",
"size-limit": "^4.10.2",
"ts-jest": "^26.5.4",
"ts-node": "^9.1.1",
"tslib": "^2.2.0",
"typescript": "^4.2.4"
},
"config": {
"prettier_target": "src/**/*.{ts,js,jsx,md,json} test/**/*.{ts,js,jsx,md,json}"
},
"scripts": {
"validate": "yarn lint && yarn typecheck",
"test": "yarn jest",
"test:size": "yarn build && size-limit",
"typecheck": "yarn tsc --noEmit",
"prettier:check": "yarn prettier --debug-check $npm_package_config_prettier_target",
"prettier:write": "yarn prettier --write $npm_package_config_prettier_target",
"lint:eslint": "eslint $npm_package_config_prettier_target",
"lint": "yarn lint:eslint && yarn prettier:check",
"build": "yarn build:clean && yarn build:dist && yarn build:typescript && yarn build:flow",
"build:clean": "rimraf dist",
"build:dist": "rollup -c",
"build:typescript": "tsc ./src/memoize-one.ts --emitDeclarationOnly --declaration --outDir ./dist",
"build:flow": "cp src/memoize-one.js.flow dist/memoize-one.cjs.js.flow",
"perf": "ts-node ./benchmarks/shallow-equal.ts",
"prepublishOnly": "yarn build"
}
}

39
book/node_modules/memoize-one/src/are-inputs-equal.ts generated vendored Normal file
View File

@ -0,0 +1,39 @@
// Number.isNaN as it is not supported in IE11 so conditionally using ponyfill
// Using Number.isNaN where possible as it is ~10% faster
const safeIsNaN =
Number.isNaN ||
function ponyfill(value: unknown): boolean {
return typeof value === 'number' && value !== value;
};
function isEqual(first: unknown, second: unknown): boolean {
if (first === second) {
return true;
}
// Special case for NaN (NaN !== NaN)
if (safeIsNaN(first) && safeIsNaN(second)) {
return true;
}
return false;
}
export default function areInputsEqual(
newInputs: readonly unknown[],
lastInputs: readonly unknown[],
): boolean {
// no checks needed if the inputs length has changed
if (newInputs.length !== lastInputs.length) {
return false;
}
// Using for loop for speed. It generally performs better than array.every
// https://github.com/alexreardon/memoize-one/pull/59
for (let i = 0; i < newInputs.length; i++) {
if (!isEqual(newInputs[i], lastInputs[i])) {
return false;
}
}
return true;
}

View File

@ -0,0 +1,8 @@
// @flow
export type EqualityFn = (newArgs: mixed[], lastArgs: mixed[]) => boolean;
// default export
declare export default function memoizeOne<ResultFn: (...any[]) => mixed>(
fn: ResultFn,
isEqual?: EqualityFn,
): ResultFn;

40
book/node_modules/memoize-one/src/memoize-one.ts generated vendored Normal file
View File

@ -0,0 +1,40 @@
import areInputsEqual from './are-inputs-equal';
// Using ReadonlyArray<T> rather than readonly T as it works with TS v3
export type EqualityFn = (newArgs: any[], lastArgs: any[]) => boolean;
function memoizeOne<
// Need to use 'any' rather than 'unknown' here as it has
// The correct Generic narrowing behaviour.
ResultFn extends (this: any, ...newArgs: any[]) => ReturnType<ResultFn>
>(resultFn: ResultFn, isEqual: EqualityFn = areInputsEqual): ResultFn {
let lastThis: unknown;
let lastArgs: unknown[] = [];
let lastResult: ReturnType<ResultFn>;
let calledOnce: boolean = false;
// breaking cache when context (this) or arguments change
function memoized(this: unknown, ...newArgs: unknown[]): ReturnType<ResultFn> {
if (calledOnce && lastThis === this && isEqual(newArgs, lastArgs)) {
return lastResult;
}
// Throwing during an assignment aborts the assignment: https://codepen.io/alexreardon/pen/RYKoaz
// Doing the lastResult assignment first so that if it throws
// nothing will be overwritten
lastResult = resultFn.apply(this, newArgs);
calledOnce = true;
lastThis = this;
lastArgs = newArgs;
return lastResult;
}
return memoized as ResultFn;
}
// default export
export default memoizeOne;
// disabled for now as mixing named and
// default exports is problematic with CommonJS
// export { memoizeOne };