https://github.com/capaj/hashset.js
simple hashset implementation aimed to working with DB entries on frontend. If it has primary keys, storing it in a hashset is probably a good idea.
https://github.com/capaj/hashset.js
Last synced: 12 months ago
JSON representation
simple hashset implementation aimed to working with DB entries on frontend. If it has primary keys, storing it in a hashset is probably a good idea.
- Host: GitHub
- URL: https://github.com/capaj/hashset.js
- Owner: capaj
- License: mit
- Created: 2015-07-06T12:56:20.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-07-13T10:39:53.000Z (almost 11 years ago)
- Last Synced: 2025-07-09T13:08:04.202Z (12 months ago)
- Language: JavaScript
- Size: 133 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hashset.js [](https://travis-ci.org/capaj/hashset.js)
simple hashset implementation aimed to working with DB entries on frontend. If it has primary keys, storing it in a hashset is probably a good idea.
## Installation
```
jspm i npm:hashset.js
npm i hashset.js
```
## Why not es6 Set?
Because it is not suited for working with DB objects, such as objects returned from mongoDB where primary keys are '_id' properties. It is not possible to tell the native set that two objects with the same _id are the same.
## API
With the API I tried to shadow ES6 Set, so it should feel almost as if you were working with the native Set implementation.
### Hashset
```
var hashFunction = '_id';
//or your hashFunction can be an actual function
var hashFunction = function(item){return item.a+item.b};
new Hashset(hashFunction) //returns new instance of a Hashset
```
### Methods
```
add(value) returns {boolean}
addArray(arr) returns {Number}
clear()
delete(valueOrKey) returns {boolean}
each(iteratorFunction, thisObjopt)
filter() returns {Hashset}
getValue(hash) returns {*}
has(valueOrKey) returns {boolean}
size() returns {number}
toArray() returns {Array}
upsert(value) returns {boolean} //requires Object.assign
upsertArray(arr) returns {Number}
```
If you want upserts in the ES5 environment, [polyfill Object.assign()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)