New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ts-set-utils

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-set-utils

A collection of Set utilities similar to python's

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
125
increased by303.23%
Maintainers
1
Weekly downloads
 
Created
Source

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])

Keywords

FAQs

Package last updated on 03 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc