Giter Club home page Giter Club logo

ordered-set's Introduction

Ordered Set

by Nicholas C. Zakas

If you find this useful, please consider supporting my work with a donation.

Description

A JavaScript data structure that allows random access and traversal of a set's items. This is implemented as a doubly-linked list behinds the scenes.

Installation

You can install using npm or Yarn:

npm install @humanwhocodes/ordered-set --save

# or

yarn add @humanwhocodes/ordered-set

Usage

Retrieve the OrderedSet constructor and use the methods:

const { OrderedSet } = require("@humanwhocodes/ordered-set");

// create a new instance
const set = new OrderedSet();

// add in data
set.add(1);
set.add(2);
set.add(3);

// get the value before 2
const prev = set.previous(2);

// get the value after 1
const next = set.next(1);

// check if a value exists in the set
const exists = set.has(2);

// how many values are in the set?
const size = set.size;

// get the first value in the set
const first = set.first();

// get the last value in the set
const last = set.last();

// find the next value matching this function
const value1 = set.findNext(value => value > 1, 1);

// find the previous value matching this function
const value2 = set.findPrevious(value => value > 1, 3);

// remove a value
set.delete(1);

// get all values
let values1 = [...set.values()];
let values2 = [...set];

// get all values in reverse order
let values3 = [...set.reverse()];

License

Apache 2.0

ordered-set's People

Contributors

nzakas avatar dependabot[bot] avatar cschuller avatar

Watchers

James Cloos avatar

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.