Skip to content

Commit

Permalink
Merge pull request #23 from olegnn/1.10.1
Browse files Browse the repository at this point in the history
`1.10.1`: minor improvements
  • Loading branch information
olegnn authored Aug 17, 2023
2 parents a21a9fe + 85842b3 commit 1f11c11
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 117 deletions.
34 changes: 17 additions & 17 deletions __tests__/__snapshots__/Root.js.snap
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Root input validation 1`] = `"\`totalStoragesLimit\`'s 5 is too small, expected 6 at least"`;
exports[`Root input validation 1`] = `"\`totalStoragesLimit\`'s value = \`5\` is too small, expected \`6\` at least"`;

exports[`Root input validation 2`] = `"\`totalLeavesLimit\` is invalid: 1.3"`;
exports[`Root input validation 2`] = `"\`totalLeavesLimit\` is invalid: \`1.3\`"`;

exports[`Root input validation 3`] = `"\`totalStoragesLimit\` is invalid: 1.3"`;
exports[`Root input validation 3`] = `"\`totalStoragesLimit\` is invalid: \`1.3\`"`;

exports[`Root input validation 4`] = `"\`totalLeafStoragesLimit\` is invalid: 1.3"`;
exports[`Root input validation 4`] = `"\`totalLeafStoragesLimit\` is invalid: \`1.3\`"`;

exports[`Root input validation 5`] = `"\`leavesPerStorageLimit\` is invalid: 1.3"`;
exports[`Root input validation 5`] = `"\`leavesPerStorageLimit\` is invalid: \`1.3\`"`;

exports[`Root input validation 6`] = `"Invalid depth, an expected natural number which is a safe integer"`;

exports[`Root input validation 7`] = `"\`totalLeavesLimit\` is invalid: -1"`;
exports[`Root input validation 7`] = `"\`totalLeavesLimit\` is invalid: \`-1\`"`;

exports[`Root input validation 8`] = `"\`totalStoragesLimit\` is invalid: -1"`;
exports[`Root input validation 8`] = `"\`totalStoragesLimit\` is invalid: \`-1\`"`;

exports[`Root input validation 9`] = `"\`totalLeafStoragesLimit\` is invalid: -1"`;
exports[`Root input validation 9`] = `"\`totalLeafStoragesLimit\` is invalid: \`-1\`"`;

exports[`Root input validation 10`] = `"\`leavesPerStorageLimit\` is invalid: -1"`;
exports[`Root input validation 10`] = `"\`leavesPerStorageLimit\` is invalid: \`-1\`"`;

exports[`Root input validation 11`] = `"Invalid depth, an expected natural number which is a safe integer"`;

exports[`Root input validation 12`] = `"\`totalLeavesLimit\` is invalid: -Infinity"`;
exports[`Root input validation 12`] = `"\`totalLeavesLimit\` is invalid: \`-Infinity\`"`;

exports[`Root input validation 13`] = `"\`totalStoragesLimit\` is invalid: -Infinity"`;
exports[`Root input validation 13`] = `"\`totalStoragesLimit\` is invalid: \`-Infinity\`"`;

exports[`Root input validation 14`] = `"\`totalLeafStoragesLimit\` is invalid: -Infinity"`;
exports[`Root input validation 14`] = `"\`totalLeafStoragesLimit\` is invalid: \`-Infinity\`"`;

exports[`Root input validation 15`] = `"\`leavesPerStorageLimit\` is invalid: -Infinity"`;
exports[`Root input validation 15`] = `"\`leavesPerStorageLimit\` is invalid: \`-Infinity\`"`;

exports[`Root input validation 16`] = `"Invalid depth, an expected natural number which is a safe integer"`;

exports[`Root input validation 17`] = `"\`totalLeavesLimit\` is invalid: 131232312313123110000"`;
exports[`Root input validation 17`] = `"\`totalLeavesLimit\` is invalid: \`131232312313123110000\`"`;

exports[`Root input validation 18`] = `"\`totalStoragesLimit\` is invalid: 131232312313123110000"`;
exports[`Root input validation 18`] = `"\`totalStoragesLimit\` is invalid: \`131232312313123110000\`"`;

exports[`Root input validation 19`] = `"\`totalLeafStoragesLimit\` is invalid: 131232312313123110000"`;
exports[`Root input validation 19`] = `"\`totalLeafStoragesLimit\` is invalid: \`131232312313123110000\`"`;

exports[`Root input validation 20`] = `"\`leavesPerStorageLimit\` is invalid: 131232312313123110000"`;
exports[`Root input validation 20`] = `"\`leavesPerStorageLimit\` is invalid: \`131232312313123110000\`"`;

exports[`Root input validation 21`] = `"Invalid depth, an expected natural number which is a safe integer"`;

Expand Down
48 changes: 24 additions & 24 deletions __tests__/__snapshots__/functions.js.snap
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Basic function memoize tests different limits 1`] = `"\`totalLeavesLimit\` is invalid: -1"`;
exports[`Basic function memoize tests different limits 1`] = `"\`totalLeavesLimit\` is invalid: \`-1\`"`;

