My first npm package is about sets

dbalazs97

D贸ka Bal谩zs

Posted on February 16, 2021

My first npm package is about sets

Hi! This is my first DEV post. Recently I had to use some sets in my Javascript code that needed to contain objects as items. The problem is the browser Set object compares their items by equality. So I decide to create a micro-library in which you can use sets that can handle all the set operation comaprinf the items with an id as a key.

Let me introduce you to the library SmartSet

Installation

If you want to use my library you can install it easily by npm or yarn.

npm install smart-set

yarn add smart-set
Enter fullscreen mode Exit fullscreen mode

Creation and operations

The SmartSet class extends the Set class, so every member of Set is available, and compatible with the Set interface.

Constructor

SmartSet has two template paramters:

  • T is the type of items
  • ID is the type of the key (must be primitive)

SmartSet constructor requires a function that determines the id of an item which should be unique and primitive. The second parameter is a list of items of T, which will be the starting data of the set. These items will be unique in the set.

import { SmartSet } from "smart-set";

interface Data { id: string };

const mySet = new SmartSet<Data, string>(item => item.id, [{ id: 'id1' }, { id: 'id2' }, { id: 'id1' }]);
Enter fullscreen mode Exit fullscreen mode

Addition

The add(item: T) and addAll(items: T[]) functions add item(s) to the set uniquely by the given id function.

mySet.add({ id: 'id3' });
mySet.add({ id: 'id1' });
mySet.addAll([{ id: 'id1' }, { id: 'id4' }, { id: 'id5' }]);
Enter fullscreen mode Exit fullscreen mode

Deletion

The delete(item: T) and deleteId(id: ID) functions delete an item by the given id function.
The clear() function deletes all elements.

mySet.delete({ id: 'id3' });
mySet.deleteId('id1');
mySet.clear();
Enter fullscreen mode Exit fullscreen mode

Containment

The has(item: T): boolean and hasId(id: ID): boolean check whether an item is part of the set by the given id function.

mySet.has({ id: 'id3' }) === false;
mySet.hasId('id1') === true;
Enter fullscreen mode Exit fullscreen mode

Iteration

SmartSet is iterable and has the keys(): IterableIterator<ID>, values(): IterableIterator<T> and entries(): IterableIterator<[ID, T]> functions for iterating the set. Keys are the values of the ID type.

const mySet = new SmartSet<Data, string>(item => item.id, [{ id: 'id1' }, { id: 'id2' }, { id: 'id3' }]);

[...mySet.keys()] === ['id1', 'id2', 'id3'];

[...mySet.values()] === [{ id: 'id1' }, { id: 'id2' }, { id: 'id3' }];

[...mySet.entries()] === [['id1', { id: 'id1' }], ['id2', { id: 'id2' }], ['id3', { id: 'id3' }]];

[...mySet] === [{ id: 'id1' }, { id: 'id2' }, { id: 'id3' }];

mySet.forEach((value, key, set) => { /* something with the key, value and set */ })
Enter fullscreen mode Exit fullscreen mode

Set operations

SmartSet has some set operation that corresponds to the mathematics representation. These operations are immutable, so they return a new SmartSet instance and does not modify the original set. The methods accept regular Set objects, because it runs the ID creation function on the items.

union(other: Set<T>): SmartSet<T, ID>

intersection(other: Set<T>): SmartSet<T, ID>

difference(other: Set<T>): SmartSet<T, ID>

symmetricDifference(other: Set<T>): SmartSet<T, ID>

let result;
const setA = new SmartSet<Data, string>(item => item.s, [ { s: '1' }, { s: '2' } ]);
const setB = new SmartSet<Data, string>(item => item.s, [ { s: '1' }, { s: '3' } ]);

result = setA.union(setB); //  [ { s: '1' }, { s: '2' }, { s: '3' } ]
result = setA.intersection(setB); //  [ { s: '1' } ]
result = setA.difference(setB); //  [ { s: '2' } ]
result = setB.difference(setA); //  [ { s: '3' } ]
result = setA.symmetricDifference(setB); //  [ { s: '2' }, { s: '3' } ]
Enter fullscreen mode Exit fullscreen mode

Closing words

As I am new to library publishing and open source world it would be a huge help if you could take a look at my source code or report an issue at the repository of the package at github. Thanks for reading my first post.

馃挅 馃挭 馃檯 馃毄
dbalazs97
D贸ka Bal谩zs

Posted on February 16, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related