buckets.spec.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { createRangeArray } from '../array';
  7. import { makeBuckets } from '../buckets';
  8. describe('buckets', () => {
  9. function reorder(order: ArrayLike<number>, data: any[]): any[] {
  10. const ret = [];
  11. for (const i of (order as number[])) ret[ret.length] = data[i];
  12. return ret;
  13. }
  14. it('full range', () => {
  15. const xs = [1, 1, 2, 2, 3, 1];
  16. const range = createRangeArray(0, xs.length - 1);
  17. const bs = makeBuckets(range, i => xs[i]);
  18. expect(reorder(range, xs)).toEqual([1, 1, 1, 2, 2, 3]);
  19. expect(Array.from(bs)).toEqual([0, 3, 5, 6]);
  20. });
  21. it('sort', () => {
  22. const xs = [3, 1, 2, 1, 2, 3];
  23. const range = createRangeArray(0, xs.length - 1);
  24. makeBuckets(range, i => xs[i], { sort: true });
  25. expect(reorder(range, xs)).toEqual([1, 1, 2, 2, 3, 3]);
  26. });
  27. it('subrange', () => {
  28. const xs = [2, 1, 2, 1, 2, 3, 1];
  29. const range = createRangeArray(0, xs.length - 1);
  30. const bs = makeBuckets(range, i => xs[i], { sort: false, start: 1, end: 5 });
  31. expect(reorder(range, xs)).toEqual([2, 1, 1, 2, 2, 3, 1]);
  32. expect(Array.from(bs)).toEqual([1, 3, 5]);
  33. });
  34. });