exports[`Basic function memoize tests different limits 2`] = `"\`totalLeavesLimit\` is invalid: -Infinity"`;
exports[`Basic function memoize tests different limits 2`] = `"\`totalLeavesLimit\` is invalid: \`-Infinity\`"`;

exports[`Basic function memoize tests different limits 3`] = `"\`totalLeavesLimit\` is invalid: 0"`;
exports[`Basic function memoize tests different limits 3`] = `"\`totalLeavesLimit\` is invalid: \`0\`"`;

exports[`Basic function memoize tests different limits 4`] = `"\`totalLeavesLimit\` is invalid: 123456789123456780"`;
exports[`Basic function memoize tests different limits 4`] = `"\`totalLeavesLimit\` is invalid: \`123456789123456780\`"`;

exports[`Basic function memoize tests different limits 5`] = `"\`totalLeavesLimit\` is invalid: 1.231231"`;
exports[`Basic function memoize tests different limits 5`] = `"\`totalLeavesLimit\` is invalid: \`1.231231\`"`;

exports[`Basic function memoize tests different limits 6`] = `"\`totalLeavesLimit\` is invalid: NaN"`;
exports[`Basic function memoize tests different limits 6`] = `"\`totalLeavesLimit\` is invalid: \`NaN\`"`;

exports[`Basic function memoize tests different limits 7`] = `"\`totalStoragesLimit\` is invalid: -1"`;
exports[`Basic function memoize tests different limits 7`] = `"\`totalStoragesLimit\` is invalid: \`-1\`"`;

exports[`Basic function memoize tests different limits 8`] = `"\`totalStoragesLimit\` is invalid: -Infinity"`;
exports[`Basic function memoize tests different limits 8`] = `"\`totalStoragesLimit\` is invalid: \`-Infinity\`"`;

exports[`Basic function memoize tests different limits 9`] = `"\`totalStoragesLimit\` is invalid: 0"`;
exports[`Basic function memoize tests different limits 9`] = `"\`totalStoragesLimit\` is invalid: \`0\`"`;

exports[`Basic function memoize tests different limits 10`] = `"\`totalStoragesLimit\` is invalid: 123456789123456780"`;
exports[`Basic function memoize tests different limits 10`] = `"\`totalStoragesLimit\` is invalid: \`123456789123456780\`"`;

exports[`Basic function memoize tests different limits 11`] = `"\`totalStoragesLimit\` is invalid: 1.231231"`;
exports[`Basic function memoize tests different limits 11`] = `"\`totalStoragesLimit\` is invalid: \`1.231231\`"`;

