spiritjs
Version:
The animation toolkit for the web
49 lines (48 loc) • 1.47 kB
JavaScript
;
exports.__esModule = true;
exports["default"] = _default;
var _polyfill = require("./polyfill");
function _default() {
return arguments.length === 1 ? boundClass.apply(void 0, arguments) : boundMethod.apply(void 0, arguments);
}
var ignoreMethods = ['constructor'];
function boundClass(target) {
var keys = Object.getOwnPropertyNames(target.prototype);
if (typeof Object.getOwnPropertySymbols === 'function') {
keys = keys.concat(Object.getOwnPropertySymbols(target.prototype));
}
keys.forEach(function (key) {
if ((0, _polyfill.includes)(ignoreMethods, key)) {
return;
}
var descriptor = Object.getOwnPropertyDescriptor(target.prototype, key);
if (typeof descriptor.value === 'function') {
Object.defineProperty(target.prototype, key, boundMethod(target, key, descriptor));
}
});
return target;
}
function boundMethod(target, key, descriptor) {
var fn = descriptor.value;
if (typeof fn !== 'function') {
throw new Error("@autobind decorator can only be applied to methods not: " + typeof fn);
}
return {
configurable: true,
get: function get() {
if (this === target.prototype || this.hasOwnProperty(key)) {
return fn;
}
var boundFn = fn.bind(this);
Object.defineProperty(this, key, {
value: boundFn,
configurable: true,
writable: true
});
return boundFn;
},
set: function set() {
return fn;
}
};
}