1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/lib/services/plugins/utils/mapEventHandlersToIds.ts

32 lines
916 B
TypeScript
Raw Normal View History

let eventHandlerIndex_ = 1;
export interface EventHandlers {
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
[key: string]: Function;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
export default function mapEventHandlersToIds(arg: any, eventHandlers: EventHandlers) {
if (Array.isArray(arg)) {
for (let i = 0; i < arg.length; i++) {
arg[i] = mapEventHandlersToIds(arg[i], eventHandlers);
}
return arg;
} else if (typeof arg === 'function') {
const id = `___plugin_event_${eventHandlerIndex_}`;
eventHandlerIndex_++;
eventHandlers[id] = arg;
return id;
} else if (arg === null) {
return null;
} else if (arg === undefined) {
return undefined;
} else if (typeof arg === 'object') {
for (const n in arg) {
arg[n] = mapEventHandlersToIds(arg[n], eventHandlers);
}
}
return arg;
}