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.
- Host: GitHub
- URL: https://github.com/fraxken/frequencyset
- Owner: fraxken
- License: mit
- Created: 2020-09-20T22:53:09.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-11-01T15:47:45.000Z (over 1 year ago)
- Last Synced: 2025-02-28T01:19:16.299Z (over 1 year ago)
- Topics: ecmascript, es6, frequencyset, set
- Language: JavaScript
- Homepage:
- Size: 220 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
FrequencySet
An ES6 Set compliant structure that keeps the frequency of occurrences.
## 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 ✨
[](#contributors-)
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
## License
MIT