Skip to content
This repository has been archived by the owner on Sep 17, 2023. It is now read-only.
/ delta-set Public archive

JavaScript Set keeping delta of changes made to it

License

Notifications You must be signed in to change notification settings

proc7ts/delta-set

Repository files navigation

DeltaSet

NPM Build Status Code Quality Coverage GitHub Project API Documentation

JavaScript Set keeping delta of changes made to it

A DeltaSet class inherits ES2015 Set. In addition, it keeps changes made to it and has methods to deal with these changes' delta.

import { DeltaSet } from '@proc7ts/delta-set';

// Construct a delta set containing specified elements
// and record their addition.
const deltaSet = new DeltaSet([1, 2, 3]); // [1, 2, 3]

// Remove element and record its removal
deltaSet.delete(2); // [1, 3]

// Add element and record its addition
deltaSet.add(4); // [1, 3, 4]

const otherSet = new Set<number>();

// Replay changes in another set
deltaSet.redelta(otherSet); // otherSet: [1, 3, 4]

// Changes may be reported to receiver function
deltaSet.redelta((add, remove) => console.log('added:', ...add, '; removed:', ...remove));
// Logs: added: 1 3 4 ; removed: 2

// Forget about changes made to delta set
deltaSet.undelta();

// Apply more changes
deltaSet.delta(/* add */ [11, 12], /* remove */ [4]); // [1, 3, 11, 12]

// Replay last changes in another set
deltaSet.redelta(otherSet); // otherSet: [1, 3, 11, 12]

// Remove all elements and record their removal
deltaSet.clear();

deltaSet.redelta((add, remove) => console.log('added:', ...add, '; removed:', ...remove));
// Logs: added: ; removed: 4 1 3 11 12