UNPKG

ts-prime

Version:

A utility library for JavaScript and Typescript.

62 lines (61 loc) 2.09 kB
import { sortBy } from './sortBy'; import { pipe } from './pipe'; var items = [{ a: 1 }, { a: 3 }, { a: 7 }, { a: 2 }]; var sorted = [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 7 }]; var multiPropSortItems = [ { favorite: false, category: 'A' }, { favorite: true, category: 'C' }, { favorite: true, category: 'B' }, { favorite: false, category: 'B' }, ]; // Expect favorites sorted alphabetically var multiPropSortItemsSorted = [ { favorite: true, category: 'B' }, { favorite: true, category: 'C' }, { favorite: false, category: 'A' }, { favorite: false, category: 'B' }, ]; var withLocaleComparison = [ { category: 'Garden' }, { category: 'cleaning' }, { category: 'books' }, { category: 'books' }, { category: 'Tech' }, { category: 'Cooking' }, ]; var sortedWithLocaleComparison = [ { category: 'books' }, { category: 'books' }, { category: 'cleaning' }, { category: 'Cooking' }, { category: 'Garden' }, { category: 'Tech' }, ]; describe('data first', function () { test('sort correctly', function () { expect(sortBy(items, function (x) { return x.a; })).toEqual(sorted); }); test('sort with multi keys', function () { expect(sortBy(multiPropSortItems, function (x) { return [ { value: x.favorite, order: 'desc' }, x.category, ]; })).toEqual(multiPropSortItemsSorted); }); test('sort with multi keys', function () { expect(sortBy(withLocaleComparison, function (x) { return [ { value: x.category, compare: function (a, b) { return a.toString().localeCompare(b.toString()); }, }, x.category, ]; })).toEqual(sortedWithLocaleComparison); }); }); xdescribe('data last', function () { test('sort correctly', function () { expect(pipe(items, sortBy(function (x) { return x.a; }))).toEqual(sorted); }); test('sort multi objects correctly', function () { expect(pipe(items, sortBy(function (x) { return x.a; }))).toEqual(sorted); }); });