Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mgwalker/counting-set
A simple counting set
https://github.com/mgwalker/counting-set
Last synced: about 2 months ago
JSON representation
A simple counting set
- Host: GitHub
- URL: https://github.com/mgwalker/counting-set
- Owner: mgwalker
- Created: 2022-01-05T03:10:27.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-05T03:14:30.000Z (about 3 years ago)
- Last Synced: 2024-11-18T08:59:27.033Z (2 months ago)
- Language: JavaScript
- Size: 89.8 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CountingSet
A data structure for counting the occurrances of a thing. This is essentially a
wrapper around [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map),
and follows the same equality checking rules.### Usage
```sh
npm install counting-set
``````javascript
import CountingSet from "counting-set";const letters = new CountingSet();
for (const letter of "hadjetesdnsfgkfghkfgkdffgasdfa") {
letters.add(letter);
}console.log(letters);
```**Outputs:**
```
CountingSet(11) [Map] {
'h' => 2,
'a' => 3,
'd' => 4,
'j' => 1,
'e' => 2,
't' => 1,
's' => 3,
'n' => 1,
'f' => 6,
'g' => 4,
'k' => 3
}
```### API
#### `CountingSet.add(value);`
Add another instance of `value` to the count. If the item wasn't previously in
the counting set, it is added and the count is set to one.#### `CountingSet.count(value)`
Returns the current count for `value`, or zero if the value is not in the set.
#### `CountingSet.remove(value)`
Removes a single instance of `value` from the set. If the value does not
currently exist, it gets added to the set with a count of zero. If the count
for the value is already zero, nothing happens.---
All other `Map` functions are also present, except for `set()`, which will
throw a `TypeError`. CountingSet does not permit setting values for keys
directly.