"use strict";


/**
 * @name get property value by functions recursively
 * @type higher order function
 * @example
 *

   const o = {name: "THX1138", friends: [{name: "LUH3417"}, {name: "SEN5241"}]}
   getBy(() => "friends", xs => xs.length - 1, () => "name") (o); // "SEN5241"

 */


// (Object|Array -> String) -> Object -> a
const getBy = (...fs) => o => {
  const aux = (o, k, [f, ...tail]) => {
    if (f === undefined) return o;
    k = f(o);
    if (k in o) return aux(o[k], tail);
    throw new TypeError("invalid property");
  }

  return aux(o, "", fs);
};


// (Object, ((Object|Array -> String))) -> a
const getBy = (o, ...fs) => {
  const aux = (o, k, [f, ...tail]) => {
    if (f === undefined) return o;
    k = f(o);
    if (k in o) return aux(o[k], tail);
    throw new TypeError("invalid property");
  }

  return aux(o, "", fs);
};


// API


module.exports = {getBy, getBy_};
@@ -202,22 +202,18 @@
   getBy(
+K
 (
-) =%3E 
 %22friends
@@ -213,16 +213,17 @@
 friends%22
+)
 , xs =%3E 
@@ -241,21 +241,18 @@
  1, 
+K
 (
-) =%3E 
 %22name%22)
+)
  (o)

Version 0.1.19