jeanpaul62
jeanpaul62

Reputation: 10551

Get the key of an object that a function uses

Let's say I have a props object, which contains multiple keys keyA, keyB...

And I have 3 functions funcA, funcB and funcC, each having one argument which is props. However, each function will use zero, one or more key/values of their props argument, and I wish to know which keys have been used by each function.

For example:

const funcA = props => {/* do something with props.keyA and props.keyB */}

const funcB = props => {/* do something with props.keyC */}

const funcC = props => {/* do nothing with props */}

And I wish to have a mechanism (i.e. a wrapper function, or a wrapper class...), which, when func{A,B,C} is called, will console.log the keys with which the function has been called:

// I wish to have the following lines console.logged:
funcA is using keyA and keyB
funcB is using keyC
funcC is not using anything

My first step is to create a Proxy on the props object, in the following way:

const propsProxy = new Proxy(props, {
  get (target, key) {
    console.log(`${key} has been used.`);
    return target[key];
  }
}

// ...

funcA(propsProxy); // Logs "keyA has been used. keyB has been used."
funcB(propsProxy); // Logs "keyC has been used."
funcC(propsProxy);

But then I'm stuck on how to log the function name too.

Edit: After reading some comments, it seems I need to add some clarifications. Let's make it more practical.

I'm actually writing this lib, let's call it wrapper, which end developers would use it like:

import wrapper from 'wrapper';

// User defines funcA=..., funcB=..., funcC=... and props

wrapper(funcA, funcB, funcC, props); // Will call funcA(props), funcB(props) and funcC(props)
// Should log:
// funcA is using keyA and keyB
// funcB is using keyC
// funcC is not using anything

Question: please write the code for wrapper?

Upvotes: 1

Views: 94

Answers (1)

ibrahim mahrir
ibrahim mahrir

Reputation: 31682

You can define wrapper like so:

function wrapper(props, ...funcs) {
  funcs.forEach(function(func) {                                       // for each function func passed as argument
    var used = [];                                                     // this will be the array of used property names (duplicates included)

    var propsProxy = new Proxy(props, {                                // set up a new proxy of props
      get(target, key) {
        used.push(key);                                                // ... which add the key used to used array
        return target[key];
      }
    });

    func(propsProxy);                                                  // call func with the proxy object

    console.log(func.name + " used " + (used.length? used.join(" and "): "nothing")); // log the results
  });
}

Example:

function funcA(props) {
  var result = props.propA + props.propC;
}

function funcB(props) {
  if(props.propA) {
    return true;
  }
}

function funcC(props) {
}

function wrapper(props, ...funcs) {
  funcs.forEach(function(func) {
    var used = [];

    var propsProxy = new Proxy(props, {
      get(target, key) {
        used.push(key);
        return target[key];
      }
    });

    func(propsProxy);
    
    console.log(func.name + " used " + (used.length? used.join(" and "): "nothing"));
  });
}

wrapper({
  propA: 5,
  propB: 6,
  propC: 7
}, funcA, funcB, funcC);

Upvotes: 1

Related Questions