An open API service indexing awesome lists of open source software.

https://github.com/fraxken/frequencyset

A set that keeps the frequency of occurrences.
https://github.com/fraxken/frequencyset

ecmascript es6 frequencyset set

Last synced: about 1 year ago
JSON representation

A set that keeps the frequency of occurrences.

Awesome Lists containing this project

README

          


FrequencySet


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






maintenance


license


githubaction

## Requirements
- [Node.js](https://nodejs.org/en/) v20 or higher

## Getting Started

This package is available in the Node Package Repository and can be easily installed with [npm](https://docs.npmjs.com/getting-started/what-is-npm) or [yarn](https://yarnpkg.com).

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

## Usage example

```js
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](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/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.

```js
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.
```js
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.

```js
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](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):



Gentilhomme

💻 🐛 👀 📖 🛡️

## License
MIT