UNPKG

2.45 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var REACT_LIFECYCLE_EXCLUSIONS = [
4 'setState',
5 'render',
6 'componentWillMount',
7 'UNSAFE_componentWillMount',
8 'componentDidMount',
9 'componentWillReceiveProps',
10 'UNSAFE_componentWillReceiveProps',
11 'shouldComponentUpdate',
12 'componentWillUpdate',
13 'getSnapshotBeforeUpdate',
14 'UNSAFE_componentWillUpdate',
15 'componentDidUpdate',
16 'componentWillUnmount',
17];
18/**
19 * Allows you to hoist methods, except those in an exclusion set from a source object into a destination object.
20 *
21 * @public
22 * @param destination - The instance of the object to hoist the methods onto.
23 * @param source - The instance of the object where the methods are hoisted from.
24 * @param exclusions - (Optional) What methods to exclude from being hoisted.
25 * @returns An array of names of methods that were hoisted.
26 */
27function hoistMethods(
28// eslint-disable-next-line @typescript-eslint/no-explicit-any
29destination,
30// eslint-disable-next-line @typescript-eslint/no-explicit-any
31source, exclusions) {
32 if (exclusions === void 0) { exclusions = REACT_LIFECYCLE_EXCLUSIONS; }
33 var hoisted = [];
34 var _loop_1 = function (methodName) {
35 if (typeof source[methodName] === 'function' &&
36 destination[methodName] === undefined &&
37 (!exclusions || exclusions.indexOf(methodName) === -1)) {
38 hoisted.push(methodName);
39 // eslint-disable-next-line @typescript-eslint/no-explicit-any
40 destination[methodName] = function () {
41 var args = [];
42 for (var _i = 0; _i < arguments.length; _i++) {
43 args[_i] = arguments[_i];
44 }
45 source[methodName].apply(source, args);
46 };
47 }
48 };
49 for (var methodName in source) {
50 _loop_1(methodName);
51 }
52 return hoisted;
53}
54exports.hoistMethods = hoistMethods;
55/**
56 * Provides a method for convenience to unhoist hoisted methods.
57 *
58 * @public
59 * @param source - The source object upon which methods were hoisted.
60 * @param methodNames - An array of method names to unhoist.
61 */
62// eslint-disable-next-line @typescript-eslint/no-explicit-any
63function unhoistMethods(source, methodNames) {
64 methodNames.forEach(function (methodName) { return delete source[methodName]; });
65}
66exports.unhoistMethods = unhoistMethods;
67//# sourceMappingURL=hoist.js.map
\No newline at end of file