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

reverse-iterable-set

v5.0.0

Published

A reverse-iterable set implementation based on the built-in Set object

Downloads

7

Readme

reverse-iterable-set

The ReverseIterableSet object is a reverse-iterable set implementation based on the built-in Set object.

Links:

See also:

Table of Contents

Installation

npm install reverse-iterable-set

Usage

import ReverseIterableSet from 'reverse-iterable-set';

const set = new ReverseIterableSet();

Examples

For some live usage examples, clone the repository and run the following:

npm install
npm start

Then, open localhost:8080/examples in a browser.

Tests

In order to run the tests, clone the repository and run the following:

npm install
npm test

Documentation

A ReverseIterableSet object iterates its elements in insertion or reverse-insertion order — a for...of loop returns the values for each iteration.

Constructor

Syntax

new ReverseIterableSet([iterable])

Parameters:

Usage

const set = new ReverseIterableSet([1, 2, 3]);

for (const value of set.reverseIterator()) {
  console.log(value);
}

[...set.reverseIterator()]

size

The size accessor property returns the number of values in a ReverseIterableSet object.

Syntax

set.size

Usage

const set = new ReverseIterableSet(['a', 'b', 'c']);

set.size
//> 3

[Symbol.toStringTag]

The ReverseIterableSet[@@toStringTag] property has an initial value of “ReverseIterableSet”.

add()

Syntax

set.add(value);

Parameters:

  • value: Required. The value to add to the ReverseIterableSet object.

Return value:

  • The ReverseIterableSet object.

Usage

const set = new ReverseIterableSet();

set.add('hey');
//> ReverseIterableSet [ "hey" ]

set.add('beauty');
//> ReverseIterableSet [ "hey", "beauty" ]

set.add('hey');
//> ReverseIterableSet [ "hey", "beauty" ]

The add() method returns a reference to the set object. This makes the add() operation chainable.

const set = new ReverseIterableSet()
  .add('key … is spelled like tea')
  .add('hey … somehow ney');
//> ReverseIterableSet [ "key … is spelled like tea", "hey … somehow ney" ]

clear()

Syntax

set.clear();

Return value:

undefined.

Usage

// Clears the underlying map object (yes, this is correct)
// Sets the first and last node references to null
set.clear();
//> undefined

delete()

Syntax

set.delete(value);

Parameters:

  • value: Required. The value to remove from the ReverseIterableSet object.

Return value:

  • Boolean: Returns true if the value existed in the ReverseIterableSet object and has been removed, or false if the value did not exist.

Usage

const set = new ReverseIterableSet(['1', '2']);

set.delete('1');
//> true

set.delete('2');
//> true

set.delete('2');
//> false

entries()

Returns an iterator containing the [value, value] pairs for each value in the ReverseIterableSet object in insertion order.

An iterator containing the same pairs in reverse-insertion order can be obtained with entries().reverseIterator().

Syntax

set.entries();

Return value:

A new ReverseIterableSet iterator object.

Usage

const set = new ReverseIterableSet([1, 2, 4]);

const iterator = set.entries();

iterator.next().value;
//> [1, 1]

iterator.next().value;
//> [2, 2]

iterator.next().value;
//> [4, 4]

iterator.next().value;
//> undefined

forEach()

The forEach() method executes a provided function once for each value in the ReverseIterableSet object, in insertion order.

Syntax

set.forEach(callback[, thisArg]);

Parameters:

  • callback: Function to execute for each element.
  • thisArg: Value to use as this when executing callback.

Return value:

undefined.

Usage

const set = new ReverseIterableSet(['a', 'b', 'c']);

set.forEach(value => {
  console.log(value);
});
//> a
//> b
//> c

set.forEach(function (value1, value2, setReference) {
  console.log(value1, value2, setReference.size);
});
//> a a 3
//> b b 3
//> c c 3

forEachReverse()

The forEachReverse() method executes a provided function for each value in the ReverseIterableSet object, in reverse-insertion order.

Syntax

set.forEachReverse(callback[, thisArg]);

Parameters:

  • callback: Function to execute for each element.
  • thisArg: Value to use as this when executing callback.

Return value:

undefined.

Usage

const set = new ReverseIterableSet(['a', 'b', 'c']);

set.forEachReverse(value => {
  console.log(value);
});
//> c
//> b
//> a

set.forEachReverse(function (value1, value2, setReference) {
  console.log(value1, value2, setReference.size);
});
//> c c 3
//> b b 3
//> a a 3

has()

Syntax

set.has(value);

Parameters:

  • value: Required. The value to test for presence in the ReverseIterableSet object.

Return value:

  • Boolean: Returns true if the value exists in the ReverseIterableSet object; otherwise false.

Usage

const set = new ReverseIterableSet(['hey', 'beauty']);

set.has('hey');
//> true

set.has('beauty');
//> true

set.has('beast');
//> false

iteratorFor()

Returns an iterator containing the values in the ReverseIterableSet object in insertion order starting with the value specified by the value parameter.

This allows starting iteration at a specific value in the ReverseIterableSet object.

An iterator containing the same values in reverse-insertion order can be obtained with iteratorFor().reverseIterator().

Syntax

set.iteratorFor(value);

Parameters:

  • value: Required. The value to start iterating from.

Return value:

A new ReverseIterableSet iterator object.

Usage

const set = new ReverseIterableSet([1, 2, 4]);

// Iterator, starting at the element with key 1.
const iterator = set.iteratorFor(2);

iterator.next().value;
//> 2

iterator.next().value;
//> 4

iterator.next().value;
//> undefined

// Reverse-iterator, starting at the element with key 1.
const reverseIterator = set.iteratorFor(2).reverseIterator();

reverseIterator.next().value;
//> 2

reverseIterator.next().value;
//> 1

reverseIterator.next().value;
//> undefined

reverseIterator()

In theory, following the semantics of [Symbol.iterator](), this should be [Symbol.reverseIterator](). However, as a developer, I cannot define a well-known symbol myself and make use of it. For the time being, the reverseIterator() function serves the same purpose.

Syntax

set.reverseIterator();

Return value:

The set reverse-iterator function, which is the values().reverseIterator() function by default.

Usage

const set = new ReverseIterableSet([1, 2, 4]);

const iterator = set.reverseIterator();

iterator.next().value;
//> 4

iterator.next().value;
//> 2

iterator.next().value;
//> 1

iterator.next().value;
//> undefined

values()

Returns an iterator containing the values in the ReverseIterableSet object in insertion order.

An iterator containing the same values in reverse-insertion order can be obtained with values().reverseIterator().

Syntax

set.values();

Return value:

A new ReverseIterableSet iterator object.

Usage

const set = new ReverseIterableSet([1, 2, 4]);

const iterator = set.values();

iterator.next().value;
//> 1

iterator.next().value;
//> 2

iterator.next().value;
//> 4

iterator.next().value;
//> undefined

[Symbol.iterator]()

Returns the set iterator function. By default, this is the values() function.

Syntax

set[Symbol.iterator]();

Return value:

The set iterator function, which is the values() function by default.

Usage

const set = new ReverseIterableSet([1, 2, 4]);

const iterator = set[Symbol.iterator]();

iterator.next().value;
//> 1

iterator.next().value;
//> 2

iterator.next().value;
//> 4

iterator.next().value;
//> undefined