fraxken / FrequencySet

A set that keeps the frequency of occurrences.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FrequencySet

An ES6 Set compliant structure that keeps the frequency of occurrences.

maintenance license githubaction

Requirements

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i frequency-set
# or
$ yarn add frequency-set

Usage example

const FrequencySet = require("frequency-set");

const MySet = new FrequencySet(["foo", "bar"]);
MySet.add("foo");
MySet.add("foo");
MySet.add("bar");

console.log([...MySet.entries()]); // [["foo", 3], ["bar", 2]]

const clone = new FrequencySet(MySet);
console.log(clone);

API

FrequencySet implements exactly the same interfaces as an ES6 Set. Except for the @@ Iteration Symbol and the entries(). Instead of returning the unique value as key and value, FrequencySet return the unique value as key and the count as value.

const mySet = new FrequencySet(["foo", "foo", "bar"]);

for (const [uniqueValue, count] of mySet) {
    console.log([uniqueValue, count]); // [foo, 2] and [bar, 1]
}

Also the add method has been extended with a additional count argument witch take a number.

const mySet = new FrequencySet().add("foo", 10);

console.log(mySet.toJSON()); // ["foo", 10]

toJSON()

FrequencySet implement a custom toJSON() method which will allow an automatic transformation into JSON.

const mySet = new FrequencySet(["foo", "foo", "bar"]);

console.log(mySet.toJSON()); // [foo, 2] and [bar, 1];

The toJSON method does not take into account functions and objects.

Contributors ✨

All Contributors

Thanks goes to these wonderful people (emoji key):


Gentilhomme

πŸ’» πŸ› πŸ‘€ πŸ“– πŸ›‘οΈ

License

MIT

About

A set that keeps the frequency of occurrences.

License:MIT License


Languages

Language:JavaScript 100.0%