@wixc3/patterns
Advanced tools
Comparing version 17.0.0 to 17.1.0
@@ -5,2 +5,3 @@ "use strict"; | ||
const common_1 = require("@wixc3/common"); | ||
const multi_map_1 = require("./multi-map"); | ||
/** | ||
@@ -20,3 +21,3 @@ * Maps keys to a set of values | ||
constructor(entries) { | ||
this.map = new Map(); | ||
this.map = new multi_map_1.MultiMap(); | ||
(0, common_1.forEach)(entries, ([key, val]) => { | ||
@@ -27,17 +28,11 @@ this.add(key, val); | ||
get size() { | ||
return (0, common_1.chain)(this.map) | ||
.map(([_, { size }]) => size) | ||
.reduce((sum, size) => sum + size, 0).value; | ||
return this.map.size; | ||
} | ||
get(key) { | ||
return this.map.get(key); | ||
var _a; | ||
const innerKeys = (_a = this.map.getInnerMap(key)) === null || _a === void 0 ? void 0 : _a.keys(); | ||
return innerKeys ? new Set(innerKeys) : undefined; | ||
} | ||
add(key, value) { | ||
const valueSet = this.map.get(key); | ||
if (valueSet) { | ||
valueSet.add(value); | ||
} | ||
else { | ||
this.map.set(key, new Set([value])); | ||
} | ||
this.map.set(key, value, true); | ||
return this; | ||
@@ -49,21 +44,12 @@ } | ||
delete(key, value) { | ||
const valueSet = this.map.get(key); | ||
if (valueSet) { | ||
const wasInSet = valueSet.delete(value); | ||
if (valueSet.size === 0) { | ||
this.map.delete(key); | ||
} | ||
return wasInSet; | ||
} | ||
return false; | ||
return this.map.delete(key, value); | ||
} | ||
deleteKey(key) { | ||
return this.map.delete(key); | ||
return this.map.deleteInnerMap(key); | ||
} | ||
has(key, value) { | ||
const valueSet = this.map.get(key); | ||
return valueSet ? valueSet.has(value) : false; | ||
return this.map.has(key, value); | ||
} | ||
hasKey(key) { | ||
const existingSet = this.map.get(key); | ||
const existingSet = this.get(key); | ||
return !!existingSet && existingSet.size > 0; | ||
@@ -76,16 +62,15 @@ } | ||
const { map } = this; | ||
for (const [key, valueSet] of map.entries()) { | ||
for (const value of valueSet) { | ||
yield [key, value]; | ||
} | ||
} | ||
yield* map.keys(); | ||
} | ||
*values() { | ||
const { map } = this; | ||
for (const valueSet of map.values()) { | ||
yield* valueSet.values(); | ||
for (const [_key, value] of map.keys()) { | ||
yield value; | ||
} | ||
} | ||
keys() { | ||
return this.map.keys(); | ||
*keys() { | ||
const { map } = this; | ||
for (const [key, _value] of map.keys()) { | ||
yield key; | ||
} | ||
} | ||
@@ -92,0 +77,0 @@ } |
@@ -1,2 +0,3 @@ | ||
import { forEach, chain } from '@wixc3/common'; | ||
import { forEach } from '@wixc3/common'; | ||
import { MultiMap } from './multi-map'; | ||
/** | ||
@@ -16,3 +17,3 @@ * Maps keys to a set of values | ||
constructor(entries) { | ||
this.map = new Map(); | ||
this.map = new MultiMap(); | ||
forEach(entries, ([key, val]) => { | ||
@@ -23,17 +24,11 @@ this.add(key, val); | ||
get size() { | ||
return chain(this.map) | ||
.map(([_, { size }]) => size) | ||
.reduce((sum, size) => sum + size, 0).value; | ||
return this.map.size; | ||
} | ||
get(key) { | ||
return this.map.get(key); | ||
var _a; | ||
const innerKeys = (_a = this.map.getInnerMap(key)) === null || _a === void 0 ? void 0 : _a.keys(); | ||
return innerKeys ? new Set(innerKeys) : undefined; | ||
} | ||
add(key, value) { | ||
const valueSet = this.map.get(key); | ||
if (valueSet) { | ||
valueSet.add(value); | ||
} | ||
else { | ||
this.map.set(key, new Set([value])); | ||
} | ||
this.map.set(key, value, true); | ||
return this; | ||
@@ -45,21 +40,12 @@ } | ||
delete(key, value) { | ||
const valueSet = this.map.get(key); | ||
if (valueSet) { | ||
const wasInSet = valueSet.delete(value); | ||
if (valueSet.size === 0) { | ||
this.map.delete(key); | ||
} | ||
return wasInSet; | ||
} | ||
return false; | ||
return this.map.delete(key, value); | ||
} | ||
deleteKey(key) { | ||
return this.map.delete(key); | ||
return this.map.deleteInnerMap(key); | ||
} | ||
has(key, value) { | ||
const valueSet = this.map.get(key); | ||
return valueSet ? valueSet.has(value) : false; | ||
return this.map.has(key, value); | ||
} | ||
hasKey(key) { | ||
const existingSet = this.map.get(key); | ||
const existingSet = this.get(key); | ||
return !!existingSet && existingSet.size > 0; | ||
@@ -72,16 +58,15 @@ } | ||
const { map } = this; | ||
for (const [key, valueSet] of map.entries()) { | ||
for (const value of valueSet) { | ||
yield [key, value]; | ||
} | ||
} | ||
yield* map.keys(); | ||
} | ||
*values() { | ||
const { map } = this; | ||
for (const valueSet of map.values()) { | ||
yield* valueSet.values(); | ||
for (const [_key, value] of map.keys()) { | ||
yield value; | ||
} | ||
} | ||
keys() { | ||
return this.map.keys(); | ||
*keys() { | ||
const { map } = this; | ||
for (const [key, _value] of map.keys()) { | ||
yield key; | ||
} | ||
} | ||
@@ -88,0 +73,0 @@ } |
{ | ||
"name": "@wixc3/patterns", | ||
"version": "17.0.0", | ||
"version": "17.1.0", | ||
"description": "A utility for saving objects to be disposed", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
@@ -1,2 +0,3 @@ | ||
import { forEach, chain } from '@wixc3/common'; | ||
import { forEach } from '@wixc3/common'; | ||
import { MultiMap } from './multi-map'; | ||
@@ -16,3 +17,3 @@ /** | ||
export class SetMultiMap<K, V> implements Iterable<[K, V]> { | ||
private map = new Map<K, Set<V>>(); | ||
private map = new MultiMap<K, V, boolean>(); | ||
@@ -26,18 +27,12 @@ constructor(entries?: Iterable<[K, V]>) { | ||
public get size(): number { | ||
return chain(this.map) | ||
.map(([_, { size }]) => size) | ||
.reduce((sum: number, size: number) => sum + size, 0).value; | ||
return this.map.size; | ||
} | ||
public get(key: K): ReadonlySet<V> | undefined { | ||
return this.map.get(key); | ||
const innerKeys = this.map.getInnerMap(key)?.keys(); | ||
return innerKeys ? new Set(innerKeys) : undefined; | ||
} | ||
public add(key: K, value: V): this { | ||
const valueSet = this.map.get(key); | ||
if (valueSet) { | ||
valueSet.add(value); | ||
} else { | ||
this.map.set(key, new Set([value])); | ||
} | ||
this.map.set(key, value, true); | ||
return this; | ||
@@ -51,24 +46,15 @@ } | ||
public delete(key: K, value: V): boolean { | ||
const valueSet = this.map.get(key); | ||
if (valueSet) { | ||
const wasInSet = valueSet.delete(value); | ||
if (valueSet.size === 0) { | ||
this.map.delete(key); | ||
} | ||
return wasInSet; | ||
} | ||
return false; | ||
return this.map.delete(key, value); | ||
} | ||
public deleteKey(key: K): boolean { | ||
return this.map.delete(key); | ||
return this.map.deleteInnerMap(key); | ||
} | ||
public has(key: K, value: V): boolean { | ||
const valueSet = this.map.get(key); | ||
return valueSet ? valueSet.has(value) : false; | ||
return this.map.has(key, value); | ||
} | ||
public hasKey(key: K): boolean { | ||
const existingSet = this.map.get(key); | ||
const existingSet = this.get(key); | ||
return !!existingSet && existingSet.size > 0; | ||
@@ -83,7 +69,3 @@ } | ||
const { map } = this; | ||
for (const [key, valueSet] of map.entries()) { | ||
for (const value of valueSet) { | ||
yield [key, value]; | ||
} | ||
} | ||
yield* map.keys(); | ||
} | ||
@@ -93,9 +75,12 @@ | ||
const { map } = this; | ||
for (const valueSet of map.values()) { | ||
yield* valueSet.values(); | ||
for (const [_key, value] of map.keys()) { | ||
yield value; | ||
} | ||
} | ||
public keys(): IterableIterator<K> { | ||
return this.map.keys(); | ||
public *keys(): IterableIterator<K> { | ||
const { map } = this; | ||
for (const [key, _value] of map.keys()) { | ||
yield key; | ||
} | ||
} | ||
@@ -102,0 +87,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
285981
139
5526