exports[`Basic function memoize tests different limits 12`] = `"\`totalStoragesLimit\` is invalid: NaN"`;
exports[`Basic function memoize tests different limits 12`] = `"\`totalStoragesLimit\` is invalid: \`NaN\`"`;

exports[`Basic function memoize tests different limits 13`] = `"\`leavesPerStorageLimit\` is invalid: -1"`;
exports[`Basic function memoize tests different limits 13`] = `"\`leavesPerStorageLimit\` is invalid: \`-1\`"`;

exports[`Basic function memoize tests different limits 14`] = `"\`leavesPerStorageLimit\` is invalid: -Infinity"`;
exports[`Basic function memoize tests different limits 14`] = `"\`leavesPerStorageLimit\` is invalid: \`-Infinity\`"`;

exports[`Basic function memoize tests different limits 15`] = `"\`leavesPerStorageLimit\` is invalid: 0"`;
exports[`Basic function memoize tests different limits 15`] = `"\`leavesPerStorageLimit\` is invalid: \`0\`"`;

exports[`Basic function memoize tests different limits 16`] = `"\`leavesPerStorageLimit\` is invalid: 123456789123456780"`;
exports[`Basic function memoize tests different limits 16`] = `"\`leavesPerStorageLimit\` is invalid: \`123456789123456780\`"`;

exports[`Basic function memoize tests different limits 17`] = `"\`leavesPerStorageLimit\` is invalid: 1.231231"`;
exports[`Basic function memoize tests different limits 17`] = `"\`leavesPerStorageLimit\` is invalid: \`1.231231\`"`;

exports[`Basic function memoize tests different limits 18`] = `"\`leavesPerStorageLimit\` is invalid: NaN"`;
exports[`Basic function memoize tests different limits 18`] = `"\`leavesPerStorageLimit\` is invalid: \`NaN\`"`;

exports[`Basic function memoize tests different limits 19`] = `"\`totalLeafStoragesLimit\` is invalid: -1"`;
exports[`Basic function memoize tests different limits 19`] = `"\`totalLeafStoragesLimit\` is invalid: \`-1\`"`;

exports[`Basic function memoize tests different limits 20`] = `"\`totalLeafStoragesLimit\` is invalid: -Infinity"`;
exports[`Basic function memoize tests different limits 20`] = `"\`totalLeafStoragesLimit\` is invalid: \`-Infinity\`"`;

exports[`Basic function memoize tests different limits 21`] = `"\`totalLeafStoragesLimit\` is invalid: 0"`;
exports[`Basic function memoize tests different limits 21`] = `"\`totalLeafStoragesLimit\` is invalid: \`0\`"`;

exports[`Basic function memoize tests different limits 22`] = `"\`totalLeafStoragesLimit\` is invalid: 123456789123456780"`;
exports[`Basic function memoize tests different limits 22`] = `"\`totalLeafStoragesLimit\` is invalid: \`123456789123456780\`"`;

exports[`Basic function memoize tests different limits 23`] = `"\`totalLeafStoragesLimit\` is invalid: 1.231231"`;
exports[`Basic function memoize tests different limits 23`] = `"\`totalLeafStoragesLimit\` is invalid: \`1.231231\`"`;

