# @tangle/complex-set

> A strategy for tracking a set over time, when it matters when a certain thing was added to a Set, and when it was removed

Latest version **3.0.3** (published 2023-04-03) · LGPL-3.0-or-later license · 0 weekly downloads

## Install

```sh
npm install @tangle/complex-set
pnpm add @tangle/complex-set
yarn add @tangle/complex-set
bun add @tangle/complex-set
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.3 |
| Published | 2023-04-03 |
| First published | 2020-09-30 |
| Weekly downloads | 0 |
| License | LGPL-3.0-or-later |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 26.8 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Cherese Eriepa |
| Maintainers | mixmix, chereseeriepa, cpilbrow |
| Keywords | tangle, strategy, set |

## Links

- npm: https://www.npmjs.com/package/@tangle/complex-set
- Repository: https://gitlab.com/tangle-js/complex-set
- Homepage: https://gitlab.com/tangle-js/complex-set#readme
- Issues: https://gitlab.com/tangle-js/complex-set/issues
- npm.io page: https://npm.io/package/@tangle/complex-set

## Dependencies (6)

- [lodash.get](https://npm.io/package/lodash.get.md) ^4.4.2
- [lodash.isempty](https://npm.io/package/lodash.isempty.md) ^4.4.0
- [lodash.isequal](https://npm.io/package/lodash.isequal.md) ^4.5.0
- [lodash.setwith](https://npm.io/package/lodash.setwith.md) ^4.3.2
- [is-my-json-valid](https://npm.io/package/is-my-json-valid.md) ^2.20.6
- [lodash.mergewith](https://npm.io/package/lodash.mergewith.md) ^4.6.2

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 3.0.3 (latest) — 2023-04-03
- 3.0.2 — 2023-03-01
- 3.0.1 — 2021-12-06
- 3.0.0 — 2021-12-05
- 1.5.0 — 2021-11-15
- 1.4.0 — 2021-10-31
- 2.0.0 — 2021-01-26
- 1.3.2 — 2020-11-05
- 1.3.1 — 2020-11-05
- 1.3.0 — 2020-11-05
- 1.2.2 — 2020-11-03
- 1.2.1 — 2020-11-01
- 1.2.0 — 2020-11-01
- 1.1.1 — 2020-10-28
- 1.1.0 — 2020-10-28
- … 2 more at https://npm.io/package/@tangle/complex-set/versions

## README

# @tangle/complex-set

A strategy for tracking a set over time, when it matters when a certain thing was added to a Set, and when it was removed

## Example Usage

```js
const ComplexSet = require('@tangle/complex-set')

const input = {
  add: [
    { id: '@ben', seq: 25 },
    { id: '@cherese', seq: 2 }
  ]
}

strategy.mapFromInput(input, [strategy.identity()])
// => {
//   '@ben': { 25: 1 },
//   'cherese: { 2: 1 }
// }
```

The raw transformations look like this:
```js
{
  [id]: {
    [seq]: state
  }
}
```

where

* `id` *String* - the thing being added / removed from the set
* `seq` *Integer* - some Integer which is a unique representation of when. (e.g. logical clock or clock wall time). Must be >= 0
* `state` *Integer* - an Integer representing whether it was an add or remove.
  * `state > 0` - add
  * `state <= 0` - remove

However there are convenience methods for making it easier to work with these.

## API

### `ComplexSet(idPattern) => complexSet`

Instantiates a strategy, `complexSet`.

- `idPattern` *String* (optional)
    - a pattern which is converted into a regexp validating the `id` field
    - e.g. `'^@\w+$'` would make only ids like `@mixmix` valid
    - default: `'^.+$'`

### `complexSet.isValid(T) => Boolean`

### `complexSet.schema`

Access the JSON shcema that `isValid` was built with.

### `complexSet.concat(A, B) => C`

### `complexSet.identity() => I`

returns "identity transformation"


### `complexSet.mapFromInput(input, currentTips) => T`

Takes a current transformation state, `currentTips`, an array of Ts that are the
tips of the graph, along with a human friendly (descriptive) `input`
and returns a transformation `T` which satisfies the
change requested in `input.

Format of input:
```
{
  add: [{ id: Id, seq: Integer }, ... ]
  remove: [{ id: Id, seq: Integer }, ... ]
}
```
where:
- `id` is a unique identifier for something being added.
    - `Id` must pass the `idPattern` validator
- `seq` is a "sequence", some whole number which represents a position in time
    - this could be a vector clock, or
    - a wall clock (UTC time)

You can provide EITHER add / remove OR both.


### `complexSet.mapToOutput(T) => t`

Takes a transformation `T` and returns an output state `t`, which is more
"human readable"

Format of output `t`:

```
{
  Id: [Interval],
}
```
A series of `Interval`s is computed for each `Id` from all the add and remove
events, and these are ordered lowest to highest in terms of sequence.

Intervals will be of form:
- `{ start: Integer, end: Integer }` - a "closed" interval
- `{ start: Integer, end: null }`
    - an "open" interval
    - this will only ever occur as the last interval in a series






---

## An Example transformation

When users were added / removed as permitted authors for an ssb-record.

```js
// an example of a transformation
T = {
  // an example set where each @user was permitted at different intervals
  '@cherese': {
    200: 1, // seq=200, state=1=add
    1000: -1 // seq=1000, state=-1=remove
  },
  '@ben': {
    1000: 1, // seq=1000, state=1=add
    1100: 2, // seq=1100, state=2=add
    2000: -1 // seq=2000, state=-1=remove
  },
  '@mix': {
    300: -1 // seq=300, state=-1=remove
  }
}

// the reified state of that transformation
mapToOutput(T) = {
  '@cherese': [{ start: 200, end: 1000 }],
  '@ben': [{ start: 1000, end: 2000 }], // note how 1100 wasnt added
  // note @mix is not here
  // ids with no "add" states are ignored
}
```
### `simpleSet.isConflict() => False`
### `simpleSet.isValidMerge() => True`
### `simpleSet.merge(graph, mergeNode, field) => T`
where:
- `graph` is a `@tangle/graph` instance
- `mergeNode` is the proposed merge-node
- `field` *String* contains the the data fields `node.data[field]`

---
_Source: https://npm.io/package/@tangle/complex-set · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
