FrequencySet
An ES6 Set compliant structure that keeps the frequency of occurrences.
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
$ 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()]);
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]);
}
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());
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());
The toJSON method does not take into account functions and objects.
Contributors ✨
Thanks goes to these wonderful people (emoji key):
License
MIT