api call validator for Marci

by Csaba Hellinger

HTML

<div id="test"></div>

JavaScript

function tryToCallApi(apiName, ...params) {
	let thisArg = null;
    const api = apiName
    	.split('.')
        .reduce((acc, current) => {
        	thisArg = acc;
        	return acc[current] || {}
        }, window);
    if (typeof api === 'function') {
    	return api.apply(thisArg, params);
    }
}

tryToCallApi('console.clear');                       // calls it
tryToCallApi('document.getElementById', 'test');     // returns the <div/>
tryToCallApi('console.log', '1', '2', '3');          // logs all params
tryToCallApi('document.missingFunc', 'foo', 'bar');  // nothing
tryToCallApi('console.this.is.not.here', 'nyeh');    // nothing