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

@cubux/readonly-set

v2.0.0

Published

Functions to work with read-only sets

Downloads

4

Readme

@cubux/readonly-set

NPM latest

A bunch of helper functions to work with read-only sets. Works internally with native sets without any kind of magic.

import { add, remove } from '@cubux/readonly-set';

const input: ReadonlySet<number> = new Set([10, 20, 30]);

console.log(add(input, 40));
// => Set(4) { 10, 20, 30, 40 }

console.log(remove(input, 20));
// => Set(2) { 10, 30 }

Alternative usage:

import * as RoSet from '@cubux/readonly-set';

const input: ReadonlySet<number> = new Set([10, 20, 30]);
console.log(RoSet.add(input, 40));
// => Set(4) { 10, 20, 30, 40 }

Use Cases

State management

import { FC, useState, ChangeEvent } from 'react';
import { toggle } from '@cubux/readonly-set';

const TodoList: FC = () => {
  const [checked, setChecked] = useState<ReadonlySet<string>>(() => new Set());

  const handleCheckItem = (key: string, e: ChangeEvent<HTMLInputElement>) =>
    setChecked(prev => toggle(prev, key, e.target.checked));

  ...
};

Install

npm i @cubux/readonly-set

API

add()

Add item to set

add(
  set: ReadonlySet<K>,
  key: K,
): ReadonlySet<K>

Creates new set from input set by adding item key.

  • Will return input set when key is already included.
const input: ReadonlySet<number> = new Set([10, 20, 30]);

add(input, 40);
// => Set(4) { 10, 20, 30, 40 }

See also: toggle(), union().

intersection()

Intersection of sets

intersection(
  set:    ReadonlySet<K>,
  ...and: ReadonlySet<K>[],
): ReadonlySet<K>

Creates new set containing only items which are presented in all given sets.

  • Will return input set when nothing to change (t.i. when set is a subset of every and sets).
  • May return earlier when intermediate result is already empty set.
const input: ReadonlySet<number> = new Set([10, 20, 30, 40]);

intersection(input, new Set([30, 42, 20]));
// => Set(2) { 20, 30 }

See also subtract(), union().

remove()

Remove item from set

remove(
  set: ReadonlySet<K>,
  key: K,
): ReadonlySet<K>

Creates new set from set without given item key.

  • Will return input set when key is not included in set.
const input: ReadonlySet<number> = new Set([10, 20, 30]);

console.log(remove(input, 20));
// => Set(2) { 10, 30 }

See also add(), intersection(), toggle().

subtract()

Subtract sets

subtract(
  set:    ReadonlySet<K>,
  ...sub: ReadonlySet<K>[]
): ReadonlySet<K>

Creates new set from set by subtracting every given sub set.

  • Will return input set when nothing to change (t.i. when set doesn't include any item of any sub sets).
  • May return earlier when intermediate result is already empty set.
  • Logic of subtract(a, b, c) is same as subtract(subtract(a, b), c) and is same as subtract(a, union(b, c)).
const input: ReadonlySet<number> = new Set([10, 20, 30, 40]);

subtract(input, new Set([30, 42, 20]));
// => Set(2) { 10, 40 }

See also intersection(), remove().

syncFrom()

syncFrom(
  prev: ReadonlySet<K>,
  next: ReadonlySet<K>,
): ReadonlySet<K>

Returns prev when its elements are equal to elements in next (order does not matter). Otherwise, returns next.

toggle()

Toggle an item in set

toggle(
  set:           ReadonlySet<K>,
  key:           K,
  toBeIncluded?: boolean,
): ReadonlySet<K>

Creates new set from input set by either adding or removing item key with respect to optional flag toBeIncluded.

  • If toBeIncluded is undefined (by default), it works as !set.has(key), t.i. key will be added if set doesn't include it, or removed otherwise. So, the result is always a new set in this case.
  • If toBeIncluded is defined, it causes toggle() to work as either add() when toBeIncluded is true, or as remove() otherwise. In this case toggle() may return input set when nothing to change.
const input: ReadonlySet<number> = new Set([10, 20, 30]);

console.log(toggle(input, 20));
console.log(toggle(input, 20, false));
// both => Set(2) { 10, 30 }

console.log(toggle(input, 40));
console.log(toggle(input, 40, true));
// both => Set(4) { 10, 20, 30, 40 }

console.log(toggle(input, 20, true));
console.log(toggle(input, 40, false));
// both => Set(3) { 10, 20, 30 }

See also add(), remove().

union()

Union of sets

union(
  set:    ReadonlySet<K>,
  ...add: ReadonlySet<K>[],
): ReadonlySet<K>

Creates new set which includes items from all the given sets.

  • Will return input set when nothing to change (t.i. when set is a superset of every sub sets).
const input: ReadonlySet<number> = new Set([10, 20, 30]);

union(input, new Set([30, 42, 20, 40]));
// => Set(5) { 10, 20, 30, 42, 40 }

See also add().