Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/basic set operations #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,37 @@ const test = new MaFP([
# Usage
```javascript
test.filter(val => val);
// MaFP [Map] { 'A' => true, 'C' => true, 'D' => true }
MaFP [Map] { 'A' => true, 'C' => true, 'D' => true }

test.filterToArray(val => val);
// [ 'A', true ], [ 'C', true ], [ 'D', true ] ]
[ 'A', true ], [ 'C', true ], [ 'D', true ] ]

test.map(val => !val);
// MaFP [Map] { 'A' => false, 'B' => true, 'C' => false, 'D' => false }
MaFP [Map] { 'A' => false, 'B' => true, 'C' => false, 'D' => false }

test.mapToArray(val => !val);
// [ [ 'A', false ], [ 'B', true ], [ 'C', false ], [ 'D', false ] ]
[ [ 'A', false ], [ 'B', true ], [ 'C', false ], [ 'D', false ] ]

test.reduce((acc, curr) => acc + Number(curr), 0);
// 3
3

test.every(val => val);
// false
false

test.some(val => val);
// true
true

test.isSuperset(new MaFP([
["A", true],
["B", false]
]))
true
```

Also works with `.keys()` and `.values()`:
```javascript
test.keys().filter(key => key !== 'B');
// [ 'A', 'C', 'D' ]
[ 'A', 'C', 'D' ]

// ETC...
```
119 changes: 119 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,62 @@ export default class MaFP<K, V> extends Map<K, V> {
super(args);
}

static clone<K, V, T extends Map<K, V>>(toClone: T): MaFP<K, V> {
const _clone = new MaFP<K, V>();
// We could have just used the spread operator new MaFP<K, V>([...map]);
// but its faster to not create the array first
for (const elem of toClone) {
_clone.set(elem[0], elem[1]);
}
return _clone;
}

static union<K, V, T extends Map<K, V>>(mapA: T, mapB: T): MaFP<K, V> {
const _union = MaFP.clone<K, V, T>(mapA);
for (const elem of mapB) {
_union.set(elem[0], elem[1]);
}
return _union;
}

static intersection<K, V, T extends Map<K, V>>(mapA: T, mapB: T): MaFP<K, V> {
const _intersection = new MaFP<K, V>();
for (const elem of mapB) {
const entry = mapA.get(elem[0]);
if (entry && entry === elem[1]) {
_intersection.set(elem[0], elem[1]);
}
}
return _intersection;
}

static symmetricDifference<K, V, T extends Map<K, V>>(
mapA: T,
mapB: T,
): MaFP<K, V> {
const _difference = MaFP.clone<K, V, T>(mapA);
for (const elem of mapB) {
const entry = _difference.get(elem[0]);
if (entry && entry === elem[1]) {
_difference.delete(elem[0]);
} else {
_difference.set(elem[0], elem[1]);
}
}
return _difference;
}

static difference<K, V, T extends Map<K, V>>(mapA: T, mapB: T): MaFP<K, V> {
const _difference = MaFP.clone<K, V, T>(mapA);
for (const elem of mapB) {
const entry = _difference.get(elem[0]);
if (entry && entry === elem[1]) {
_difference.delete(elem[0]);
}
}
return _difference;
}

private _map<T>(fn: FnSig<K, V, T>, op: (key: K, value: T) => void): void {
this.forEach((val, key) => {
op(key, fn(val, key, this));
Expand Down Expand Up @@ -176,6 +232,69 @@ export default class MaFP<K, V> extends Map<K, V> {
values(): KeyOrValue<V> {
return this._defineProperties(() => super.values());
}

clone(): MaFP<K, V> {
const _clone = new MaFP<K, V>();
// We could have just used the spread operator new MaFP<K, V>([...map]);
// but its faster to not create the array first
for (const elem of this) {
_clone.set(elem[0], elem[1]);
}
return _clone;
}

isSuperset<T extends Map<K, V>>(subset: T): boolean {
for (const elem of subset) {
const entry = this.get(elem[0]);
if (!entry || entry !== elem[1]) {
return false;
}
}
return true;
}

union<T extends Map<K, V>>(otherMap: T): MaFP<K, V> {
const _union = this.clone();
for (const elem of otherMap) {
_union.set(elem[0], elem[1]);
}
return _union;
}

intersection<T extends Map<K, V>>(otherMap: T): MaFP<K, V> {
const _intersection = new MaFP<K, V>();
for (const elem of otherMap) {
const entry = this.get(elem[0]);
if (entry && entry === elem[1]) {
_intersection.set(elem[0], elem[1]);
}
}
return _intersection;
}

symmetricDifference<T extends Map<K, V>>(otherMap: T): MaFP<K, V> {
const _difference = this.clone();
for (const elem of otherMap) {
const entry = _difference.get(elem[0]);
if (entry && entry === elem[1]) {
_difference.delete(elem[0]);
} else {
_difference.set(elem[0], elem[1]);
}
}
return _difference;
}

difference<T extends Map<K, V>>(otherMap: T): MaFP<K, V> {
const _difference = this.clone();
for (const elem of otherMap) {
const entry = _difference.get(elem[0]);
if (entry && entry === elem[1]) {
_difference.delete(elem[0]);
}
}
return _difference;
}
}

interface KeyOrValue<T> extends IterableIterator<T> {
Expand Down