All files / apis/shared get.ts

18.18% Statements 2/11
0% Branches 0/1
0% Functions 0/2
20% Lines 2/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                  1x                                       1x                            
/**
 * Безопасно получает свойство объекта
 *
 * @export
 * @template T
 * @param {Record<string, unknown>} object
 * @param {string} key
 * @return {T|undefined}  {(T | undefined)}
 */
export function getOwnProperty<O extends Record<string, unknown>, K extends keyof O>(
  object: O,
  key: K
): O[K] {
  const keys = Object.keys(object);
 
  Iif (!keys.includes(key as string)) return undefined as O[K];
 
  return Reflect.get(object, key);
}
 
/**
 * Safe _.get
 *
 * @export
 * @template {T}
 * @param {Record<string, unknown>} object
 * @param {string} key
 * @return {T|undefined}
 */
export function getOwnPropertyDeep<T>(
  object: Record<string, unknown>,
  key: string
): T | undefined {
  const parts = key.split(".");
 
  let value: any = object;
 
  for (const path of parts) {
    value = getOwnProperty(value, path);
  }
 
  return value;
}