jimmybyrum / dataset

Managing Deep Uniqueness

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dataset

Extension of Array that adds functions for ensuring unique values. Loosely based on mongo arrays: https://docs.mongodb.com/manual/reference/operator/update-array/

Why?

Set is a new data type in ES6, though it does not enforce object uniqueness:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

Install

npm install -S dataset.js

Usage

var DataSet = require('dataset.js');
var ds = new DataSet();

Examples

// assume this object for all examples below
var doc = {
  id: 123,
  name: 'jimmy',
  location: 'San Francisco, CA'
};

// iterates through all items in the set,
// does a deep compare, and adds if the doc
// is not already in the set.
ds.addToSet(doc);

// pulls the item from the set
ds.pull(doc);

// for each item in docs,
// call addToSet
ds.addEachToSet(docs);

// returns true if the exact (deeply compared) document is in the set.
ds.hasItem(doc);

// clear all items from the set.
ds.clear();

String Comparator

// iterates through the set and checks for any 
// existing documents with the same id. (does NOT deep compare)
ds.addToSet(doc, 'id');
ds.pull(doc, 'id');
ds.addEachToSet(docs, 'id');
ds.hasItem(doc, 'id');

Custom Function Comparator

var comparatorFunction = function(lhs, rhs) {
  return lhs.fancyKey === rhs.fancyKey;
};

// iterates through the set and passes each item
// through the comparator function
ds.addToSet(doc, comparatorFunction);
ds.addToSet(doc, comparatorFunction);
ds.pull(doc, comparatorFunction);
ds.addEachToSet(docs, comparatorFunction);
ds.hasItem(doc, comparatorFunction);

About

Managing Deep Uniqueness

License:MIT License


Languages

Language:JavaScript 100.0%