import type {NestedKeys, NestedValue, PlainObject} from '../../models'; import type {Ok} from '../../result/models'; import {getNestedValue} from './misc'; // #region Functions /** * Get the value from an object using a known path * * @param data Object to get value from * @param path Path for value * @returns Found value, or `undefined` * * @example * ```typescript * const data = {foo: {bar: {baz: 42}}}; * * getValue(data, 'foo'); // => {bar: {baz: 42}} * getValue(data, 'foo.bar'); // => {baz: 42} * getValue(data, 'foo.bar.baz'); // => 42 * getValue(data, 'foo.nope'); // => undefined * ``` */ export function getValue>( data: Data, path: Path, ): NestedValue; /** * Get the value from an object using an unknown path * * @param data Object to get value from * @param path Path for value * @param ignoreCase If `true`, path matching is case-insensitive * @returns Found value, or `undefined` * * @example * ```typescript * const data = {foo: {bar: {baz: 42}}}; * * getValue(data, 'foo'); // => {bar: {baz: 42}} * getValue(data, 'foo.bar'); // => {baz: 42} * getValue(data, 'Foo.Bar.Baz', true); // => 42 * getValue(data, 'foo.nope'); // => undefined * ``` */ export function getValue( data: Data, path: string, ignoreCase?: boolean, ): unknown; export function getValue(data: PlainObject, path: string, ignoreCase?: boolean): unknown { return (getNestedValue(data, path, ignoreCase === true) as Ok).value; } // #endregion