All files / src/apis/shared get.ts

100% Statements 9/9
100% Branches 4/4
100% Functions 3/3
100% Lines 9/9

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 45 46 47 48 49 50 51 52 53 54 55 56 57 58                                6x 34x                       6x                           6x       11x   11x   11x 18x     11x    
import type { AnyRecord } from "./types";
 
export function getOwnProperty<O extends AnyRecord, K extends keyof O>(
  object: O,
  key: K
): O[K];
 
/**
 * Безопасно получает свойство объекта
 *
 * @export
 * @template T
 * @param {AnyRecord} object
 * @param {string} key
 * @return {T|undefined} (T | undefined)
 */
export function getOwnProperty(object: AnyRecord, key: string): unknown | undefined {
  return Object.getOwnPropertyDescriptor(object, key)?.value;
}
 
/**
 *
 *
 * @export
 * @template T
 * @param {ArrayLike<T>} array
 * @param {number} index
 * @return {T} (T | undefined)
 */
export function getByIndex<T>(array: ArrayLike<T>, index: number): T | undefined {
  /* istanbul ignore next: used in web platform adapters */
  return getOwnProperty<Record<string, T>, string>(array as any, index.toString());
}
 
/**
 * Safe _.get
 *
 * @export
 * @template {T}
 * @param {AnyRecord} object
 * @param {string} key
 * @return {T|undefined}
 */
export function getOwnPropertyDeep<T>(
  object: AnyRecord,
  key: string
): T | undefined {
  const parts = key.split(".");
 
  let value: any = object;
 
  for (const path of parts) {
    value = getOwnProperty(value, path);
  }
 
  return value;
}