limited-cache 1.1.1 → 2.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/CHANGELOG.md +19 -5
- package/CODE_OF_CONDUCT.md +103 -47
- package/{CONTRIBUTING.md → CONTRIBUTING.template.md} +4 -2
- package/LICENSE +1 -1
- package/README.md +26 -18
- package/dist/core/LimitedCache.d.ts +4 -3
- package/dist/core/LimitedCache.d.ts.map +1 -0
- package/dist/core/LimitedCache.js +29 -0
- package/dist/core/LimitedCache.js.map +1 -0
- package/dist/core/LimitedCacheObject.d.ts +5 -3
- package/dist/core/LimitedCacheObject.d.ts.map +1 -0
- package/dist/core/LimitedCacheObject.js +52 -0
- package/dist/core/LimitedCacheObject.js.map +1 -0
- package/dist/core/builtIns.d.ts +12 -11
- package/dist/core/builtIns.d.ts.map +1 -0
- package/dist/core/builtIns.js +5 -0
- package/dist/core/builtIns.js.map +1 -0
- package/dist/core/defaultOptions.d.ts +6 -6
- package/dist/core/defaultOptions.d.ts.map +1 -0
- package/dist/core/defaultOptions.js +14 -0
- package/dist/core/defaultOptions.js.map +1 -0
- package/dist/core/limitedCacheUtil.d.ts +13 -12
- package/dist/core/limitedCacheUtil.d.ts.map +1 -0
- package/dist/core/limitedCacheUtil.js +14 -0
- package/dist/core/limitedCacheUtil.js.map +1 -0
- package/dist/core/lowLevelFunctions.d.ts +14 -13
- package/dist/core/lowLevelFunctions.d.ts.map +1 -0
- package/dist/core/lowLevelFunctions.js +240 -0
- package/dist/core/lowLevelFunctions.js.map +1 -0
- package/dist/index.cjs +388 -0
- package/dist/index.d.ts +7 -10
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -8
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +58 -60
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +86 -73
- package/src/__tests__/LimitedCache.test.ts +186 -0
- package/src/__tests__/LimitedCacheObject.test.ts +150 -0
- package/src/__tests__/lowLevelFunctions.test.ts +430 -0
- package/src/__tests__/scenarios/keys.test.ts +67 -0
- package/src/__tests__/scenarios/maxCacheSize.test.ts +126 -0
- package/src/__tests__/scenarios/maxCacheTime.test.ts +150 -0
- package/src/__tests__/scenarios/values.test.ts +49 -0
- package/src/__tests__/typeChecks/LimitedCacheObjectTypes.test.ts +70 -0
- package/src/__tests__/typeChecks/LimitedCacheTypes.test.ts +70 -0
- package/src/__tests__/typeChecks/lowLevelFunctionsTypes.test.ts +70 -0
- package/src/core/LimitedCache.ts +1 -1
- package/src/core/LimitedCacheObject.ts +15 -3
- package/src/core/defaultOptions.ts +1 -2
- package/src/core/limitedCacheUtil.ts +1 -1
- package/src/core/lowLevelFunctions.ts +6 -6
- package/src/index.ts +0 -8
- package/dist/limited-cache.cjs.development.js +0 -456
- package/dist/limited-cache.cjs.development.js.map +0 -1
- package/dist/limited-cache.cjs.production.min.js +0 -2
- package/dist/limited-cache.cjs.production.min.js.map +0 -1
- package/dist/limited-cache.esm.js +0 -436
- package/dist/limited-cache.esm.js.map +0 -1
- package/legacy-types/ts3.x/dist/core/LimitedCache.d.ts +0 -3
- package/legacy-types/ts3.x/dist/core/LimitedCacheObject.d.ts +0 -3
- package/legacy-types/ts3.x/dist/core/builtIns.d.ts +0 -11
- package/legacy-types/ts3.x/dist/core/defaultOptions.d.ts +0 -6
- package/legacy-types/ts3.x/dist/core/limitedCacheUtil.d.ts +0 -12
- package/legacy-types/ts3.x/dist/core/lowLevelFunctions.d.ts +0 -13
- package/legacy-types/ts3.x/dist/index.d.ts +0 -10
- package/legacy-types/ts3.x/dist/types.d.ts +0 -60
package/dist/types.d.ts
CHANGED
|
@@ -1,60 +1,58 @@
|
|
|
1
|
-
export
|
|
2
|
-
export interface LimitedCacheOptionsFull {
|
|
3
|
-
/** Items will be removed to keep the cache within the maxCacheSize limit */
|
|
4
|
-
maxCacheSize: number;
|
|
5
|
-
/** Items will be removed and never returned if they were set more than maxCacheTime milliseconds ago */
|
|
6
|
-
maxCacheTime: number;
|
|
7
|
-
/** (dev only) A warning will be emitted if an item rotates out of the cache before this many milliseconds have passed, to indicate the size is too small */
|
|
8
|
-
warnIfItemPurgedBeforeTime: number;
|
|
9
|
-
/** (private) Internal cleanup of old keys will be performed after this many operations */
|
|
10
|
-
opLimit: number;
|
|
11
|
-
/** (private) Internal optimization to adjust how much searching will be done to find expired items, to avoid being O(n) */
|
|
12
|
-
scanLimit: number;
|
|
13
|
-
}
|
|
14
|
-
export
|
|
15
|
-
export
|
|
16
|
-
export interface LimitedCacheInstance<ItemType = DefaultItemType> {
|
|
17
|
-
/** Return the requested item, if it has not expired */
|
|
18
|
-
get: (cacheKey: string) => ItemType | undefined;
|
|
19
|
-
/** Return all non-expired items */
|
|
20
|
-
getAll: () => Record<string, ItemType>;
|
|
21
|
-
/** Indicate whether or not the requested item is present and has not expired */
|
|
22
|
-
has: (cacheKey: string) => boolean;
|
|
23
|
-
/** Add the item to the cache, or update its timestamp if it already exists */
|
|
24
|
-
set: (cacheKey: string, item: ItemType) => ItemType;
|
|
25
|
-
/** Remove the requested item from the cache, if necessary */
|
|
26
|
-
remove: (cacheKey: string) => true;
|
|
27
|
-
/** Remove all items and all timestamps from the cache */
|
|
28
|
-
reset: () => LimitedCacheMeta<ItemType>;
|
|
29
|
-
/** Return a serializable representation of the cache internals, suitable for long-term storage */
|
|
30
|
-
getCacheMeta: () => LimitedCacheMeta<ItemType>;
|
|
31
|
-
/** Return the cache's current values for all options */
|
|
32
|
-
getOptions: () => LimitedCacheOptionsFull;
|
|
33
|
-
/** Update one or more of the cache's options */
|
|
34
|
-
setOptions: (newOptions: LimitedCacheOptions) => LimitedCacheOptionsReadonly;
|
|
35
|
-
/** Reduces cache size by cleaning up old keys and expired items */
|
|
36
|
-
doMaintenance: () => LimitedCacheMeta<ItemType>;
|
|
37
|
-
}
|
|
38
|
-
export interface LimitedCacheObjectInstance<ItemType = DefaultItemType> {
|
|
39
|
-
[key: string]: ItemType;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* A serializable representation of the cache internals, suitable for long-term storage
|
|
43
|
-
*/
|
|
44
|
-
export interface LimitedCacheMeta<ItemType = DefaultItemType> {
|
|
45
|
-
/** Schema version: old versions will be upgraded if possible, or a warning will be emitted if not */
|
|
46
|
-
limitedCacheMetaVersion: number;
|
|
47
|
-
/** Options to control cache size, time, and behavior */
|
|
48
|
-
options: LimitedCacheOptionsReadonly;
|
|
49
|
-
/** The values in the cache, stored by key. Will include old keys not yet garbage collected */
|
|
50
|
-
cache: Record<string, ItemType | undefined>;
|
|
51
|
-
/** List of keys that have been set, in chronological order. Used to find cache items most likely to be expired */
|
|
52
|
-
keyList: Array<string>;
|
|
53
|
-
/** The [setTime, expirationTime] for each key that has been set. Removed on unset. */
|
|
54
|
-
keyInfo: Record<string, [
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
opsLeft: number;
|
|
60
|
-
}
|
|
1
|
+
export type DefaultItemType = any;
|
|
2
|
+
export interface LimitedCacheOptionsFull {
|
|
3
|
+
/** Items will be removed to keep the cache within the maxCacheSize limit */
|
|
4
|
+
maxCacheSize: number;
|
|
5
|
+
/** Items will be removed and never returned if they were set more than maxCacheTime milliseconds ago */
|
|
6
|
+
maxCacheTime: number;
|
|
7
|
+
/** (dev only) A warning will be emitted if an item rotates out of the cache before this many milliseconds have passed, to indicate the size is too small */
|
|
8
|
+
warnIfItemPurgedBeforeTime: number;
|
|
9
|
+
/** (private) Internal cleanup of old keys will be performed after this many operations */
|
|
10
|
+
opLimit: number;
|
|
11
|
+
/** (private) Internal optimization to adjust how much searching will be done to find expired items, to avoid being O(n) */
|
|
12
|
+
scanLimit: number;
|
|
13
|
+
}
|
|
14
|
+
export type LimitedCacheOptions = Partial<LimitedCacheOptionsFull> | null;
|
|
15
|
+
export type LimitedCacheOptionsReadonly = Readonly<LimitedCacheOptionsFull>;
|
|
16
|
+
export interface LimitedCacheInstance<ItemType = DefaultItemType> {
|
|
17
|
+
/** Return the requested item, if it has not expired */
|
|
18
|
+
get: (cacheKey: string) => ItemType | undefined;
|
|
19
|
+
/** Return all non-expired items */
|
|
20
|
+
getAll: () => Record<string, ItemType>;
|
|
21
|
+
/** Indicate whether or not the requested item is present and has not expired */
|
|
22
|
+
has: (cacheKey: string) => boolean;
|
|
23
|
+
/** Add the item to the cache, or update its timestamp if it already exists */
|
|
24
|
+
set: (cacheKey: string, item: ItemType) => ItemType;
|
|
25
|
+
/** Remove the requested item from the cache, if necessary */
|
|
26
|
+
remove: (cacheKey: string) => true;
|
|
27
|
+
/** Remove all items and all timestamps from the cache */
|
|
28
|
+
reset: () => LimitedCacheMeta<ItemType>;
|
|
29
|
+
/** Return a serializable representation of the cache internals, suitable for long-term storage */
|
|
30
|
+
getCacheMeta: () => LimitedCacheMeta<ItemType>;
|
|
31
|
+
/** Return the cache's current values for all options */
|
|
32
|
+
getOptions: () => LimitedCacheOptionsFull;
|
|
33
|
+
/** Update one or more of the cache's options */
|
|
34
|
+
setOptions: (newOptions: LimitedCacheOptions) => LimitedCacheOptionsReadonly;
|
|
35
|
+
/** Reduces cache size by cleaning up old keys and expired items */
|
|
36
|
+
doMaintenance: () => LimitedCacheMeta<ItemType>;
|
|
37
|
+
}
|
|
38
|
+
export interface LimitedCacheObjectInstance<ItemType = DefaultItemType> {
|
|
39
|
+
[key: string]: ItemType;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* A serializable representation of the cache internals, suitable for long-term storage
|
|
43
|
+
*/
|
|
44
|
+
export interface LimitedCacheMeta<ItemType = DefaultItemType> {
|
|
45
|
+
/** Schema version: old versions will be upgraded if possible, or a warning will be emitted if not */
|
|
46
|
+
limitedCacheMetaVersion: number;
|
|
47
|
+
/** Options to control cache size, time, and behavior */
|
|
48
|
+
options: LimitedCacheOptionsReadonly;
|
|
49
|
+
/** The values in the cache, stored by key. Will include old keys not yet garbage collected */
|
|
50
|
+
cache: Record<string, ItemType | undefined>;
|
|
51
|
+
/** List of keys that have been set, in chronological order. Used to find cache items most likely to be expired */
|
|
52
|
+
keyList: Array<string>;
|
|
53
|
+
/** The [setTime, expirationTime] for each key that has been set. Removed on unset. */
|
|
54
|
+
keyInfo: Record<string, [number, number] | undefined>;
|
|
55
|
+
/** Number of operations remaining until internal cleanup of old keys is performed. Based on options.opLimit */
|
|
56
|
+
opsLeft: number;
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,eAAe,GAAG,GAAG,CAAC;AAElC,MAAM,WAAW,uBAAuB;IACtC,4EAA4E;IAC5E,YAAY,EAAE,MAAM,CAAC;IACrB,wGAAwG;IACxG,YAAY,EAAE,MAAM,CAAC;IAErB,4JAA4J;IAC5J,0BAA0B,EAAE,MAAM,CAAC;IACnC,0FAA0F;IAC1F,OAAO,EAAE,MAAM,CAAC;IAChB,2HAA2H;IAC3H,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,MAAM,mBAAmB,GAAG,OAAO,CAAC,uBAAuB,CAAC,GAAG,IAAI,CAAC;AAC1E,MAAM,MAAM,2BAA2B,GAAG,QAAQ,CAAC,uBAAuB,CAAC,CAAC;AAE5E,MAAM,WAAW,oBAAoB,CAAC,QAAQ,GAAG,eAAe;IAC9D,uDAAuD;IACvD,GAAG,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,QAAQ,GAAG,SAAS,CAAC;IAChD,mCAAmC;IACnC,MAAM,EAAE,MAAM,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACvC,gFAAgF;IAChF,GAAG,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC;IACnC,8EAA8E;IAC9E,GAAG,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,KAAK,QAAQ,CAAC;IACpD,6DAA6D;IAC7D,MAAM,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,yDAAyD;IACzD,KAAK,EAAE,MAAM,gBAAgB,CAAC,QAAQ,CAAC,CAAC;IACxC,kGAAkG;IAClG,YAAY,EAAE,MAAM,gBAAgB,CAAC,QAAQ,CAAC,CAAC;IAC/C,wDAAwD;IACxD,UAAU,EAAE,MAAM,uBAAuB,CAAC;IAC1C,gDAAgD;IAChD,UAAU,EAAE,CAAC,UAAU,EAAE,mBAAmB,KAAK,2BAA2B,CAAC;IAC7E,mEAAmE;IACnE,aAAa,EAAE,MAAM,gBAAgB,CAAC,QAAQ,CAAC,CAAC;CACjD;AAED,MAAM,WAAW,0BAA0B,CAAC,QAAQ,GAAG,eAAe;IACpE,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB,CAAC,QAAQ,GAAG,eAAe;IAC1D,qGAAqG;IACrG,uBAAuB,EAAE,MAAM,CAAC;IAChC,wDAAwD;IACxD,OAAO,EAAE,2BAA2B,CAAC;IACrC,8FAA8F;IAC9F,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,GAAG,SAAS,CAAC,CAAC;IAC5C,kHAAkH;IAClH,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACvB,sFAAsF;IACtF,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC,CAAC;IACtD,+GAA+G;IAC/G,OAAO,EAAE,MAAM,CAAC;CACjB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "limited-cache",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "A minimal JS cache: like using an object, except it won't grow forever",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"limited cache",
|
|
@@ -14,99 +14,112 @@
|
|
|
14
14
|
"autoexpire"
|
|
15
15
|
],
|
|
16
16
|
"license": "MIT",
|
|
17
|
-
"homepage": "https://github.com/spautz/limited-cache#readme",
|
|
17
|
+
"homepage": "https://github.com/spautz/limited-cache/packages/limited-cache#readme",
|
|
18
18
|
"bugs": "https://github.com/spautz/limited-cache/issues",
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
|
-
"url": "https://github.com/spautz/limited-cache.git"
|
|
21
|
+
"url": "git+https://github.com/spautz/limited-cache.git",
|
|
22
|
+
"directory": "packages/limited-cache"
|
|
22
23
|
},
|
|
23
24
|
"author": {
|
|
24
25
|
"name": "Steven Pautz",
|
|
25
|
-
"url": "
|
|
26
|
+
"url": "https://github.com/spautz/"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public",
|
|
30
|
+
"provenance": true,
|
|
31
|
+
"tag": "next"
|
|
26
32
|
},
|
|
27
33
|
"files": [
|
|
28
34
|
"dist/",
|
|
35
|
+
"docs/",
|
|
29
36
|
"legacy-types/",
|
|
30
37
|
"src/",
|
|
31
38
|
"LICENSE",
|
|
32
39
|
"*.md"
|
|
33
40
|
],
|
|
34
|
-
"source": "src/index.ts",
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"
|
|
43
|
-
},
|
|
44
|
-
"scripts": {
|
|
45
|
-
"____ LIFECYCLE HOOKS _______________________________________________": "",
|
|
46
|
-
"prepare": "yon run build && husky install",
|
|
47
|
-
"prerelease": "yon run build:clean",
|
|
48
|
-
"prepublishOnly": "yarn run dev:readonly && yarn run build",
|
|
49
|
-
"____ INTEGRATION ___________________________________________________": "",
|
|
50
|
-
"clean": "yon run build:clean && yon run test:clean && rimraf ./node_modules/.cache/",
|
|
51
|
-
"dev": "yon run format && yon run types && yon run lint",
|
|
52
|
-
"dev:readonly": "yon run format:verify && yon run types && yon run lint",
|
|
53
|
-
"all": "yon run clean && yon run dev && yon run test:coverage && yon run build",
|
|
54
|
-
"all:readonly": "yon run dev:readonly && yon run test:coverage",
|
|
55
|
-
"____ INDIVIDUAL COMMANDS ___________________________________________": "",
|
|
56
|
-
"build": "yon run build:clean && yon run build:main && yon run build:verify && yon run build:types",
|
|
57
|
-
"build:clean": "rimraf ./dist ./legacy-types",
|
|
58
|
-
"build:main": "tsdx build",
|
|
59
|
-
"build:types": "yon run build:types:3.x && yon run build:types:4.x",
|
|
60
|
-
"build:types:3.x": "downlevel-dts ./dist ./legacy-types/ts3.x/dist --to=3.0",
|
|
61
|
-
"build:types:4.x": "downlevel-dts ./dist ./dist --to=4.0",
|
|
62
|
-
"build:verify": "node ./scripts/verify-build.js",
|
|
63
|
-
"build:watch": "tsdx watch",
|
|
64
|
-
"format": "prettier --write \"**/*.*\"",
|
|
65
|
-
"format:verify": "prettier --list-different \"**/*.*\"",
|
|
66
|
-
"lint": "tsdx lint . --max-warnings 0",
|
|
67
|
-
"lint-staged": "lint-staged",
|
|
68
|
-
"release:changelog": "standard-version --skip.commit --skip.tag --release-as ",
|
|
69
|
-
"release:tag": "standard-version --commit-all --sign --skip.changelog --release-as ",
|
|
70
|
-
"test": "yon run test:coverage",
|
|
71
|
-
"test:clean": "rimraf ./coverage",
|
|
72
|
-
"test:coverage": "yon run test:clean && tsdx test --coverage",
|
|
73
|
-
"test:nowatch": "yon run test:clean && tsdx test",
|
|
74
|
-
"test:watch": "yon run test:clean && tsdx test --watch",
|
|
75
|
-
"test:watchcoverage": "yon run test:clean && tsdx test --watchAll --coverage",
|
|
76
|
-
"types": "tsc --noEmit --p tsconfig.json"
|
|
41
|
+
"source": "./src/index.ts",
|
|
42
|
+
"type": "module",
|
|
43
|
+
"exports": {
|
|
44
|
+
".": {
|
|
45
|
+
"import": "./dist/index.js",
|
|
46
|
+
"require": "./dist/index.cjs",
|
|
47
|
+
"types": "./dist/index.d.ts"
|
|
48
|
+
},
|
|
49
|
+
"./package.json": "./package.json"
|
|
77
50
|
},
|
|
51
|
+
"main": "./dist/index.cjs",
|
|
52
|
+
"module": "./dist/index.js",
|
|
53
|
+
"jsnext:main": "./dist/index.js",
|
|
54
|
+
"types": "./dist/index.d.ts",
|
|
55
|
+
"sideEffects": false,
|
|
78
56
|
"dependencies": {},
|
|
79
57
|
"devDependencies": {
|
|
80
|
-
"@types/
|
|
81
|
-
"@types/node": "15.12.4",
|
|
82
|
-
"downlevel-dts": "0.7.0",
|
|
83
|
-
"husky": "6.0.0",
|
|
84
|
-
"lint-staged": "10.5.4",
|
|
85
|
-
"rimraf": "3.0.2",
|
|
86
|
-
"standard-version": "9.3.0",
|
|
87
|
-
"tsdx": "0.14.1",
|
|
88
|
-
"typescript": "4.2.4",
|
|
89
|
-
"yarn-or-npm": "3.0.1"
|
|
90
|
-
},
|
|
91
|
-
"resolutions": {
|
|
92
|
-
"**/@typescript-eslint/eslint-plugin": "^4.28.0",
|
|
93
|
-
"**/@typescript-eslint/parser": "^4.28.0"
|
|
58
|
+
"@types/node": "18.16.3"
|
|
94
59
|
},
|
|
95
|
-
"
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
60
|
+
"peerDependencies": {},
|
|
61
|
+
"size-limit": [
|
|
62
|
+
{
|
|
63
|
+
"path": "dist/index.js",
|
|
64
|
+
"import": "{}",
|
|
65
|
+
"limit": "20 B"
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
"path": "dist/index.js",
|
|
69
|
+
"limit": "2 kB"
|
|
103
70
|
}
|
|
104
|
-
|
|
71
|
+
],
|
|
105
72
|
"typesVersions": {
|
|
106
|
-
"<4": {
|
|
73
|
+
"<4.0": {
|
|
107
74
|
"*": [
|
|
108
|
-
"legacy-types/ts3.
|
|
75
|
+
"legacy-types/ts3.5/index.d.ts"
|
|
76
|
+
]
|
|
77
|
+
},
|
|
78
|
+
"<4.5": {
|
|
79
|
+
"*": [
|
|
80
|
+
"legacy-types/ts4.0/index.d.ts"
|
|
81
|
+
]
|
|
82
|
+
},
|
|
83
|
+
"<4.7": {
|
|
84
|
+
"*": [
|
|
85
|
+
"legacy-types/ts4.5/index.d.ts"
|
|
86
|
+
]
|
|
87
|
+
},
|
|
88
|
+
"*": {
|
|
89
|
+
"*": [
|
|
90
|
+
"dist/index.d.ts"
|
|
109
91
|
]
|
|
110
92
|
}
|
|
93
|
+
},
|
|
94
|
+
"scripts": {
|
|
95
|
+
"____ HOOKS _________________________________________________________": "",
|
|
96
|
+
"____ INTEGRATION ___________________________________________________": "",
|
|
97
|
+
"clean": "pnpm run build:clean && pnpm run test:clean && rimraf --glob ./node_modules/.cache *.log",
|
|
98
|
+
"all": "pnpm run format && pnpm run typecheck && pnpm run lint:fix && pnpm run test:coverage && pnpm run build",
|
|
99
|
+
"all:readonly": "pnpm run format:verify && pnpm run typecheck && pnpm run lint && pnpm run test:quick",
|
|
100
|
+
"all:quick": "pnpm run format && pnpm run typecheck && pnpm run lint:fix",
|
|
101
|
+
"all:ci": "pnpm run format:verify && pnpm run typecheck && pnpm run lint && pnpm run test:ci && pnpm run build",
|
|
102
|
+
"____ INDIVIDUAL COMMANDS ___________________________________________": "",
|
|
103
|
+
"build": "pnpm run build:main && pnpm run sizecheck && pnpm run build:legacytypes",
|
|
104
|
+
"build:clean": "rimraf ./dist ./legacy-types",
|
|
105
|
+
"build:main": "pnpm run build:clean && tsup src/index.ts --format cjs && tsc -p ./tsconfig.build.json",
|
|
106
|
+
"build:legacytypes": "pnpm run build:legacytypes:3.5 && pnpm run build:legacytypes:4.0 && pnpm run build:legacytypes:4.5",
|
|
107
|
+
"build:legacytypes:3.5": "downlevel-dts ./dist ./legacy-types/ts3.5 --to=3.5",
|
|
108
|
+
"build:legacytypes:4.0": "downlevel-dts ./dist ./legacy-types/ts4.0 --to=4.0",
|
|
109
|
+
"build:legacytypes:4.5": "downlevel-dts ./dist ./legacy-types/ts4.5 --to=4.5",
|
|
110
|
+
"build:watch": "pnpm run build:clean && tsup src/index.ts --format esm,cjs --dts --watch",
|
|
111
|
+
"format": "prettier --write .",
|
|
112
|
+
"format:verify": "prettier --list-different .",
|
|
113
|
+
"lint": "eslint . --max-warnings 0",
|
|
114
|
+
"lint:fix": "eslint . --max-warnings 0 --fix",
|
|
115
|
+
"sizecheck": "size-limit",
|
|
116
|
+
"test": "pnpm run test:coverage",
|
|
117
|
+
"test:clean": "rimraf ./coverage",
|
|
118
|
+
"test:ci": "pnpm run test:clean && vitest run --coverage",
|
|
119
|
+
"test:coverage": "pnpm run test:clean && vitest run --coverage",
|
|
120
|
+
"test:quick": "pnpm run test:clean && vitest run --coverage=false",
|
|
121
|
+
"test:watch": "pnpm run test:clean && vitest watch --coverage=false",
|
|
122
|
+
"test:watchcoverage": "pnpm run test:clean && vitest watch --coverage",
|
|
123
|
+
"typecheck": "tsc -p ./tsconfig.json --noEmit"
|
|
111
124
|
}
|
|
112
|
-
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { describe, beforeEach, expect, it } from 'vitest';
|
|
2
|
+
import { defaultOptions } from '../core/defaultOptions';
|
|
3
|
+
import { LimitedCache } from '../core/LimitedCache';
|
|
4
|
+
import { LimitedCacheInstance } from '../types';
|
|
5
|
+
|
|
6
|
+
describe('LimitedCache', () => {
|
|
7
|
+
it('initializes without options', () => {
|
|
8
|
+
const myCache: LimitedCacheInstance = LimitedCache();
|
|
9
|
+
|
|
10
|
+
expect(myCache).toBeTruthy();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('initializes with options', () => {
|
|
14
|
+
const myCache: LimitedCacheInstance = LimitedCache({
|
|
15
|
+
maxCacheSize: 123,
|
|
16
|
+
maxCacheTime: 456,
|
|
17
|
+
warnIfItemPurgedBeforeTime: 999,
|
|
18
|
+
opLimit: 10,
|
|
19
|
+
scanLimit: 100,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
expect(myCache).toBeTruthy();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('works as a standard cache', () => {
|
|
26
|
+
let myCache: LimitedCacheInstance<number | undefined>;
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
myCache = LimitedCache<number | undefined>();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('has: when missing', () => {
|
|
32
|
+
const result = myCache.has('asdf');
|
|
33
|
+
expect(result).toEqual(false);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('has: when present', () => {
|
|
37
|
+
myCache.set('abc', 123);
|
|
38
|
+
|
|
39
|
+
const result = myCache.has('abc');
|
|
40
|
+
expect(result).toEqual(true);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('get: when missing', () => {
|
|
44
|
+
const result = myCache.get('asdf');
|
|
45
|
+
expect(result).toBeUndefined();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('get: when present', () => {
|
|
49
|
+
myCache.set('abc', 123);
|
|
50
|
+
|
|
51
|
+
const result = myCache.get('abc');
|
|
52
|
+
expect(result).toEqual(123);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('getAll: when empty', () => {
|
|
56
|
+
const result = myCache.getAll();
|
|
57
|
+
expect(result).toEqual({});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('getAll: when present', () => {
|
|
61
|
+
myCache.set('abc', 123);
|
|
62
|
+
|
|
63
|
+
const result = myCache.getAll();
|
|
64
|
+
expect(result).toEqual({ abc: 123 });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('set: when new', () => {
|
|
68
|
+
const result = myCache.set('abc', 123);
|
|
69
|
+
expect(result).toEqual(123);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('set: when already present', () => {
|
|
73
|
+
myCache.set('abc', 123);
|
|
74
|
+
|
|
75
|
+
const result = myCache.set('abc', 456);
|
|
76
|
+
expect(result).toEqual(456);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('remove and return value', () => {
|
|
80
|
+
myCache.set('abc', 123);
|
|
81
|
+
|
|
82
|
+
const result = myCache.remove('abc');
|
|
83
|
+
expect(result).toEqual(true);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('remove, then check', () => {
|
|
87
|
+
myCache.set('abc', 123);
|
|
88
|
+
myCache.remove('abc');
|
|
89
|
+
|
|
90
|
+
const result = myCache.has('abc');
|
|
91
|
+
expect(result).toEqual(false);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('remove, then get', () => {
|
|
95
|
+
myCache.set('abc', 123);
|
|
96
|
+
myCache.remove('abc');
|
|
97
|
+
|
|
98
|
+
const result = myCache.get('abc');
|
|
99
|
+
expect(result).toEqual(undefined);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('remove undefined values', () => {
|
|
103
|
+
myCache.set('abc', 123);
|
|
104
|
+
myCache.remove('abc');
|
|
105
|
+
|
|
106
|
+
const result = myCache.get('abc');
|
|
107
|
+
expect(result).toEqual(undefined);
|
|
108
|
+
|
|
109
|
+
myCache.set('abc', undefined);
|
|
110
|
+
myCache.remove('abc');
|
|
111
|
+
|
|
112
|
+
const result2 = myCache.get('abc');
|
|
113
|
+
expect(result2).toEqual(undefined);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe('provides functions for managing options and meta', () => {
|
|
118
|
+
let myCache: LimitedCacheInstance<unknown>;
|
|
119
|
+
beforeEach(() => {
|
|
120
|
+
myCache = LimitedCache<unknown>();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('getCacheMeta', () => {
|
|
124
|
+
const result = myCache.getCacheMeta();
|
|
125
|
+
|
|
126
|
+
expect(result).toEqual({
|
|
127
|
+
limitedCacheMetaVersion: 2,
|
|
128
|
+
options: defaultOptions,
|
|
129
|
+
cache: {},
|
|
130
|
+
keyList: [],
|
|
131
|
+
keyInfo: {},
|
|
132
|
+
opsLeft: 200,
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('getOptions', () => {
|
|
137
|
+
const result = myCache.getOptions();
|
|
138
|
+
|
|
139
|
+
expect(result).toEqual(defaultOptions);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('setOptions and return value', () => {
|
|
143
|
+
const result = myCache.setOptions({
|
|
144
|
+
maxCacheSize: 123,
|
|
145
|
+
maxCacheTime: 456,
|
|
146
|
+
warnIfItemPurgedBeforeTime: 999,
|
|
147
|
+
opLimit: 10,
|
|
148
|
+
scanLimit: 100,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
expect(result).toEqual({
|
|
152
|
+
...defaultOptions,
|
|
153
|
+
maxCacheSize: 123,
|
|
154
|
+
maxCacheTime: 456,
|
|
155
|
+
warnIfItemPurgedBeforeTime: 999,
|
|
156
|
+
opLimit: 10,
|
|
157
|
+
scanLimit: 100,
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('setOptions as mutation', () => {
|
|
162
|
+
myCache.setOptions({
|
|
163
|
+
maxCacheSize: 123,
|
|
164
|
+
maxCacheTime: 456,
|
|
165
|
+
warnIfItemPurgedBeforeTime: 999,
|
|
166
|
+
opLimit: 10,
|
|
167
|
+
scanLimit: 100,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
const result = myCache.getOptions();
|
|
171
|
+
expect(result).toEqual({
|
|
172
|
+
...defaultOptions,
|
|
173
|
+
maxCacheSize: 123,
|
|
174
|
+
maxCacheTime: 456,
|
|
175
|
+
warnIfItemPurgedBeforeTime: 999,
|
|
176
|
+
opLimit: 10,
|
|
177
|
+
scanLimit: 100,
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('doMaintenance', () => {
|
|
182
|
+
// should not throw
|
|
183
|
+
myCache.doMaintenance();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
});
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { describe, beforeEach, expect, it } from 'vitest';
|
|
2
|
+
import { defaultOptions } from '../core/defaultOptions';
|
|
3
|
+
import { LimitedCacheObject, getCacheMetaFromObject } from '../core/LimitedCacheObject';
|
|
4
|
+
import { LimitedCacheObjectInstance } from '../types';
|
|
5
|
+
|
|
6
|
+
describe('LimitedCacheObject', () => {
|
|
7
|
+
it('initializes without options', () => {
|
|
8
|
+
const myCache: LimitedCacheObjectInstance = LimitedCacheObject();
|
|
9
|
+
|
|
10
|
+
expect(myCache).toBeTruthy();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('initializes with options', () => {
|
|
14
|
+
const myCache: LimitedCacheObjectInstance = LimitedCacheObject({
|
|
15
|
+
maxCacheSize: 123,
|
|
16
|
+
maxCacheTime: 456,
|
|
17
|
+
warnIfItemPurgedBeforeTime: 999,
|
|
18
|
+
opLimit: 10,
|
|
19
|
+
scanLimit: 100,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
expect(myCache).toBeTruthy();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('look like a standard object', () => {
|
|
26
|
+
let myCache: LimitedCacheObjectInstance<number>;
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
myCache = LimitedCacheObject<number>();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('has: when missing, via prototype hasOwnProperty', () => {
|
|
32
|
+
const result = Object.prototype.hasOwnProperty.call(myCache, 'asdf');
|
|
33
|
+
expect(result).toEqual(false);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('has: when missing, via local hasOwnProperty', () => {
|
|
37
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
38
|
+
const result = myCache.hasOwnProperty('asdf');
|
|
39
|
+
expect(result).toEqual(false);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('has: when missing, via "in"', () => {
|
|
43
|
+
const result = 'asdf' in myCache;
|
|
44
|
+
expect(result).toEqual(false);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('has: when present, via prototype hasOwnProperty', () => {
|
|
48
|
+
myCache.abc = 123;
|
|
49
|
+
|
|
50
|
+
const result = Object.prototype.hasOwnProperty.call(myCache, 'abc');
|
|
51
|
+
expect(result).toEqual(true);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('has: when present, via local hasOwnProperty', () => {
|
|
55
|
+
myCache.abc = 123;
|
|
56
|
+
|
|
57
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
58
|
+
const result = myCache.hasOwnProperty('abc');
|
|
59
|
+
expect(result).toEqual(true);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('has: when present, via "in"', () => {
|
|
63
|
+
myCache.abc = 123;
|
|
64
|
+
|
|
65
|
+
const result = 'abc' in myCache;
|
|
66
|
+
expect(result).toEqual(true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('get: when missing', () => {
|
|
70
|
+
const result = myCache.asdf;
|
|
71
|
+
expect(result).toBeUndefined();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('get: when present', () => {
|
|
75
|
+
myCache.abc = 123;
|
|
76
|
+
|
|
77
|
+
const result = myCache.abc;
|
|
78
|
+
expect(result).toEqual(123);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('set: when new', () => {
|
|
82
|
+
const result = (myCache.abc = 123);
|
|
83
|
+
expect(result).toEqual(123);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('set: when already present', () => {
|
|
87
|
+
myCache.abc = 123;
|
|
88
|
+
|
|
89
|
+
const result = (myCache.abc = 456);
|
|
90
|
+
expect(result).toEqual(456);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('delete and return value', () => {
|
|
94
|
+
myCache.abc = 123;
|
|
95
|
+
|
|
96
|
+
const result = delete myCache.abc;
|
|
97
|
+
expect(result).toEqual(true);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('delete, then check', () => {
|
|
101
|
+
myCache.abc = 123;
|
|
102
|
+
delete myCache.abc;
|
|
103
|
+
|
|
104
|
+
const result = Object.prototype.hasOwnProperty.call(myCache, 'abc');
|
|
105
|
+
expect(result).toEqual(false);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('delete, then get', () => {
|
|
109
|
+
myCache.abc = 123;
|
|
110
|
+
delete myCache.abc;
|
|
111
|
+
|
|
112
|
+
const result = myCache.abc;
|
|
113
|
+
expect(result).toEqual(undefined);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('keys: when empty', () => {
|
|
117
|
+
const result = Object.keys(myCache);
|
|
118
|
+
expect(result).toEqual([]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('keys: when populated', () => {
|
|
122
|
+
myCache.abc = 123;
|
|
123
|
+
|
|
124
|
+
const result = Object.keys(myCache);
|
|
125
|
+
expect(result).toEqual(['abc']);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('keys: when all are removed', () => {
|
|
129
|
+
myCache.abc = 123;
|
|
130
|
+
delete myCache.abc;
|
|
131
|
+
|
|
132
|
+
const result = Object.keys(myCache);
|
|
133
|
+
expect(result).toEqual([]);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('offers a way to get the cacheMeta', () => {
|
|
138
|
+
const myCache = LimitedCacheObject<number>();
|
|
139
|
+
const result = getCacheMetaFromObject(myCache);
|
|
140
|
+
|
|
141
|
+
expect(result).toEqual({
|
|
142
|
+
limitedCacheMetaVersion: 2,
|
|
143
|
+
options: defaultOptions,
|
|
144
|
+
cache: {},
|
|
145
|
+
keyList: [],
|
|
146
|
+
keyInfo: {},
|
|
147
|
+
opsLeft: 200,
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
});
|