create-memo
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -5,3 +5,3 @@ 'use strict'; | ||
class LinkedMapCache { | ||
class LinkedMap { | ||
constructor() { | ||
@@ -87,3 +87,3 @@ this.head = void 0; | ||
class LRUCache extends LinkedMapCache { | ||
class LRUMap extends LinkedMap { | ||
constructor(options) { | ||
@@ -134,3 +134,3 @@ super(); | ||
class MapCache { | ||
class OrderedMap { | ||
constructor() { | ||
@@ -175,3 +175,3 @@ this.map = new Map(); | ||
this.expireAfterAccess = expireAfterAccess; | ||
this.cache = !maxSize ? new MapCache() : new LRUCache({ | ||
this.cache = !maxSize ? new OrderedMap() : new LRUMap({ | ||
maxSize | ||
@@ -195,2 +195,4 @@ }); | ||
} | ||
return undefined; | ||
} | ||
@@ -221,3 +223,3 @@ | ||
function assertFn(fn) { | ||
if (typeof fn !== "function") { | ||
if (typeof fn !== 'function') { | ||
throw new TypeError("Memo: 'fn' expected to be a 'function'."); | ||
@@ -245,13 +247,34 @@ } | ||
class PromiseMemoCache extends MemoCache { | ||
prime(key, value) { | ||
return super.prime(key, Promise.resolve(value).catch(error => { | ||
this.clear(key); | ||
return Promise.reject(error); | ||
})); | ||
} | ||
} | ||
function createPromiseMemo(fn, options) { | ||
assertFn(fn); | ||
const memo = createMemo(key => Promise.resolve(fn(key)).catch(error => { | ||
memo.cache.clear(key); | ||
return Promise.reject(error); | ||
}), options); | ||
const cache = new PromiseMemoCache(options); | ||
memo.cache = cache; | ||
return memo; | ||
function memo(key) { | ||
let value = cache.get(key); | ||
if (value === undefined) { | ||
cache.prime(key, fn(key)); | ||
value = cache.get(key); | ||
} | ||
return value; | ||
} | ||
} | ||
exports.MemoCache = MemoCache; | ||
exports.PromiseMemoCache = PromiseMemoCache; | ||
exports.createMemo = createMemo; | ||
exports.createPromiseMemo = createPromiseMemo; | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,4 @@ | ||
export * from "./memo-cache/MemoCache.js"; | ||
export * from "./memo/createMemo.js"; | ||
export * from "./promise-memo/createPromiseMemo.js"; | ||
export * from "./MemoCache.js"; | ||
export * from "./createMemo.js"; | ||
export * from "./PromiseMemoCache.js"; | ||
export * from "./createPromiseMemo.js"; |
export function assertFn(fn) { | ||
if (typeof fn !== "function") { | ||
if (typeof fn !== 'function') { | ||
throw new TypeError("Memo: 'fn' expected to be a 'function'."); | ||
} | ||
} |
@@ -1,3 +0,48 @@ | ||
export * from "./memo-cache/MemoCache"; | ||
export * from "./memo/createMemo"; | ||
export * from "./promise-memo/createPromiseMemo"; | ||
interface MapLike<TKey, TValue> { | ||
get(key: TKey): TValue | undefined; | ||
prime(key: TKey, value: TValue): this; | ||
clear(key: TKey): this; | ||
clearAll(): this; | ||
} | ||
declare type MemoCacheKeyType<TKey> = TKey | string | number; | ||
declare type MemoCacheKeyFn<TKey> = (key: TKey) => MemoCacheKeyType<TKey>; | ||
interface MemoCacheOptions<TKey> { | ||
readonly maxSize?: number; | ||
readonly cacheKeyFn?: MemoCacheKeyFn<TKey>; | ||
readonly expireAfterWrite?: number; | ||
readonly expireAfterAccess?: number; | ||
} | ||
interface MemoCacheNode<TValue> { | ||
value: TValue; | ||
expiresAt: number; | ||
} | ||
declare class MemoCache<TKey, TValue> implements MapLike<TKey, TValue> { | ||
protected readonly cache: MapLike<MemoCacheKeyType<TKey>, MemoCacheNode<TValue>>; | ||
protected readonly cacheKeyFn: MemoCacheKeyFn<TKey>; | ||
protected readonly expireAfterWrite: number; | ||
protected readonly expireAfterAccess: number; | ||
constructor({ maxSize, expireAfterWrite, expireAfterAccess, cacheKeyFn, }?: MemoCacheOptions<TKey>); | ||
get(key: TKey): TValue | undefined; | ||
prime(key: TKey, value: TValue): this; | ||
clear(key: TKey): this; | ||
clearAll(): this; | ||
} | ||
interface MemoFn<TKey, TValue> { | ||
(key: TKey): TValue; | ||
readonly cache: MemoCache<TKey, TValue>; | ||
} | ||
declare function createMemo<TKey, TValue>(fn: (key: TKey) => TValue, options?: MemoCacheOptions<TKey>): MemoFn<TKey, TValue>; | ||
declare class PromiseMemoCache<TKey, TValue> extends MemoCache<TKey, Promise<TValue>> { | ||
prime(key: TKey, value: TValue | Promise<TValue>): this; | ||
} | ||
interface PromiseMemoFn<TKey, TValue> { | ||
(key: TKey): Promise<TValue>; | ||
readonly cache: PromiseMemoCache<TKey, TValue>; | ||
} | ||
declare function createPromiseMemo<TKey, TValue>(fn: (key: TKey) => Promise<TValue>, options?: MemoCacheOptions<TKey>): PromiseMemoFn<TKey, TValue>; | ||
export { MemoCache, MemoCacheKeyFn, MemoCacheKeyType, MemoCacheOptions, MemoFn, PromiseMemoCache, PromiseMemoFn, createMemo, createPromiseMemo }; |
@@ -1,40 +0,2 @@ | ||
function _slicedToArray(arr, i) { | ||
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); | ||
} | ||
function _arrayWithHoles(arr) { | ||
if (Array.isArray(arr)) return arr; | ||
} | ||
function _iterableToArrayLimit(arr, i) { | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _e = undefined; | ||
try { | ||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { | ||
_arr.push(_s.value); | ||
if (i && _arr.length === i) break; | ||
} | ||
} catch (err) { | ||
_d = true; | ||
_e = err; | ||
} finally { | ||
try { | ||
if (!_n && _i["return"] != null) _i["return"](); | ||
} finally { | ||
if (_d) throw _e; | ||
} | ||
} | ||
return _arr; | ||
} | ||
function _nonIterableRest() { | ||
throw new TypeError("Invalid attempt to destructure non-iterable instance"); | ||
} | ||
class LinkedMapCache { | ||
class LinkedMap { | ||
constructor() { | ||
@@ -120,3 +82,3 @@ this.head = void 0; | ||
class LRUCache extends LinkedMapCache { | ||
class LRUMap extends LinkedMap { | ||
constructor(options) { | ||
@@ -140,3 +102,5 @@ super(); | ||
removeLatterlyUsed() { | ||
const maxSize = this.options.maxSize; | ||
const { | ||
maxSize | ||
} = this.options; | ||
const itemsToRemove = this.map.size - maxSize + 1; | ||
@@ -148,6 +112,5 @@ | ||
for (let i = 0; i < itemsToRemove; i++) { | ||
const _entries$next = entries.next(), | ||
_entries$next$value = _slicedToArray(_entries$next.value, 1), | ||
key = _entries$next$value[0]; | ||
const { | ||
value: [key] | ||
} = entries.next(); | ||
this.clear(key); | ||
@@ -168,3 +131,3 @@ } | ||
class MapCache { | ||
class OrderedMap { | ||
constructor() { | ||
@@ -196,12 +159,8 @@ this.map = new Map(); | ||
class MemoCache { | ||
constructor() { | ||
let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
maxSize = _ref.maxSize, | ||
_ref$expireAfterWrite = _ref.expireAfterWrite, | ||
expireAfterWrite = _ref$expireAfterWrite === void 0 ? 0 : _ref$expireAfterWrite, | ||
_ref$expireAfterAcces = _ref.expireAfterAccess, | ||
expireAfterAccess = _ref$expireAfterAcces === void 0 ? 0 : _ref$expireAfterAcces, | ||
_ref$cacheKeyFn = _ref.cacheKeyFn, | ||
cacheKeyFn = _ref$cacheKeyFn === void 0 ? key => key : _ref$cacheKeyFn; | ||
constructor({ | ||
maxSize, | ||
expireAfterWrite = 0, | ||
expireAfterAccess = 0, | ||
cacheKeyFn = key => key | ||
} = {}) { | ||
this.cache = void 0; | ||
@@ -214,3 +173,3 @@ this.cacheKeyFn = void 0; | ||
this.expireAfterAccess = expireAfterAccess; | ||
this.cache = !maxSize ? new MapCache() : new LRUCache({ | ||
this.cache = !maxSize ? new OrderedMap() : new LRUMap({ | ||
maxSize | ||
@@ -234,2 +193,4 @@ }); | ||
} | ||
return undefined; | ||
} | ||
@@ -260,3 +221,3 @@ | ||
function assertFn(fn) { | ||
if (typeof fn !== "function") { | ||
if (typeof fn !== 'function') { | ||
throw new TypeError("Memo: 'fn' expected to be a 'function'."); | ||
@@ -284,11 +245,31 @@ } | ||
class PromiseMemoCache extends MemoCache { | ||
prime(key, value) { | ||
return super.prime(key, Promise.resolve(value).catch(error => { | ||
this.clear(key); | ||
return Promise.reject(error); | ||
})); | ||
} | ||
} | ||
function createPromiseMemo(fn, options) { | ||
assertFn(fn); | ||
const memo = createMemo(key => Promise.resolve(fn(key)).catch(error => { | ||
memo.cache.clear(key); | ||
return Promise.reject(error); | ||
}), options); | ||
const cache = new PromiseMemoCache(options); | ||
memo.cache = cache; | ||
return memo; | ||
function memo(key) { | ||
let value = cache.get(key); | ||
if (value === undefined) { | ||
cache.prime(key, fn(key)); | ||
value = cache.get(key); | ||
} | ||
return value; | ||
} | ||
} | ||
export { MemoCache, createMemo, createPromiseMemo }; | ||
export { MemoCache, PromiseMemoCache, createMemo, createPromiseMemo }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "create-memo", | ||
"description": "Memoization utils for JavaScript", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"license": "MIT", | ||
"esnext": "dist-src/index.js", | ||
"main": "dist-node/index.js", | ||
"module": "dist-web/index.js", | ||
"types": "dist-types/index.d.ts", | ||
"pika": true, | ||
"sideEffects": false, | ||
"files": [ | ||
"dist-*/", | ||
"assets/", | ||
"bin/" | ||
], | ||
"pika": true, | ||
"sideEffects": false, | ||
"repository": "https://github.com/umidbekkarimov/memo.git", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@babel/core": "^7.0.0-0", | ||
"@babel/plugin-proposal-class-properties": "^7.4.0", | ||
"@babel/preset-env": "^7.4.2", | ||
"@babel/preset-typescript": "^7.3.3", | ||
"@dc0de/eslint-config": "^0.5.7", | ||
"@dc0de/eslint-config-base": "^0.5.0", | ||
"@dc0de/eslint-config-node": "^0.5.5", | ||
"@dc0de/jest-preset": "^0.0.8", | ||
"@pika/pack": "^0.3.6", | ||
"@pika/plugin-build-node": "^0.3.14", | ||
"@pika/plugin-build-types": "^0.3.14", | ||
"@pika/plugin-build-web": "^0.3.14", | ||
"@pika/plugin-standard-pkg": "^0.3.14", | ||
"@types/jest": "^24.0.11", | ||
"@typescript-eslint/eslint-plugin": "^1.4.2", | ||
"@typescript-eslint/parser": "^1.4.2", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-jest": "^23.6.0", | ||
"codecov": "^3.2.0", | ||
"eslint": "^5.15.3", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-config-prettier": "^4.0.0", | ||
"eslint-import-resolver-typescript": "^1.1.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-node": "^8.0.0", | ||
"import-sort": "^6.0.0", | ||
"import-sort-parser-typescript": "^6.0.0", | ||
"import-sort-style-module": "^6.0.0", | ||
"jest": "^24.5.0", | ||
"jest-circus": "^23.6.0", | ||
"prettier": "^1.16.4", | ||
"stylotron": "^0.0.37", | ||
"typescript": "^3.4.1" | ||
"@babel/plugin-proposal-class-properties": "^7.7.4", | ||
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.7.4", | ||
"@babel/plugin-proposal-optional-chaining": "^7.7.5", | ||
"@babel/preset-env": "^7.7.6", | ||
"@babel/preset-typescript": "^7.7.4", | ||
"@pika/pack": "^0.5.0", | ||
"@pika/plugin-build-node": "^0.8.1", | ||
"@pika/plugin-build-types": "^0.8.1", | ||
"@pika/plugin-build-web": "^0.8.1", | ||
"@pika/plugin-bundle-types": "^0.8.1", | ||
"@pika/plugin-standard-pkg": "^0.8.1", | ||
"@superdispatch/eslint-plugin": "^0.1.30", | ||
"@superdispatch/js-tools": "^0.1.30", | ||
"@superdispatch/prettier-config": "^0.1.26", | ||
"@superdispatch/tsconfig": "^0.1.13", | ||
"@types/jest": "^24.0.23", | ||
"codecov": "^3.6.1", | ||
"eslint": "^6.7.2", | ||
"jest": "^24.9.0", | ||
"prettier": "^1.19.1", | ||
"typescript": "^3.7.3" | ||
}, | ||
"engines": { | ||
"node": ">=8.0.0" | ||
"node": ">=10.0.0" | ||
}, | ||
"private": false | ||
"esnext": "dist-src/index.js", | ||
"main": "dist-node/index.js", | ||
"module": "dist-web/index.js", | ||
"types": "dist-types/index.d.ts" | ||
} |
@@ -5,2 +5,4 @@ # memo | ||
[![CircleCI](https://circleci.com/gh/umidbekkarimov/memo.svg?style=svg)](https://circleci.com/gh/umidbekkarimov/memo) | ||
[![codecov](https://codecov.io/gh/umidbekkarimov/memo/branch/master/graph/badge.svg)](https://codecov.io/gh/umidbekkarimov/memo) | ||
[![npm version](https://img.shields.io/npm/v/create-memo.svg)](https://npmjs.com/create-memo) | ||
@@ -10,4 +12,10 @@ [![npm minzipped size](https://img.shields.io/bundlephobia/minzip/create-memo.svg)](https://bundlephobia.com/result?p=create-memo) | ||
[![npm downloads](https://img.shields.io/npm/dm/create-memo.svg)](https://npmjs.com/create-memo) | ||
[![Build Status](https://travis-ci.com/umidbekkarimov/memo.svg?branch=master)](https://travis-ci.com/umidbekkarimov/memo) | ||
[![codecov](https://codecov.io/gh/umidbekkarimov/memo/branch/master/graph/badge.svg)](https://codecov.io/gh/umidbekkarimov/memo) | ||
[![npm license](https://img.shields.io/npm/l/create-memo.svg)](https://npmjs.com/create-memo) | ||
### Installation | ||
```bash | ||
npm i create-memo | ||
``` | ||
### Usage |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43056
21
20
18
685
1