https://github.com/component/set
Set container
https://github.com/component/set
Last synced: 8 months ago
JSON representation
Set container
- Host: GitHub
- URL: https://github.com/component/set
- Owner: component
- Created: 2012-09-17T16:13:34.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2015-03-16T21:15:04.000Z (about 11 years ago)
- Last Synced: 2025-04-03T07:05:56.501Z (12 months ago)
- Language: JavaScript
- Size: 147 KB
- Stars: 17
- Watchers: 5
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
Awesome Lists containing this project
README
# set
Generic Set container
## Installation
```
$ component install component/set
```
## Example
```js
var Set = require('set');
var set = new Set;
set.add('foo');
set.add('foo');
set.add({ some: 'object' });
set.remove('foo');
set.values();
// => [{ some: 'object' }]
```
## Equality
Set supports an `.equals(other)` method when present,
for example you may then add two separate `User` instances
that are identified as being the same via their name:
```js
User.prototype.equals = function(user){
return this.name == user.name;
};
```
## API
### Set()
Create a new `Set`.
### Set(values)
Create a new `Set` with `values` array. Duplicates will be removed.
### Set#add(value)
Add `value` to the set.
### Set#remove(value)
Remove `value` from the set, returning __true__ when present,
otherwise returning __false__.
### Set#has(value)
Check if `value` is present.
### Set#values()
Return an array of values.
Aliased as `.toJSON()`.
### Set#each(fn)
Iterate each member and invoke `fn(val)`.
### Set#size()
Return the set size.
### Set#clear()
Empty the set and return the old values array.
### Set#isEmpty()
Check if the set is empty.
### Set#union(set)
Perform a union with `set` and return a new `Set`.
### Set#intersect(set)
Perform an intersection with `set` and return a new `Set`.
### Set#clone()
Clone the set and return a new `Set` of the same values.
## License
MIT