import { type EndsWith, endsWith, endsWithGuard, type WhenEndsWith, } from './ends-with.js' namespace TypeTests { type test1 = Expect, true>> type test2 = Expect, boolean>> type test3 = Expect, 'c'>, boolean>> type test4 = Expect, boolean>> type test6 = Expect, true>> type test7 = Expect, false>> type test8 = Expect, true>> type test9 = Expect, false>> // Template strings type testTS1 = Expect, true>> type testTS2 = Expect, false>> type testTS3 = Expect, true>> type testTS4 = Expect, false>> type testTS5 = Expect, true>> type testTS6 = Expect, boolean>> // Union types type testUnion1 = Expect, boolean>> type testUnion2 = Expect, true>> } describe('endsWith', () => { const text = 'abc' describe('without offset', () => { test('should return true when text ends with search', () => { const result = endsWith(text, 'c') expect(result).toEqual(true) type test = Expect> }) test('should return false when text does not end with search', () => { const result = endsWith(text, 'b') expect(result).toEqual(false) type test = Expect> }) }) describe('with offset', () => { test('should return true when offset text ends with search', () => { const result = endsWith(text, 'b', 2) expect(result).toEqual(true) type test = Expect> }) test('should return true when offset text ends with search (multi-char)', () => { const result = endsWith(text, 'bc', 3) expect(result).toEqual(true) type test = Expect> }) test('should return false when offset string does not end with search', () => { const result = endsWith(text, 'c', 1) expect(result).toEqual(false) type test = Expect> }) }) describe('with bad offset', () => { test('should return false when the offset is negative', () => { const result = endsWith(text, 'a', -1) expect(result).toEqual(false) type test = Expect> }) test('should return true when the end matches and offset is greater than text length', () => { const result = endsWith(text, 'c', 10) expect(result).toEqual(true) type test = Expect> }) }) }) namespace GuardTypeTests { // keeps the members that end with the search, drops the ones that don't type test1 = Expect, 'abc'>> type test2 = Expect< Equal, 'abc' | 'xyzc'> > // a single non-matching member narrows to `never` type test3 = Expect, never>> // non-literal members can't be ruled out, so they're kept type test4 = Expect, string>> type test5 = Expect, 'abc'>> // honors the position argument type test6 = Expect< Equal, 'abcde' | 'abxde'> > } describe('endsWithGuard', () => { describe('type narrowing', () => { test('narrows a union to the members that end with the search', () => { const reportType = 'HouseCalendar' as | 'HouseCalendar' | 'SenateCalendar' | 'HouseFirstReading' if (endsWithGuard(reportType, 'Calendar')) { type test = Expect< Equal > expect(reportType.endsWith('Calendar')).toBe(true) } else { type test = Expect> } }) }) describe('runtime behavior', () => { const text = 'abc' test('returns a plain boolean (narrowing trades away the literal)', () => { const result = endsWithGuard(text, 'c') type test = Expect> expect(result).toBe(true) }) test('returns false when text does not end with search', () => { expect(endsWithGuard(text, 'b')).toBe(false) }) test('honors the position argument', () => { expect(endsWithGuard(text, 'b', 2)).toBe(true) }) }) })