/
githubmirror
/
preact
Обзор
Документация
Войти
/
githubmirror
/
preact
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
test/_util/logCall.js
85 строк
2 KB
jdecroock
Experiment: add moveBefore rather than insertBefor when available
16 июл 2026, 19:05
16 июл 2026, 19:05
0273151
Код
Авторство
О чём код?
/** * 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'; 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]; obj[method] = function (...args) { let c = ''; 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; } case 'moveBefore': { if (args[1] === null && args.length === 2) { operation = `${serialize(this)}.appendChild(${serialize(args[0])})`; } else { operation = `${serialize(this)}.insertBefore(${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; }