All files ts-safe-access.ts

81.82% Statements 9/11
100% Branches 4/4
100% Functions 2/2
81.82% Lines 9/11
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  1x                 3x 3x 3x           1x               3x 3x 3x    
/**
 *
 * @param {T} obj
 * @param {(obj: T) => R} fn
 * @param {R} defaultValue
 * @returns {R}
 */
export function get<T, R>( obj : T, fn : ( obj : T ) => R, defaultValue? : R ) {
  try {
    const result = fn(obj);
    return fn(obj) === undefined ? defaultValue : result;
  } catch ( err ) {
    return defaultValue;
  }
}

 
/**
 *
 * @param {T} obj
 * @param {(o: T) => R} getFn
 * @returns {any}
 */
export function has<T, R>( obj : T, getFn : ( o : T ) => R ) {
  try {
    let result = getFn(obj);
    return result == undefined ? false : true;
  } catch ( err ) {
    return false;
  }
}