| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import {basename, generatorContains, group, mapObject, partition, truncate} from '../utils'; |
| 9 | |
| 10 | describe('basename', () => { |
| 11 | it('/path/to/foo.txt -> foo.txt', () => { |
| 12 | expect(basename('/path/to/foo.txt')).toEqual('foo.txt'); |
| 13 | }); |
| 14 | |
| 15 | it("/path/to/ -> ''", () => { |
| 16 | expect(basename('/path/to/')).toEqual(''); |
| 17 | }); |
| 18 | |
| 19 | it('customizable delimiters', () => { |
| 20 | expect(basename('/path/to/foo.txt', '.')).toEqual('txt'); |
| 21 | }); |
| 22 | |
| 23 | it('empty string', () => { |
| 24 | expect(basename('')).toEqual(''); |
| 25 | }); |
| 26 | |
| 27 | it('delimiter not in string', () => { |
| 28 | expect(basename('hello world')).toEqual('hello world'); |
| 29 | }); |
| 30 | }); |
| 31 | |
| 32 | describe('mapObject', () => { |
| 33 | it('maps object types', () => { |
| 34 | expect(mapObject({foo: 123, bar: 456}, ([key, value]) => [key, {value}])).toEqual({ |
| 35 | foo: {value: 123}, |
| 36 | bar: {value: 456}, |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | it('handles different key types', () => { |
| 41 | expect(mapObject({foo: 123, bar: 456}, ([key, value]) => [value, key])).toEqual({ |
| 42 | 123: 'foo', |
| 43 | 456: 'bar', |
| 44 | }); |
| 45 | }); |
| 46 | }); |
| 47 | |
| 48 | describe('generatorContains', () => { |
| 49 | let lastYield = 0; |
| 50 | function* gen() { |
| 51 | lastYield = 3; |
| 52 | yield 3; |
| 53 | lastYield = 5; |
| 54 | yield 5; |
| 55 | } |
| 56 | |
| 57 | it('supports testing a value', () => { |
| 58 | expect(generatorContains(gen(), 3)).toBeTruthy(); |
| 59 | expect(lastYield).toBe(3); |
| 60 | expect(generatorContains(gen(), 5)).toBeTruthy(); |
| 61 | expect(lastYield).toBe(5); |
| 62 | expect(generatorContains(gen(), 2)).toBeFalsy(); |
| 63 | expect(lastYield).toBe(5); |
| 64 | }); |
| 65 | |
| 66 | it('supports testing via a function', () => { |
| 67 | expect(generatorContains(gen(), v => v > 2)).toBeTruthy(); |
| 68 | expect(lastYield).toBe(3); |
| 69 | expect(generatorContains(gen(), v => v > 4)).toBeTruthy(); |
| 70 | expect(lastYield).toBe(5); |
| 71 | expect(generatorContains(gen(), v => v > 6)).toBeFalsy(); |
| 72 | expect(lastYield).toBe(5); |
| 73 | }); |
| 74 | }); |
| 75 | |
| 76 | describe('truncate', () => { |
| 77 | it('does not truncate strings within the maxLength constraint', () => { |
| 78 | expect(truncate('abc', 3)).toBe('abc'); |
| 79 | expect(truncate('def', 4)).toBe('def'); |
| 80 | }); |
| 81 | |
| 82 | it('truncates long strings', () => { |
| 83 | expect(truncate('abc', 2)).toBe('a…'); |
| 84 | expect(truncate('def', 0)).toBe('…'); |
| 85 | }); |
| 86 | }); |
| 87 | |
| 88 | describe('partition', () => { |
| 89 | it('partitions an array into two arrays based on a predicate', () => { |
| 90 | expect(partition([1, 2, 3], n => n % 2 === 0)).toEqual([[2], [1, 3]]); |
| 91 | expect(partition([1, 2, 3], () => true)).toEqual([[1, 2, 3], []]); |
| 92 | expect(partition([1, 2, 3], () => false)).toEqual([[], [1, 2, 3]]); |
| 93 | }); |
| 94 | }); |
| 95 | |
| 96 | describe('group', () => { |
| 97 | it('groups an array into multiple subarrays based on a key', () => { |
| 98 | expect(group(['a', 'ab', 'abc', 'ef', 'g'], e => e.length)).toEqual({ |
| 99 | 1: ['a', 'g'], |
| 100 | 2: ['ab', 'ef'], |
| 101 | 3: ['abc'], |
| 102 | }); |
| 103 | }); |
| 104 | }); |
| 105 | |