2024-02-29 23:57:36 +00:00
|
|
|
const MamiCompat = (path, handler) => {
|
2024-01-22 13:12:14 +00:00
|
|
|
if(typeof path !== 'string')
|
|
|
|
throw 'path must be a string';
|
2024-02-29 23:57:36 +00:00
|
|
|
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
|
|
|
|
2024-02-29 23:57:36 +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))
|
2024-02-29 23:57:36 +00:00
|
|
|
Object.defineProperty(current, final, handler);
|
2024-01-22 13:12:14 +00:00
|
|
|
};
|