exports[`Basic function memoize tests different limits 24`] = `"\`totalLeafStoragesLimit\` is invalid: NaN"`;
exports[`Basic function memoize tests different limits 24`] = `"\`totalLeafStoragesLimit\` is invalid: \`NaN\`"`;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fun-memoize",
"version": "1.10.0",
"version": "1.10.1",
"author": "Oleg Nosov <[email protected]>",
"description": "Performant configurable memoization module for modern JavaScript applications",
"dependencies": {
Expand Down
13 changes: 12 additions & 1 deletion src/iterables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ export function once<V>(value: V): Iterable<V> {
}

/**
* Creates an iterable which emits supplied arguments
* Creates an iterable which emits supplied arguments.
*/
export function values<V>(...args: V[]): Iterable<V> {
if (args.length === 0) return EMPTY_ITERABLE;

return {
[Symbol.iterator]() {
let idx = 0;
Expand Down Expand Up @@ -71,6 +73,8 @@ export function map<V, R>(
fn: (item: V) => R,
iterable: Iterable<V>
): Iterable<R> {
if (iterable === EMPTY_ITERABLE) return EMPTY_ITERABLE;

return {
[Symbol.iterator]() {
const iter = iterable[Symbol.iterator]();
Expand All @@ -95,6 +99,8 @@ export function flatMap<V, R>(
map: (item: V) => Iterable<R>,
iterable: Iterable<V>
): Iterable<R> {
if (iterable === EMPTY_ITERABLE) return EMPTY_ITERABLE;

return {
[Symbol.iterator]() {
const rootIter = iterable[Symbol.iterator]();
Expand Down Expand Up @@ -180,6 +186,9 @@ export function zip<L, R>(
leftIterable: Iterable<L>,
rightIterable: Iterable<R>
): Iterable<{ left: L; right: R }> {
if (leftIterable === EMPTY_ITERABLE || rightIterable === EMPTY_ITERABLE)
return EMPTY_ITERABLE;

return {
[Symbol.iterator]() {
let leftIter = leftIterable[Symbol.iterator]();
Expand Down Expand Up @@ -207,6 +216,8 @@ export function zip<L, R>(
* @param iterable
*/
export function cycle<V>(iterable: Iterable<V>): Iterable<V> {
if (iterable === EMPTY_ITERABLE) return EMPTY_ITERABLE;

return {
[Symbol.iterator]() {
let lastIter = iterable[Symbol.iterator]();
Expand Down
12 changes: 2 additions & 10 deletions src/memoize/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ export type NestedStorage<K, V> =
| LeafStorage<K, V>
| Storage<K, NestedStorage<K, V> | LeafStorage<K, V> | V>;

/**
* Storage class describing storage that may have child storage(s).
*/
type NestedStorageClass<K, V> = new (
params: StorageParams<K, V>,
parents: Iterable<ParentPath<K | NestedStorage<K, V>>>
) => NestedStorage<K, V>;

/**
* Params for the storage context.
*/
Expand Down Expand Up @@ -95,11 +87,11 @@ const limitIsValid = (limit: number) =>
*/
const ensureLimitIsValid = (name: string, value: number, min: number) => {
if (!limitIsValid(value)) {
throw new Error(`\`${name}\` is invalid: ${value}`);
throw new Error(`\`${name}\` is invalid: \`${value}\``);
}
if (value < min) {
throw new Error(
`\`${name}\`'s ${value} is too small, expected ${min} at least`
`\`${name}\`'s value = \`${value}\` is too small, expected \`${min}\` at least`
);
}
};
Expand Down
30 changes: 14 additions & 16 deletions src/memoize/RootLeafStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@ import { LeafStorage } from "./LeafStorage";
* Root cache strategy for leaf values.
*/
export class RootLeafStrategy<K, V> extends CacheStrategy<LeafStorage<K, V>> {
_leafStorageStrategy: CacheStrategy<LeafStorage<K, V>>;
_leaves: number;
leafStorageStrategy: CacheStrategy<LeafStorage<K, V>>;
leaves: number;

constructor(
leafCapacity: number,
strategy: CacheStrategy<LeafStorage<K, V>>
) {
super(leafCapacity);
this._leafStorageStrategy = strategy;
this._leaves = 0;
this.leafStorageStrategy = strategy;
this.leaves = 0;
}

/**
* Records a read access of the supplied value.
*/
read(value: LeafStorage<K, V>): Result<LeafStorage<K, V>> {
return this.handleInnerStrategyResult(
this._leafStorageStrategy.read(value)
);
return this.handleInnerStrategyResult(this.leafStorageStrategy.read(value));
}

/**
Expand All @@ -36,20 +34,20 @@ export class RootLeafStrategy<K, V> extends CacheStrategy<LeafStorage<K, V>> {
const res = !this.has(value)
? this.reservePlace()
: (Result.empty() as Result<LeafStorage<K, V>>);
this._leaves++;
this.leaves++;

return res.chain(
this.handleInnerStrategyResult(this._leafStorageStrategy.write(value))
this.handleInnerStrategyResult(this.leafStorageStrategy.write(value))
);
}

/**
* Removes supplied value from the cache.
*/
drop(value: LeafStorage<K, V>): boolean {
const dropped = this._leafStorageStrategy.drop(value);
const dropped = this.leafStorageStrategy.drop(value);
if (dropped) {
this._leaves -= (value as LeafStorage<K, V>).len();
this.leaves -= value.len();
}

return dropped;
Expand All @@ -59,21 +57,21 @@ export class RootLeafStrategy<K, V> extends CacheStrategy<LeafStorage<K, V>> {
* Returns underlying amount of tracked cache entries.
*/
len(): number {
return this._leaves;
return this.leaves;
}

/**
* Returns `true` if supplied node exists in cache.
*/
has(node: LeafStorage<K, V>) {
return this._leafStorageStrategy.has(node);
return this.leafStorageStrategy.has(node);
}

/**
* Peeks an item from the beginning of the queue.
*/
peek() {
return this._leafStorageStrategy.peek();
return this.leafStorageStrategy.peek();
}

/**
Expand All @@ -87,7 +85,7 @@ export class RootLeafStrategy<K, V> extends CacheStrategy<LeafStorage<K, V>> {
const storage = maybeStorage as LeafStorage<K, V>;
const removed = storage.take();
if (removed !== NO_VALUE) {
this._leaves--;
this.leaves--;
}

if (storage.isEmpty()) {
Expand All @@ -104,7 +102,7 @@ export class RootLeafStrategy<K, V> extends CacheStrategy<LeafStorage<K, V>> {
* Handles a result produced by calling one of the inner strategy methods.
*/
private handleInnerStrategyResult(result: Result<LeafStorage<K, V>>) {
forEach((removed) => (this._leaves -= removed.len()), result.removed);
forEach((removed) => (this.leaves -= removed.len()), result.removed);

return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/storage/WeakStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { empty } from "../iterables";
* Weak storage for values with non-primitive keys.
*/
export class WeakStorage<K extends NonPrimitive, V> extends Storage<K, V> {
weakMap: WeakMap<NonPrimitive, V>;
weakMap: WeakMap<K, V>;
addedEntries: number;

constructor(
Expand Down
13 changes: 4 additions & 9 deletions src/strategy/fifo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ export class FIFO<V> extends CacheStrategy<V> {
*
*/
drop(value: V): boolean {
const has = this.queue.has(value);
if (has) {
this.queue.drop(value);
}

return has;
return this.queue.drop(value) !== NO_VALUE;
}

/**
Expand All @@ -67,19 +62,19 @@ export class FIFO<V> extends CacheStrategy<V> {

const pushed = this.queue.pushBack(value);
if (pushed === NO_VALUE) {
throw new Error(`\`FIFO\`: failed to push a new cache entry`);
throw new Error("`FIFO`: failed to push a new cache entry");
}

return res.chain(Result.added(once(value)));
} else {
const item = this.queue.get(value);
if (item === NO_VALUE) {
throw new Error(`\`FIFO\`: cache entry doesn't exist`);
throw new Error("`FIFO`: cache entry doesn't exist");
}

const moved = this.queue.moveBack(item as ListNode<Single<V>>);
if (!moved) {
throw new Error(`\`FIFO\`: failed to move the cache entry`);
throw new Error("`FIFO`: failed to move the cache entry");
}

return Result.empty();
Expand Down
Loading

0 comments on commit 1f11c11

Please sign in to comment.