emilsoftware-utilities 1.3.14 → 1.3.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1,5 @@
1
- export declare function autobind(...args: any[]): any;
1
+ export declare function autobind<T extends {
2
+ new (...args: any[]): {};
3
+ }>(constructor: T): {
4
+ new (...args: any[]): {};
5
+ } & T;
package/dist/Autobind.js CHANGED
@@ -1,92 +1,45 @@
1
1
  "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
2
17
  Object.defineProperty(exports, "__esModule", { value: true });
3
18
  exports.autobind = autobind;
4
- // DECORATOR
5
- /**
6
- * Return a descriptor removing the value and returning a getter
7
- * The getter will return a .bind version of the function
8
- * and memoize the result against a symbol on the instance
9
- */
10
- function boundMethod(target, key, descriptor) {
11
- var fn = descriptor === null || descriptor === void 0 ? void 0 : descriptor.value;
12
- if (typeof fn !== 'function') {
13
- throw new TypeError("@boundMethod decorator can only be applied to methods not: ".concat(typeof fn));
14
- }
15
- // In IE11 calling Object.defineProperty has a side effect of evaluating the
16
- // getter for the property which is being replaced. This causes infinite
17
- // recursion and an "Out of stack space" error.
18
- var definingProperty = false;
19
- return {
20
- configurable: true,
21
- get: function () {
22
- // eslint-disable-next-line no-prototype-builtins
23
- if (definingProperty || this === target.prototype || this.hasOwnProperty(key) ||
24
- typeof fn !== 'function') {
25
- return fn;
19
+ function autobind(constructor) {
20
+ return /** @class */ (function (_super) {
21
+ __extends(class_1, _super);
22
+ function class_1() {
23
+ var args = [];
24
+ for (var _i = 0; _i < arguments.length; _i++) {
25
+ args[_i] = arguments[_i];
26
26
  }
27
- var boundFn = fn.bind(this);
28
- definingProperty = true;
29
- if (key) {
30
- Object.defineProperty(this, key, {
31
- configurable: true,
32
- get: function () {
33
- return boundFn;
34
- },
35
- set: function (value) {
36
- fn = value;
37
- // @ts-ignore
38
- delete this[key];
39
- }
40
- });
27
+ var _this = _super.apply(this, args) || this;
28
+ var prototype = Object.getPrototypeOf(_this);
29
+ var propertyNames = Object.getOwnPropertyNames(prototype);
30
+ for (var _a = 0, propertyNames_1 = propertyNames; _a < propertyNames_1.length; _a++) {
31
+ var key = propertyNames_1[_a];
32
+ var descriptor = Object.getOwnPropertyDescriptor(prototype, key);
33
+ if (descriptor && typeof descriptor.value === 'function' && key !== 'constructor') {
34
+ Object.defineProperty(_this, key, {
35
+ value: descriptor.value.bind(_this),
36
+ configurable: true,
37
+ writable: true,
38
+ });
39
+ }
41
40
  }
42
- definingProperty = false;
43
- return boundFn;
44
- },
45
- set: function (value) {
46
- fn = value;
47
- }
48
- };
49
- }
50
- /**
51
- * Use boundMethod to bind all methods on the target.prototype
52
- */
53
- function boundClass(target) {
54
- // (Using reflect to get all keys including symbols)
55
- var keys;
56
- // Use Reflect if exists
57
- if (typeof Reflect !== 'undefined' && typeof Reflect.ownKeys === 'function') {
58
- keys = Reflect.ownKeys(target.prototype);
59
- }
60
- else {
61
- keys = Object.getOwnPropertyNames(target.prototype);
62
- // Use symbols if support is provided
63
- if (typeof Object.getOwnPropertySymbols === 'function') {
64
- // @ts-ignore
65
- keys = keys.concat(Object.getOwnPropertySymbols(target.prototype));
66
- }
67
- }
68
- keys.forEach(function (key) {
69
- // Ignore special case target method
70
- if (key === 'constructor') {
71
- return;
41
+ return _this;
72
42
  }
73
- var descriptor = Object.getOwnPropertyDescriptor(target.prototype, key);
74
- // Only methods need binding
75
- if (typeof (descriptor === null || descriptor === void 0 ? void 0 : descriptor.value) === 'function') {
76
- Object.defineProperty(target.prototype, key, boundMethod(target, key, descriptor));
77
- }
78
- });
79
- return target;
80
- }
81
- function autobind() {
82
- var args = [];
83
- for (var _i = 0; _i < arguments.length; _i++) {
84
- args[_i] = arguments[_i];
85
- }
86
- if (args.length === 1) {
87
- // @ts-ignore
88
- return boundClass.apply(void 0, args);
89
- }
90
- // @ts-ignore
91
- return boundMethod.apply(void 0, args);
43
+ return class_1;
44
+ }(constructor));
92
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emilsoftware-utilities",
3
- "version": "1.3.14",
3
+ "version": "1.3.15",
4
4
  "description": "Utilities for EmilSoftware",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",