diff --git a/src/sort.ts b/src/sort.ts index 0b239ddd49..7625937ca5 100644 --- a/src/sort.ts +++ b/src/sort.ts @@ -81,7 +81,7 @@ function isReadonlyArray(value: any): value is readonly T[] { /** @internal */ function pairToMap(v: readonly [string, SortDirection]): SortForCmd { - return new Map([[`${v[0]}`, prepareDirection([v[1]])]]); + return new Map([[`${v[0]}`, prepareDirection(v[1])]]); } /** @internal */ diff --git a/test/unit/sort.test.ts b/test/unit/sort.test.ts new file mode 100644 index 0000000000..19669feff3 --- /dev/null +++ b/test/unit/sort.test.ts @@ -0,0 +1,21 @@ +import { expect } from 'chai'; + +import { formatSort } from '../mongodb'; + +describe('formatSort', function () { + context('when the sort is a [field, direction] pair', function () { + it('formats a numeric direction', function () { + expect(formatSort(['alpha', -1])).to.deep.equal(new Map([['alpha', -1]])); + }); + + it('formats a string direction', function () { + expect(formatSort(['alpha', 'asc'])).to.deep.equal(new Map([['alpha', 1]])); + }); + + it('preserves a $meta direction', function () { + expect(formatSort(['alpha', { $meta: 'textScore' }])).to.deep.equal( + new Map([['alpha', { $meta: 'textScore' }]]) + ); + }); + }); +});