SIGN IN SIGN UP
preactjs / preact UNCLAIMED

⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.

0 0 191 JavaScript
/**
* Serialize an object
* @param {Object} obj
* @return {string}
*/
function serialize(obj) {
if (obj instanceof Text) return '#text';
if (obj instanceof Element) return `<${obj.localName}>${obj.textContent}`;
if (obj === document) return 'document';
2020-03-14 16:25:25 +01:00
if (typeof obj == 'string') return obj;
return Object.prototype.toString.call(obj).replace(/(^\[object |\]$)/g, '');
}
/** @type {string[]} */
let log = [];
/**
* Modify obj's original method to log calls and arguments on logger object
* @template T
* @param {T} obj
* @param {keyof T} method
*/
export function logCall(obj, method) {
let old = obj[method];
2023-03-21 14:16:56 -07:00
obj[method] = function (...args) {
let c = '';
2019-10-29 20:40:58 +01:00
for (let i = 0; i < args.length; i++) {
if (c) c += ', ';
c += serialize(args[i]);
}
let operation;
switch (method) {
case 'insertBefore': {
if (args[1] === null && args.length === 2) {
operation = `${serialize(this)}.appendChild(${serialize(args[0])})`;
} else {
operation = `${serialize(this)}.${String(method)}(${c})`;
}
break;
}
default: {
operation = `${serialize(this)}.${String(method)}(${c})`;
break;
}
}
log.push(operation);
return old.apply(this, args);
};
return () => (obj[method] = old);
}
/**
* Return log object
* @return {string[]} log
*/
export function getLog() {
return log;
}
/** Clear log object */
export function clearLog() {
log = [];
}
export function getLogSummary() {
/** @type {{ [key: string]: number }} */
const summary = {};
for (let entry of log) {
summary[entry] = (summary[entry] || 0) + 1;
}
return summary;
}