notebook: handle duplicate mimetypes in list

This commit is contained in:
Connor Peet
2021-10-26 12:00:01 -07:00
parent 99b204f64e
commit 08409c807a
2 changed files with 10 additions and 3 deletions
@@ -652,11 +652,13 @@ export class MimeTypeDisplayOrder {
// Get the other mimetypes that are before the chosenMimetype. Then, move
// them after it, retaining order.
const otherIndices = otherMimetypes.map(m => this.findIndex(m, chosenIndex)).filter(i => i !== -1);
const uniqueIndicies = new Set(otherMimetypes.map(m => this.findIndex(m, chosenIndex)));
uniqueIndicies.delete(-1);
const otherIndices = Array.from(uniqueIndicies).sort();
this.order.splice(chosenIndex + 1, 0, ...otherIndices.map(i => this.order[i]));
for (const i of otherIndices.sort((a, b) => b - a)) {
this.order.splice(i, 1);
for (let oi = otherIndices.length - 1; oi >= 0; oi--) {
this.order.splice(otherIndices[oi], 1);
}
}
@@ -192,6 +192,11 @@ suite('NotebookCommon', () => {
// deletes multiple
m.prioritize('text/plain', ['text/plain', 'text/html', Mimes.markdown]);
assert.deepStrictEqual(m.toArray(), ['text/plain', 'text/html', Mimes.markdown, 'application/json']);
// handles multiple mimetypes, unknown mimetype
const m2 = new MimeTypeDisplayOrder(['a', 'b']);
m2.prioritize('b', ['a', 'b', 'a', 'q']);
assert.deepStrictEqual(m2.toArray(), ['b', 'a']);
});
test('sortMimeTypes glob', function () {