Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cache-mapset

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cache-mapset - npm Package Compare versions

Comparing version 1.0.0-beta.2 to 1.0.0-beta.3

2

esm/constants.d.ts
export declare const enum Msg {
InvalidCapacity = "capacity must be non-negative"
InvalidCapacity = "invalid capacity"
}

@@ -1,6 +0,6 @@

// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.
export var Msg;
(function (Msg) {
Msg["InvalidCapacity"] = "capacity must be non-negative";
Msg["InvalidCapacity"] = "invalid capacity";
})(Msg || (Msg = {}));

@@ -1,4 +0,4 @@

// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.
export { assertNonNegativeNumber } from "@miyauci/assertion/number/assert_non_negative_number.js";
export { EmplaceableMap } from "@miyauci/upsert";

@@ -1,2 +0,2 @@

// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

@@ -3,0 +3,0 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {

@@ -1,2 +0,2 @@

// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

@@ -3,0 +3,0 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {

@@ -1,2 +0,2 @@

// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

@@ -3,0 +3,0 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {

@@ -1,2 +0,2 @@

// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

@@ -3,0 +3,0 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {

@@ -1,2 +0,2 @@

// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

@@ -3,0 +3,0 @@ /** A `Map` or `Set` like constructor with a cache replacement policy.

@@ -0,1 +1,2 @@

/** `Map` without `Iterator` and its related members. */
export interface MapLike<K, V> {

@@ -15,2 +16,3 @@ /** The number of entries. */

}
/** `Set` without `Iterator` and its related members. */
export interface SetLike<T> {

@@ -17,0 +19,0 @@ /** The number of values. */

@@ -1,3 +0,3 @@

// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.
export {};

@@ -16,2 +16,6 @@ import { MapLike, SetLike } from "./types.js";

}
/** Returns either a normal completion containing either an integer, `Infinity`, or `-Infinity`.
* @see https://tc39.es/ecma262/#sec-tointegerorinfinity
*/
export declare function toIntegerOrInfinity(number: number): number;
export declare abstract class BaseSet<T> implements SetLike<T> {

@@ -18,0 +22,0 @@ protected abstract cache: MapLike<T, void>;

@@ -1,2 +0,2 @@

// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

@@ -22,4 +22,6 @@ import { assertNonNegativeNumber } from "./deps.js";

});
if (isNaN(capacity))
throw new RangeError(Msg.InvalidCapacity);
capacity = toIntegerOrInfinity(capacity);
assertNonNegativeNumber(capacity, Msg.InvalidCapacity, RangeError);
capacity = Math.floor(capacity);
this.cache = new Map();

@@ -41,2 +43,12 @@ this.capacity = capacity;

}
/** Returns either a normal completion containing either an integer, `Infinity`, or `-Infinity`.
* @see https://tc39.es/ecma262/#sec-tointegerorinfinity
*/
export function toIntegerOrInfinity(number) {
if (isNaN(number) || !number)
return 0;
if (number === Infinity || number === -Infinity)
return number;
return Math.trunc(number);
}
export class BaseSet {

@@ -43,0 +55,0 @@ has(value) {

@@ -5,3 +5,3 @@ {

"name": "cache-mapset",
"version": "1.0.0-beta.2",
"version": "1.0.0-beta.3",
"description": "Maps and Sets with cache replacement policies, TC39 proposal-policy-map-set implementation",

@@ -8,0 +8,0 @@ "keywords": [

@@ -22,172 +22,102 @@ # cache-mapset

### FIFO
All Map-like constructors specify capacity.
FIFO(First In, First Out) implementations.
When the limit is reached, the cache is adjusted according to the cache
replacement policy.
#### FIFOMap
When the upper limit is reached, replaces the entry with FIFO algorithm.
```ts
import { FIFOMap } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
import { LRUMap } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
import { assert, assertEquals } from "https://deno.land/std/testing/asserts.ts";
declare const maxNumOfEntries: number;
const map = new FIFOMap(maxNumOfEntries);
```
declare const capacity: 2;
#### FIFOSet
const map = new LRUMap<number, string>(capacity);
When the upper limit is reached, replaces the value with FIFO algorithm.
map.set(200, "Ok");
map.set(201, "Created");
```ts
import { FIFOSet } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
assertEquals(map.size, 2);
declare const maxNumOfValues: number;
const set = new FIFOSet(maxNumOfValues);
```
map.set(202, "Accepted");
### LIFO
LIFO(Last In, First Out) implementations.
#### LIFOMap
When the upper limit is reached, replaces the entry with LIFO algorithm.
```ts
import { LIFOMap } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
declare const maxNumOfEntries: number;
const map = new LIFOMap(maxNumOfEntries);
assertEquals(map.size, 2);
assert(map.has(201));
assert(map.has(202));
```
#### LIFOSet
It provides a Map-like constructor with the following cache-replacement-policy:
When the upper limit is reached, replaces the value with LIFO algorithm.
- [FIFO](https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics))
- [LIFO](https://en.wikipedia.org/wiki/LIFO)
- [LRU](https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU)
- [LFU](https://en.wikipedia.org/wiki/Least_frequently_used)
```ts
import { LIFOSet } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
### Set like
declare const maxNumOfValues: number;
const set = new LIFOSet(maxNumOfValues);
```
`SetLike` is a set-like constructor, with the same cache-replacement-policy.
### LRU
`LFUSet` preferentially removes item with fewer references (by `has` or `add`).
LRU(Least Recently Used) implementations.
#### LRUMap
When the upper limit is reached, replaces the entry with LRU algorithm.
```ts
import { LRUMap } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
import { LFUSet } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
import { assert, assertEquals } from "https://deno.land/std/testing/asserts.ts";
declare const maxNumOfEntries: number;
const map = new LRUMap(maxNumOfEntries);
```
declare const capacity: 2;
#### LRUSet
const set = new LFUSet<number>(capacity);
When the upper limit is reached, replaces the value with LRU algorithm.
set.add(200);
set.add(201);
```ts
import { LRUSet } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
assertEquals(set.size, 2);
assert(set.has(200));
declare const maxNumOfValues: number;
const set = new LRUSet(maxNumOfValues);
```
set.add(202);
### LFU
LFU(Least Frequently Used) implementations.
#### LFUMap
When the upper limit is reached, replaces the entry with LFU algorithm.
```ts
import { LFUMap } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
declare const maxNumOfEntries: number;
const map = new LFUMap(maxNumOfEntries);
assert(set.has(200));
assert(set.has(202));
```
#### LFUSet
### Initial value
When the upper limit is reached, replaces the value with LFU algorithm.
Accepts an initial value, like `Map` or `Set`. If overcapacity occurs, the cache
is adjusted according to the policy.
```ts
import { LFUSet } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
import { FIFOSet } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
declare const maxNumOfValues: number;
const set = new LFUSet(maxNumOfValues);
const set = new FIFOSet<number>(3, [0, 1, 2, 3, 4, 5]);
assertEquals(set.size, 3);
```
## Common
### Errors
List items common to all implementations.
All constructors specify a capacity as their first argument.
### Interface
If it is a negative number, an error is thrown.
All instance have following members.
MapLike:
```ts
interface MapLike<K, V> {
/** The number of entries. */
size: number;
import { FIFOMap } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";
/** Whether has an entry with the given {@link key}. */
has: (key: K) => boolean;
/** Returns the value of the entry with the given {@link key}, if any such entry exists; otherwise returns `undefined`. */
get: (key: K) => V | undefined;
/** Adds an entry with the given {@link key} mapped to the given {@link value}. */
set: (key: K, value: V) => this;
/** Deletes the entry with the given {@link key}. */
delete: (key: K) => boolean;
/** Removes all entries. */
clear: () => void;
}
assertThrows(() => new FIFOMap(-1));
```
SetLike:
### Difference from Map and Set
```ts
interface SetLike<T> {
/** The number of values. */
size: number;
`MapLike` and `SetLike` are not `Iterable`.
/** Whether has the given {@link value}. */
has: (value: T) => boolean;
The following members are not implemented.
/** Adds the given {@link value}. */
add: (value: T) => this;
- `Symbol.iterator`
- `forEach`
- `entries`
- `keys`
- `values`
/** Deletes the given {@link value}. */
delete: (value: T) => boolean;
Currently, these are outside the scope of the specification. For more
information, check
[Data iteration and order](https://github.com/tc39/proposal-policy-map-set/issues/3).
/** Removes all values. */
clear: () => void;
}
```
### Throwing error
All constructors specify a capacity as their first argument.
If it is a negative number, an error is thrown.
```ts
import { FIFOMap } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";
assertThrows(() => new FIFOMap(-1));
```
## API

@@ -194,0 +124,0 @@

export declare const enum Msg {
InvalidCapacity = "capacity must be non-negative"
InvalidCapacity = "invalid capacity"
}
"use strict";
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

@@ -8,3 +8,3 @@ Object.defineProperty(exports, "__esModule", { value: true });

(function (Msg) {
Msg["InvalidCapacity"] = "capacity must be non-negative";
Msg["InvalidCapacity"] = "invalid capacity";
})(Msg = exports.Msg || (exports.Msg = {}));
"use strict";
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

@@ -4,0 +4,0 @@ Object.defineProperty(exports, "__esModule", { value: true });

"use strict";
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

@@ -4,0 +4,0 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {

"use strict";
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

@@ -4,0 +4,0 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {

"use strict";
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

@@ -4,0 +4,0 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {

"use strict";
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

@@ -4,0 +4,0 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {

"use strict";
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

@@ -4,0 +4,0 @@ Object.defineProperty(exports, "__esModule", { value: true });

@@ -0,1 +1,2 @@

/** `Map` without `Iterator` and its related members. */
export interface MapLike<K, V> {

@@ -15,2 +16,3 @@ /** The number of entries. */

}
/** `Set` without `Iterator` and its related members. */
export interface SetLike<T> {

@@ -17,0 +19,0 @@ /** The number of values. */

"use strict";
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.
Object.defineProperty(exports, "__esModule", { value: true });

@@ -16,2 +16,6 @@ import { MapLike, SetLike } from "./types.js";

}
/** Returns either a normal completion containing either an integer, `Infinity`, or `-Infinity`.
* @see https://tc39.es/ecma262/#sec-tointegerorinfinity
*/
export declare function toIntegerOrInfinity(number: number): number;
export declare abstract class BaseSet<T> implements SetLike<T> {

@@ -18,0 +22,0 @@ protected abstract cache: MapLike<T, void>;

"use strict";
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseSet = exports.BaseMap = void 0;
exports.BaseSet = exports.toIntegerOrInfinity = exports.BaseMap = void 0;
const deps_js_1 = require("./deps.js");

@@ -25,4 +25,6 @@ const constants_js_1 = require("./constants.js");

});
if (isNaN(capacity))
throw new RangeError(constants_js_1.Msg.InvalidCapacity);
capacity = toIntegerOrInfinity(capacity);
(0, deps_js_1.assertNonNegativeNumber)(capacity, constants_js_1.Msg.InvalidCapacity, RangeError);
capacity = Math.floor(capacity);
this.cache = new Map();

@@ -45,2 +47,13 @@ this.capacity = capacity;

exports.BaseMap = BaseMap;
/** Returns either a normal completion containing either an integer, `Infinity`, or `-Infinity`.
* @see https://tc39.es/ecma262/#sec-tointegerorinfinity
*/
function toIntegerOrInfinity(number) {
if (isNaN(number) || !number)
return 0;
if (number === Infinity || number === -Infinity)
return number;
return Math.trunc(number);
}
exports.toIntegerOrInfinity = toIntegerOrInfinity;
class BaseSet {

@@ -47,0 +60,0 @@ has(value) {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc