observable-multi-set

Configure observable multi set collections

npm install observable-multi-set
3 downloads in the last month

observable-multi-set

Configure observable multi set collections

It's about collection of values that are held in collection of sets. Sets can be added and removed during runtime, appropriate events propagation is assured.

Stands on native ES6 set collections

Check es6-set for appropriate shim for ES5 world

Usage

var MultiSet = require('observable-multi-set');

var set1 = new Set(['raz', 'dwa']), set2 = new Set(['trzy', 'raz']);
var mset = new MultiSet([set1, set2]);

mset.sets; // { set1, set2 }
mset;      // { 'raz', 'dwa', 'trzy' }

mset.on('change', function (event) {
  if (event.type === 'add') console.log("Added:", event.value);
  else if (event.type === 'delete') console.log("Deleted:", event.value);
  else if (event.type === 'clear') console.log("Set cleared");
  else if (event.type === 'batch') console.log("Batch change: Added: " + event.added ", Deleted: " + event.deleted);
});

set1.add('cztery'); // Added: trzy
set2.add('pięć');   // Added: pięć
set2.add('dwa');    // (ignored)
set1.delete('raz'); // (ignored)
set2.delete('raz'); // Deleted: 'raz'

mset.sets.add(new Set(['cztery', 'siedem', 'osiem'])); // Batch: Added: ['siedem, 'osiem' ]
mset.sets.delete(set2);                                // Batch: Deleted: ['trzy', 'pięć']

try {
  mset.add('other value'); // throws
} catch (e) {
  console.log("Multi set is read only");
}

Installation

NPM

In your project path:

$ npm install observable-multi-set
Browser

You can easily bundle observable-multi-set for browser with modules-webmake

Tests Build Status

$ npm test
npm loves you