import { expectType } from 'tsd' import { getByLoc, getDeep, getDeepByKey, getLastSegment, joinLoc, omitLastSegment, parseLocString, } from './_object' const obj = { a: { b: { c: { key: 'value' }, }, }, d: { b: { c: { b: { key: 'value' }, }, }, }, } test('getDeep', () => { const result = getDeep(obj, ['d', 'b', 'c']) expect(result).toEqual(obj.d.b.c) }) test('getDeepByKey', () => { const expected = [ { c: { key: 'value', }, }, { c: { b: { key: 'value' }, }, }, { key: 'value' }, ] type Expected = ( | { c: { key: string } } | { c: { b: { key: string } } } | { key: string } )[] const result = getDeepByKey(obj, 'b') expectType(result) // @ts-expect-error: error expectType(result) expect(result).toEqual(expected) }) test('parseLocString', () => { const result = parseLocString('a.b.c') expectType<['a', 'b', 'c']>(result) // @ts-expect-error: error expectType<['a', 'b']>(result) expect(result).toEqual(['a', 'b', 'c']) }) test('parseLocString (1 segment)', () => { const result = parseLocString('a') expectType<['a']>(result) // @ts-expect-error: error expectType<['a', 'b']>(result) expect(result).toEqual(['a']) }) test('parseLocString (empty)', () => { const result = parseLocString('') expectType<[]>(result) // @ts-expect-error: error expectType<['']>(result) expect(result).toEqual([]) }) test('getByLoc', () => { const result = getByLoc(obj, 'd.b.c') expect(result).toEqual(obj.d.b.c) }) test('joinLoc', () => { const result = joinLoc('a.b', 'c') expectType<'a.b.c'>(result) // @ts-expect-error: error expectType<'a.b'>(result) expect(result).toBe('a.b.c') }) test('joinLoc (empty)', () => { const result = joinLoc('', 'a.b') expectType<'a.b'>(result) // @ts-expect-error: error expectType<'.a.b'>(result) expect(result).toBe('a.b') }) test('getLastSegment', () => { const result = getLastSegment('a.b.c') expect(result).toBe('c') }) test('omitLastSegment', () => { const result = omitLastSegment('a.b.c') expectType<'a.b'>(result) // @ts-expect-error: error expectType<'a.b.c'>(result) expect(result).toBe('a.b') })