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

@creanet/records-set

v1.0.2

Published

Set of records with one or more unique properties

Downloads

3

Readme

Records Set

Class for storing a set of records with unique property.

Installation

To install this module use npm or yarn:

npm install @creanet/records-set
yarn add @creanet/records-set

After the module is installed, require it anywhere in the code:

const RecordsSet = require('@creanet/records-set');

Creating new instance

To create a new instance of Records Set, constructor should be called with two parameters:

const RecordsSet = require('@creanet/records-set');

const uniqueProp = 'id';
const iterable = [
  {
    id: '1',
    foo: 'obj1'
  },
  {
    id: '2',
    foo: 'obj2'
  },
  {
    id: '2',
    foo: 'obj3'
  }
];

const set = new RecordsSet(uniqueProp, iterable);

console.log(set.values()); // [ { id: '1', foo: 'obj1' }, { id: '2', foo: 'obj2' } ]

Properties

size

Returns the number of the records in RecordsSet:

console.log(set.size); // 2
console.log(set.size === set.values().length); // true

Methods

values()

Returns an array with all records (values):

console.log(set.values()); // [ { id: '1', foo: 'obj1' }, { id: '2', foo: 'obj2' } ]

has(record)

Determines whether set contains such record by looking for unique property. All other properties will be ignored:

console.log(set.has({ id: '2', foo: 'obj2' })); // true
console.log(set.has({ id: '2', foo: 'obj3' })); // true
console.log(set.has({ id: '3', foo: 'obj4' })); // false

add(values)

Used to add one or more records. Records with already registered unique prop will be ignored:

set.add({ id: '2', foo: 'obj5' });
console.log(set.size); // 2

set.add({ id: '3', foo: 'obj6' });
console.log(set.size); // 3

delete(values)

Used to delete one or more records. Delete can either be passed a record with the unique property:

set.delete({ id: '2', foo: 'obj7' });
console.log(set.values()); // [ { id: '1', foo: 'obj1' }, { id: '3', foo: 'obj6' } ]

Or a simple value:

set.delete('1');
console.log(set.values()); // [ { id: '3', foo: 'obj6' } ]

clear()

Removes all records:

set.clear();
console.log(set.values()); // []

Iteration methods

Records set can be iterated using these methods:

set.forEach(callback, thisArg);
set.map(callback, thisArg);
set.filter(callback, thisArg);

Methods map and filter return a new instance of RecordsSet, forEach returns the original RecordsSet. Alternatively it is possible to use any array iteration method by calling it on the values of the set:

set.values()[iterationMethod];

Some array iteration methods return an array. To turn the result into set, simply call:

set = new Set(set.uniqueProp, set.values().map(callback));