npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-set-utils

v0.2.0

Published

A collection of Set utilities similar to python's

Downloads

682

Readme

ts-set-utils

This library provides a small set of utility functions to Typescript's and Javascript's Set class. The ES6 Set has a relatively small api surface and this library fills some common set use cases. Python built-in set class is used as a api template. Find the package here

API

Union

Signature A ∪ B

function union<T> (...sets: Set<T>[]): Set<T>;

Performs a union on all sets provided. A union merge two or more sets together. Read more on wikipedia

alt text

Example

const set1 = new Set([1, 2, 3])
const set2 = new Set([2, 3, 6])

console.log(union(set1, set2));
// Set<number>: Set([1, 2, 3, 6])

Union Pair

Signature A ∪ B

function unionPair<T1, T2> (a: Set<T1>, b: Set<T2>): Set<T1 | T2>;

Performs a union on two sets of different types. This is a utility method of typescript users to mix the set element types

Example

interface Pet {
  pet: string
}
const set1 = new Set([1, 2, 3])
const set2 = new Set([{ pet: 'dog' }, { pet: 'cat' }])

console.log(union(set1, set2));
// Set<number | Pet>: Set([1, 2, 3 { pet: 'dog' }, { pet: 'cat' }])

Intersection

Signature A ∩ B

function intersection<T> (...sets: Set<T>[]): Set<T>;

Performs a intersection on all sets provided. A intersection finds common elements between two or more sets. Read more on wikipedia

alt text

Example

const set1 = new Set([1, 2, 3])
const set2 = new Set([2, 3, 4])

console.log(intersection(set1, set2));
// Set<number>: Set([2, 3])

Disjoint

Signature A ∩ B = ∅

function disjoint<T> (a: Set<T>, b: Set<T>): boolean;

Determine if two sets are disjoint. Two sets are disjoint when they share no elements. Read more on wikipedia

alt text

Example

const set1 = new Set([1, 2, 3])
const set2 = new Set([2, 3, 4])
const set3 = new Set([10])

console.log(disjoint(set1, set2));
// false

console.log(disjoint(set2, set3))
// true

Subset

Signature B ⊆ A

function subset<T> (a: Set<T>, b: Set<T>): boolean;

Determine if set B is a subset of set A. A set B is a subset of A if all elements of B are in set A. Read more on wikipedia

alt text

Example

const set1 = new Set([1, 2, 3])
const set2 = new Set([3, 4, 5])
const set3 = new Set([1, 2])

console.log(subset(set1, set2));
// false

console.log(subset(set1, set3));
// true

console.log(subset(set3, set1));
// false

Set Equal

Signature B = A or B ⊆ A && B ⊇ A

Determine if set B is equal to set A. From the wikipedia definition "Set A and B are equal if and only if they have precisely the same element". Read more on wikipedia

Example

const set1 = new Set([1, 2, 3])
const set2 = new Set([3, 4, 5])
const set3 = new Set([1, 2])

console.log(setEqual(set1, set2));
// false

console.log(setEqual(set2, set3));
// false

console.log(setEqual(set1, set1));
// true

Proper Subset

Signature B ⊂ A

function properSubset<T> (a: Set<T>, b: Set<T>): boolean;

Determine if set B is a proper subset of set A. A set B is a proper subset of A if all elements of B are in set A and A is a strictly larger. Read more on wikipedia

Example

const set1 = new Set([1, 2, 3])
const set2 = new Set([1, 2])

console.log(properSubset(set1, set1));
// false

console.log(subset(set1, set2));
// true

Difference

Signature A \ B

function difference<T> (a: Set<T>, b: Set<T>): Set<T>;

Calculate the set difference between set A and B. A set difference calculates a base set A with a set B removed. More formally this would be called the relative complement. Read more on wikipedia

alt text

Example

const set1 = new Set([1, 2, 3])
const set2 = new Set([3, 4, 5])

console.log(difference(set1, set2));
// Set<number>: Set([1, 2])

Symmetric Difference

Signature A ∆ B

function symmetricDifference<T>(...set<T>[]): Set<T>;

Calculate the symmetric difference between a collection of sets. A symmetric difference of two sets produces a set where elements are members of one set but not the other. The behavior is more complex for more sets. Read more on wikipedia

alt text

Example

const set1 = new Set([1, 2, 3])
const set2 = new Set([3, 4, 5])

console.log(symmetricDifference(set1, set2));
// Set<number>: Set([1, 2, 4, 5])