frequency-set
v1.0.2
Published
A set that keeps the frequency of occurrences.
Downloads
10,963
Readme
Requirements
- Node.js v14 or higher
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 ✨
Thanks goes to these wonderful people (emoji key):
License
MIT