Skip to content

Commit

Permalink
📝 Add docs for Set extensions, resolves #17
Browse files Browse the repository at this point in the history
  • Loading branch information
motss committed Jul 20, 2019
1 parent 173006e commit 32a3464
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 5 deletions.
183 changes: 183 additions & 0 deletions src/set/API_REFERENCE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# API Reference

## Set

### Constructor

#### from(setEntries[, mapFn])

Creates a `Set` object that contains all key-value pairs of `setEntries` and an optional `mapFn` to call on every element of `setEntries`.

```ts
const entries = [1, 2];
const s = Set.from(entries, n => 2 + n);

s.has(3) === true;
s.has(4) === true;
```

#### isSet(x)

Returns `true` if `x` is a `Set` object.

```ts
Set.isSet(new Set()) === true;
```

#### of(element0[, element1[, ...[, elementN]]])

Creates a new `Set` object from a variable number of arguments, regardless of the number of type of arguments.

```ts
const a = {};
const b = {};
const s = Set.of(1, 2, a, b);

s.has(1) == true;
s.has(2) === true;
s.has(a) === true;
s.has(b) === true;
```

### Prototype

#### difference(other)

Visits the values representing the difference, i.e., the values that are in this `Set` but not in `other`.

```ts
const a = new Set([1, 2, 3]);
const b = new Set([4, 2, 3, 5]);

a.difference(b); /** [1] */
b.difference(a); /** [4, 5] */
```

#### intersection(other)

Visits the values representing the intersection, i.e., the values that are both in this `Set` and `other`.

```ts
const a = new Set([1, 2, 3]);
const b = new Set([4, 2, 3, 5]);

a.intersection(b); /** [2, 3] */
```

#### isDisjoint(other)

Returns `true` if this `Set` has no elements in common with `other`. This is equivalent to checking for an empty intersection.

```ts
const a = new Set([1, 2, 3]);
const b = new Set();

a.isDisjoint(b) === true;

b.add(4);
a.isDisjoint(b) === true;

a.add(1);
a.isDisjoint(b) === false;
```

#### isEmpty()

Returns `true` if the set contains no elements.

```ts
const s = new Set();

s.isEmpty() === true;
```

#### isSubset(other)

Returns `true` if the set is a subset of another, i.e., `other` contains at least all the values in this Set.

```ts
const a = new Set([1, 2, 3]);
const b = new Set();

b.isSubset(a) === true;

b.add(2);
b.isSubset(a) === true;

b.add(4);
b.isSubset(a) === false;
```

#### isSuperset(other)

Returns `true` if the set is a superset of another, i.e., this `Set` contains at least all the values in `other`.

```ts
const a = new Set([1, 2]);
const b = new Set();

b.isSuperset(a) === false;

b.add(0);
b.add(1);
b.isSuperset(a) === false;

b.add(2);
b.isSuperset(a) === true;
```

#### iter()

Returns an iterator visiting all elements in the set.

```ts
const s = new Set([1, 2, 3]);
const iter = s.iter();

iter.next().value === 1;
iter.next().value === 2;
iter.next().value === 3;
iter.next().done === true;
```

#### len()

Returns the number of elements in the set.

```ts
const s = new Set([1, 2, 3]);

s.len() === 3;
```

#### symmetricDifference(other)

Visits the values representing the symmetric difference, i.e., the values that are in this `Set` or in `other` but not in both.

```ts
const a = new Set([1, 2, 3]);
const b = new Set([4, 2, 3, 5]);

a.symmetricDifference(b); /** [1, 4, 5] */
```

#### toArray()

Creates an array containing all the elements of this `Set`.

```ts
const s = new Set([1, 2, 3]);

s.toArray(); /** [1, 2, 3] */
```

#### union(other)

Visits the values representing the union, i.e., all the values in this `Set` or `other`, without duplicates.

```ts
const a = new Set([1, 2, 3]);
const b = new Set([4, 2, 3, 5]);

a.union(b); /** [1, 2, 3, 4, 5] */
```
40 changes: 35 additions & 5 deletions src/set/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,56 @@

[![MIT License][mit-license-badge]][mit-license-url]

>
> Set extensions
## Table of contents <!-- omit in toc -->

- [Usage](#Usage)
- [API Reference](#API-Reference)
- [License](#License)

## Usage
## Available extensions

```ts
```
### Constructor

## API Reference
* [from(setEntries\[, mapFn\])]
* [isSet(x)]
* [of(element0\[, element1\[, ...\[, elementN\]\]\])]

### Prototype

* [difference(other)]
* [intersection(other)]
* [isDisjoint(other)]
* [isEmpty()]
* [isSubset(other)]
* [isSuperset(other)]
* [iter()]
* [len()]
* [symmetricDifference(other)]
* [toArray()]
* [union(other)]

## License

[MIT License](http://motss.mit-license.org/) © Rong Sen Ng

<!-- References -->
[from(setEntries\[, mapFn\])]: /src/set/API_REFERENCE.md#fromsetentries-mapfn
[isSet(x)]: /src/set/API_REFERENCE.md#issetx
[of(element0\[, element1\[, ...\[, elementN\]\]\])]: /src/set/API_REFERENCE.md#ofelement0-element1--elementn

[difference(other)]: /src/set/API_REFERENCE.md#differenceother
[intersection(other)]: /src/set/API_REFERENCE.md#intersectionother
[isDisjoint(other)]: /src/set/API_REFERENCE.md#isdisjointother
[isEmpty()]: /src/set/API_REFERENCE.md#isemptyother
[isSubset(other)]: /src/set/API_REFERENCE.md#issubsetother
[isSuperset(other)]: /src/set/API_REFERENCE.md#issupersetother
[iter()]: /src/set/API_REFERENCE.md#iter
[len()]: /src/set/API_REFERENCE.md#len
[symmetricDifference(other)]: /src/set/API_REFERENCE.md#symmetricdifferenceother
[toArray()]: /src/set/API_REFERENCE.md#toarray
[union(other)]: /src/set/API_REFERENCE.md#unionother

<!-- MDN -->
[array-mdn-url]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
Expand Down

0 comments on commit 32a3464

Please sign in to comment.