GithubHelp home page GithubHelp logo

set-fns's Introduction

set-fns

A utility library for working with sets in JavaScript.

npm bundle size npm

Features

  • Pure functions that support immutability.
  • Flow and TypeScript declarations included.
  • CommonJS, UMD and ESM modules provided.
  • Zero dependencies.

Installation

Yarn:

yarn add set-fns

NPM:

npm install set-fns

Usage

import { set, union, intersection, difference, subset } from "set-fns";

const alphabet = set("abcdefghijklmnopqrstuvwxyz".split(""));

const sentence = set("the quick brown fox jumped over the lazy dog!".split(""));

console.log("All characters", union(alphabet, sentence));

console.log(
  "Characters from the alphabet used in the sentence",
  intersection(alphabet, sentence)
);

if (!subset(sentence, alphabet)) {
  console.log(
    "Extra character(s) found in sentence!",
    difference(sentence, alphabet)
  );
}

API

set

declare const set: <T>(x?: Iterable<T>) => Set<T>;

Takes an iterable and returns a set of all items in that iterable. If the iterable is already a set it is returned unchanged.

const a = set([1, 2, 3]); // Set { 1, 2, 3 }
const b = set(a); // Set { 1, 2, 3 }
const c = set([1, 2, 3]); // Set { 1, 2, 3 }
a === b; // true
a === c; // false

and / intersection

declare const and: <T>(a: Iterable<T>, b: Iterable<T>) => Set<T>;

Takes two iterables and returns a set of all items that appear in both.

const a = [1, 2, 3, 4];
const b = [6, 5, 4, 3];
and(a, b); // Set { 3, 4 }
intersection(a, b); // Set { 3, 4 }

or / union

declare const or: <T>(a: Iterable<T>, b: Iterable<T>) => Set<T>;

Takes two iterables and returns a set of all items that appear in either.

const a = [1, 2, 3, 4];
const b = [6, 5, 4, 3];
or(a, b); // Set { 1, 2, 3, 4, 5, 6 }
union(a, b); // Set { 1, 2, 3, 4, 5, 6 }

not / subtract / difference

declare const not: <T>(a: Iterable<T>, b: Iterable<T>) => Set<T>;

Takes two iterables and returns a set of all items that appear the first, but not the second.

const a = [1, 2, 3, 4];
const b = [6, 5, 4, 3];
not(a, b); // Set { 1, 2 }
subtract(a, b); // Set { 1, 2 }
difference(a, b); // Set { 1, 2 }

xor

declare const xor: <T>(a: Iterable<T>, b: Iterable<T>) => Set<T>;

Takes two iterables and returns a set of all items that appear exclusively in the first or the second (do not appear in both iterables).

const a = [1, 2, 3, 4];
const b = [6, 5, 4, 3];
xor(a, b); // Set { 1, 2, 5, 6 }

equal

declare const equal: (a: Iterable<any>, b: Iterable<any>) => boolean;

Takes two iterables and returns true if both contain the exactly the same items.

const a = [1, 2, 3];
const b = [3, 2, 1];
const c = [3, 3, 2, 2, 1, 1];
equal(a, b); // true
equal(a, c); // true

subset

declare const subset: (a: Iterable<any>, b: Iterable<any>) => boolean;

Takes two iterables and returns true if the first is a subset of the second.

const a = [1, 2];
const b = [1, 2, 3];
const c = [1, 2];
subset(a, b); // true
subset(a, c); // true

strictSubset

declare const strictSubset: (a: Iterable<any>, b: Iterable<any>) => boolean;

Takes two iterables and returns true if the first is a strict subset of the second.

const a = [1, 2];
const b = [1, 2, 3];
const c = [1, 2];
strictSubset(a, b); // true
strictSubset(a, c); // false

superset

declare const superset: (a: Iterable<any>, b: Iterable<any>) => boolean;

Takes two iterables and returns true if the first is a superset of the second.

const a = [1, 2, 3];
const b = [1, 2];
const c = [1, 2, 3];
superset(a, b); // true
superset(a, c); // true

strictSuperset

declare const strictSuperset: (a: Iterable<any>, b: Iterable<any>) => boolean;

Takes two iterables and returns true if the first is a strict superset of the second.

const a = [1, 2, 3];
const b = [1, 2];
const c = [1, 2, 3];
strictSuperset(a, b); // true
strictSuperset(a, c); // false

intersects

declare const intersects: (a: Iterable<any>, b: Iterable<any>) => boolean;

Takes two iterables and returns true if the two intersect.

const a = [1, 2];
const b = [2, 3];
const c = [3, 4];
intersects(a, b); // true
intersects(a, c); // false

set-fns's People

Contributors

haydn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

set-fns's Issues

Missing flow types

Type for intersection, union, subtract and difference are missing from index.js.flow.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.