Extends darsain/list to create a unique list.
Read about limitations here: darsain/list#limitations.
With component(1):
component install darsain/uniquelist
var UniqueList = require('uniquelist');
var list = new UniqueList(['a', 'a', 'b', 'c', 'c']);
list.join(); // a,b,c
list.push('a');
list.join(); // a,b,c
list.unshift('d');
list.join(); // d,a,b,c,
With custom compare function:
var data = [
{ id: 'a' },
{ id: 'b' },
{ id: 'b' },
{ id: 'c' }
];
var compare = function compare(a, b) {
return a.id === b.id;
};
var list = new UniqueList(data, compare);
console.log(list); // { id: 'a' }, { id: 'b' }, { id: 'c' }
UniqueList constructor. new
keyword is optional.
Array, or an array-like like object to create a UniqueList from.
Can be Array
, List
, NodeList
, arguments
, ... everything that looks like { 0: 'foo', length: 1 }
.
If you want to have a UniqueList of objects and comparing should be property based, pass the custom compare function to the argument below.
Function for comparing 2 items. Receives 2 arguments. Has to return true
when items match, false
otherwise.
Example:
function compareFunction(a, b) {
return a.id === b.id;
}
Inherits all methods from List
Methods documented below are either new, or vary from their previous behavior.
Current compare function. Compares 2 items and returns true
when they match, false
otherwise.
By default uses strict equals ===
to compare items. You can specify your own compare function by passing it as a 2nd argument to constructor.
Uses #compare()
to find an item
in a list. Returns an item index, or -1
when not found.
Appends new unique items to a list, ignoring duplicates. Uses #compare()
for matching items.
Prepends new unique items to a list, ignoring duplicates. Uses #compare()
for matching items.
To run tests:
component install --dev
component build --dev
And open test/index.html
MIT