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