|
| 1 | +import { describe, it } from "./_describe.ts" |
| 2 | +import { groupWith, equals } from '../mod.ts' |
| 3 | +import { eq } from "./utils/utils.ts" |
| 4 | + |
| 5 | +describe('groupWith', () => { |
| 6 | + |
| 7 | + it('should split the list into groups according to the grouping function', () => { |
| 8 | + eq(groupWith(equals, [1, 2, 2, 3]), [[1], [2, 2], [3]]) |
| 9 | + eq(groupWith(equals, [1, 1, 1, 1]), [[1, 1, 1, 1]]) |
| 10 | + eq(groupWith(equals, [1, 2, 3, 4]), [[1], [2], [3], [4]]) |
| 11 | + }) |
| 12 | + |
| 13 | + it('should split the list into "streaks" testing adjacent elements', () => { |
| 14 | + // @ts-ignore |
| 15 | + const isConsecutive = function(a, b) { return a + 1 === b; } |
| 16 | + eq(groupWith(isConsecutive, []), []) |
| 17 | + eq(groupWith(isConsecutive, [4, 3, 2, 1]), [[4], [3], [2], [1]]) |
| 18 | + eq(groupWith(isConsecutive, [1, 2, 3, 4]), [[1, 2, 3, 4]]) |
| 19 | + eq(groupWith(isConsecutive, [1, 2, 2, 3]), [[1, 2], [2, 3]]) |
| 20 | + eq(groupWith(isConsecutive, [1, 2, 9, 3, 4]), [[1, 2], [9], [3, 4]]) |
| 21 | + }) |
| 22 | + |
| 23 | + it('should return an empty array if given an empty array', () => { |
| 24 | + eq(groupWith(equals, []), []) |
| 25 | + }) |
| 26 | + |
| 27 | + // TODO: |
| 28 | + // it('can be turned into the original list through concatenation', () => { |
| 29 | + // var list = [1, 1, 2, 3, 4, 4, 5, 5] |
| 30 | + // eq(R.unnest(groupWith(equals, list)), list) |
| 31 | + // eq(R.unnest(groupWith(R.complement(equals), list)), list) |
| 32 | + // eq(R.unnest(groupWith(R.T, list)), list) |
| 33 | + // eq(R.unnest(groupWith(R.F, list)), list) |
| 34 | + // }) |
| 35 | + |
| 36 | + it('should also work on strings', () => { |
| 37 | + eq(groupWith(equals)('Mississippi'), ['M','i','ss','i','ss','i','pp','i']) |
| 38 | + }) |
| 39 | + |
| 40 | +}) |
0 commit comments