mami/src/mami.js/compat.js

23 lines
568 B
JavaScript
Raw Normal View History

const MamiCompat = (path, handler) => {
2024-01-22 13:12:14 +00:00
if(typeof path !== 'string')
throw 'path must be a string';
if(typeof handler !== 'object')
throw 'handler must be a property definition';
2024-01-18 19:50:37 +00:00
2024-01-22 13:12:14 +00:00
path = path.split('.');
2024-01-18 19:50:37 +00:00
2024-01-22 13:12:14 +00:00
const final = path.pop();
if(final === undefined)
throw 'invalid path';
2024-01-18 19:50:37 +00:00
let current = window;
2024-01-22 13:12:14 +00:00
for(const part of path) {
if(!(part in current))
current[part] = {};
current = current[part];
}
2024-01-18 19:50:37 +00:00
2024-01-22 13:12:14 +00:00
if(!(final in current))
Object.defineProperty(current, final, handler);
2024-01-22 13:12:14 +00:00
};