Commit 8aadd912 authored by Erik Hetzner's avatar Erik Hetzner

Add dedupItems function

parent 4345a569
......@@ -94,16 +94,7 @@ function runSearch(s) {
return item;
}
});
let seenIds = new Set([]); // To uniqify results
return Zotero.Promise.filter(items, (item) => {
if (seenIds.has(item.id)) {
return false;
} else {
seenIds.add(item.id);
return true;
}
});
return dedupItems(items, Zotero.Promise.Filter);
});
}
......
var EXPORTED_SYMBOLS = [ 'parseEasyKey', 'fixStyleId', 'cleanQuery' ];
var EXPORTED_SYMBOLS = [ 'parseEasyKey', 'fixStyleId', 'cleanQuery', 'dedupItems' ];
/**
* Parses an easy key. Returns {creator: ..., title: ..., date: ...} or null if it
......@@ -41,9 +41,23 @@ function cleanQuery(q) {
return retval;
}
/* Given a iterable of promises that return an item, return a deduped iterable
* of promises (based on the id). */
function dedupItems(items, filter) {
let seenIds = new Set([]); // To uniqify results
return filter(items, (item) => {
if (seenIds.has(item.id)) {
return false;
} else {
seenIds.add(item.id);
return true;
}
});
};
if (process) {
module.exports.fixStyleId = fixStyleId;
module.exports.parseEasyKey = parseEasyKey;
module.exports.cleanQuery = cleanQuery;
module.exports.dedupItems = dedupItems;
}
......@@ -3,6 +3,7 @@
"test": "mocha"
},
"dependencies": {
"bluebird": "^3.5.1",
"mocha": "^4.0.1",
"sinon": "^4.0.2",
"xregexp": "^3.2.0"
......
......@@ -3,6 +3,7 @@
const assert = require('assert');
const sinon = require('sinon');
const xregexp = require('xregexp');
const bluebird = require('bluebird');
const core = require('../extension/content/modules/Core.jsm');
......@@ -49,3 +50,32 @@ describe('#core.cleanQuery()', () => {
assert.deepEqual({foo: 'foo bar'}, core.cleanQuery({foo: 'foo+bar'}));
});
});
describe('#core.dedupItems()', () => {
const item1 = {id: 1};
const item2 = {id: 2};
const item3 = {id: 3};
const mkPromises = (...rest)=>{ return rest.map(bluebird.resolve); };
it('should return an Promise that resolves to an iterable', ()=>{
return core.dedupItems(mkPromises(item1), bluebird.filter).then((items)=>{
assert.equal(1, items.length);
assert.deepEqual(item1, items[0]);
});
});
it('should dedup with the same id', ()=>{
return core.dedupItems(mkPromises(item1, item1), bluebird.filter).then((items)=>{
assert.equal(1, items.length);
assert.deepEqual(item1, items[0]);
});
});
it('should dedup and return in order', ()=>{
return core.dedupItems(mkPromises(item1, item2, item1, item3, item1), bluebird.filter).then((items)=>{
assert.equal(3, items.length);
assert.deepEqual(item1, items[0]);
assert.deepEqual(item2, items[1]);
assert.deepEqual(item3, items[2]);
});
});
});
......@@ -6,6 +6,10 @@ balanced-match@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
bluebird@^3.5.1:
version "3.5.1"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.1.tgz#d9551f9de98f1fcda1e683d17ee91a0602ee2eb9"
brace-expansion@^1.1.7:
version "1.1.8"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.8.tgz#c07b211c7c952ec1f8efd51a77ef0d1d3990a292"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment