similars
v1.0.0
Published
Find similar objects and partial duplicates in collections
Downloads
11
Maintainers
Readme
similars
Find similar objects and partial duplicates in collections
Installation
$ npm install --save similars
Usage
const similars = require('similars');
const bobs = [
{ fn: 'BOB', ln: 'DYLAN', p: 'Musician' },
{ fn: 'B', ln: 'Marley', p: 'Singer' },
{ fn: 'Bob', ln: 'Dylan', p: 'SINGER' },
{ fn: 'Bobby', ln: 'Dylan', p: 'MUSICIAN' },
{ fn: 'BOB', ln: 'MARLEY', p: 'MUSICIAN' }
];
// Find Bobs with the same last name (ln) and profession (p)
similars(
bobs,
(a, b) =>
a.ln.toLowerCase() === b.ln.toLowerCase() &&
a.p.toLowerCase() === b.p.toLowerCase()
).then(found => {
console.log(found);
/*
[
{ fn: 'BOB', ln: 'DYLAN', p: 'Musician' },
{ fn: 'Bobby', ln: 'Dylan', p: 'MUSICIAN' }
]
*/
});
API
similars(collection, matcher[, onEach])
Returns a Promise
, however, processing is synchronous
collection
Required
:Array
containing objects
matcher
Required
:Function
that compares each item in the collection and returns aBoolean
- e.g.
(a, b) => a.name.toLowerCase() === b.name.toLowerCase()
onEach
Optional
:Function
that executes once for each item in thecollection
- Is good for showing progress on long running executions
License
ISC © Buster Collings