@dynamic-labs/utils 0.0.0-exp20240808.0
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.
- package/CHANGELOG.md +3746 -0
- package/LICENSE +21 -0
- package/README.md +0 -0
- package/_virtual/_tslib.cjs +51 -0
- package/_virtual/_tslib.js +45 -0
- package/package.json +37 -0
- package/src/CancellablePromise/CancellablePromise.cjs +136 -0
- package/src/CancellablePromise/CancellablePromise.d.ts +54 -0
- package/src/CancellablePromise/CancellablePromise.js +132 -0
- package/src/CancellablePromise/index.d.ts +1 -0
- package/src/DeferredPromise/DeferredPromise.cjs +22 -0
- package/src/DeferredPromise/DeferredPromise.d.ts +13 -0
- package/src/DeferredPromise/DeferredPromise.js +18 -0
- package/src/DeferredPromise/index.d.ts +1 -0
- package/src/bufferPolyfill.cjs +15 -0
- package/src/bufferPolyfill.d.ts +1 -0
- package/src/bufferPolyfill.js +13 -0
- package/src/bufferToBase64.cjs +15 -0
- package/src/bufferToBase64.d.ts +2 -0
- package/src/bufferToBase64.js +11 -0
- package/src/ceil/ceil.cjs +11 -0
- package/src/ceil/ceil.d.ts +1 -0
- package/src/ceil/ceil.js +7 -0
- package/src/ceil/index.d.ts +1 -0
- package/src/eip6963/eip6963Provider.cjs +40 -0
- package/src/eip6963/eip6963Provider.d.ts +34 -0
- package/src/eip6963/eip6963Provider.js +35 -0
- package/src/eip6963/index.d.ts +1 -0
- package/src/errors/AccessBlockedError.cjs +11 -0
- package/src/errors/AccessBlockedError.d.ts +3 -0
- package/src/errors/AccessBlockedError.js +7 -0
- package/src/errors/AccountExistsError.cjs +16 -0
- package/src/errors/AccountExistsError.d.ts +14 -0
- package/src/errors/AccountExistsError.js +12 -0
- package/src/errors/ChainalysisError.cjs +15 -0
- package/src/errors/ChainalysisError.d.ts +5 -0
- package/src/errors/ChainalysisError.js +11 -0
- package/src/errors/CookieInvalidError.cjs +14 -0
- package/src/errors/CookieInvalidError.d.ts +4 -0
- package/src/errors/CookieInvalidError.js +10 -0
- package/src/errors/CustomError.cjs +24 -0
- package/src/errors/CustomError.d.ts +11 -0
- package/src/errors/CustomError.js +20 -0
- package/src/errors/CustomFieldNotUniqueError.cjs +14 -0
- package/src/errors/CustomFieldNotUniqueError.d.ts +4 -0
- package/src/errors/CustomFieldNotUniqueError.js +10 -0
- package/src/errors/DynamicError.cjs +11 -0
- package/src/errors/DynamicError.d.ts +3 -0
- package/src/errors/DynamicError.js +7 -0
- package/src/errors/EmailAlreadyExistsError.cjs +14 -0
- package/src/errors/EmailAlreadyExistsError.d.ts +4 -0
- package/src/errors/EmailAlreadyExistsError.js +10 -0
- package/src/errors/EmailProviderError.cjs +14 -0
- package/src/errors/EmailProviderError.d.ts +4 -0
- package/src/errors/EmailProviderError.js +10 -0
- package/src/errors/EmailVerificationError.cjs +14 -0
- package/src/errors/EmailVerificationError.d.ts +4 -0
- package/src/errors/EmailVerificationError.js +10 -0
- package/src/errors/EmbeddedWalletException.cjs +14 -0
- package/src/errors/EmbeddedWalletException.d.ts +4 -0
- package/src/errors/EmbeddedWalletException.js +10 -0
- package/src/errors/ExternalAuthError.cjs +14 -0
- package/src/errors/ExternalAuthError.d.ts +4 -0
- package/src/errors/ExternalAuthError.js +10 -0
- package/src/errors/GateBlockedError.cjs +15 -0
- package/src/errors/GateBlockedError.d.ts +5 -0
- package/src/errors/GateBlockedError.js +11 -0
- package/src/errors/InsufficientFundsError.cjs +25 -0
- package/src/errors/InsufficientFundsError.d.ts +9 -0
- package/src/errors/InsufficientFundsError.js +21 -0
- package/src/errors/InvalidPhoneNumberError.cjs +14 -0
- package/src/errors/InvalidPhoneNumberError.d.ts +4 -0
- package/src/errors/InvalidPhoneNumberError.js +10 -0
- package/src/errors/MergeAccountsConfirmationError.cjs +16 -0
- package/src/errors/MergeAccountsConfirmationError.d.ts +12 -0
- package/src/errors/MergeAccountsConfirmationError.js +12 -0
- package/src/errors/MfaInvalidOtpError.cjs +14 -0
- package/src/errors/MfaInvalidOtpError.d.ts +4 -0
- package/src/errors/MfaInvalidOtpError.js +10 -0
- package/src/errors/MissingEnvironmentIdError.cjs +14 -0
- package/src/errors/MissingEnvironmentIdError.d.ts +4 -0
- package/src/errors/MissingEnvironmentIdError.js +10 -0
- package/src/errors/MissingPublicAddressError.cjs +14 -0
- package/src/errors/MissingPublicAddressError.d.ts +4 -0
- package/src/errors/MissingPublicAddressError.js +10 -0
- package/src/errors/NoAccessError.cjs +19 -0
- package/src/errors/NoAccessError.d.ts +15 -0
- package/src/errors/NoAccessError.js +15 -0
- package/src/errors/NotSupportedError.cjs +11 -0
- package/src/errors/NotSupportedError.d.ts +3 -0
- package/src/errors/NotSupportedError.js +7 -0
- package/src/errors/PasskeyError.d.ts +8 -0
- package/src/errors/SmsVerificationError.cjs +11 -0
- package/src/errors/SmsVerificationError.d.ts +3 -0
- package/src/errors/SmsVerificationError.js +7 -0
- package/src/errors/SocialAccountAlreadyExistsError.cjs +14 -0
- package/src/errors/SocialAccountAlreadyExistsError.d.ts +4 -0
- package/src/errors/SocialAccountAlreadyExistsError.js +10 -0
- package/src/errors/TooManyEmailVerificationsError.cjs +14 -0
- package/src/errors/TooManyEmailVerificationsError.d.ts +4 -0
- package/src/errors/TooManyEmailVerificationsError.js +10 -0
- package/src/errors/TransactionGasCannotBeSponsoredError.cjs +25 -0
- package/src/errors/TransactionGasCannotBeSponsoredError.d.ts +9 -0
- package/src/errors/TransactionGasCannotBeSponsoredError.js +21 -0
- package/src/errors/UserHasAccountWithEmailError.cjs +15 -0
- package/src/errors/UserHasAccountWithEmailError.d.ts +5 -0
- package/src/errors/UserHasAccountWithEmailError.js +11 -0
- package/src/errors/UserRejectedTransactionError.cjs +14 -0
- package/src/errors/UserRejectedTransactionError.d.ts +4 -0
- package/src/errors/UserRejectedTransactionError.js +10 -0
- package/src/errors/UsernameAlreadyExistsError.cjs +14 -0
- package/src/errors/UsernameAlreadyExistsError.d.ts +4 -0
- package/src/errors/UsernameAlreadyExistsError.js +10 -0
- package/src/errors/VerificationDataCollectionError.cjs +15 -0
- package/src/errors/VerificationDataCollectionError.d.ts +5 -0
- package/src/errors/VerificationDataCollectionError.js +11 -0
- package/src/errors/WalletAddressMismatchError.cjs +17 -0
- package/src/errors/WalletAddressMismatchError.d.ts +11 -0
- package/src/errors/WalletAddressMismatchError.js +13 -0
- package/src/errors/WalletNotDeployedError.cjs +14 -0
- package/src/errors/WalletNotDeployedError.d.ts +4 -0
- package/src/errors/WalletNotDeployedError.js +10 -0
- package/src/errors/WalletUsedError.cjs +14 -0
- package/src/errors/WalletUsedError.d.ts +4 -0
- package/src/errors/WalletUsedError.js +10 -0
- package/src/errors/index.d.ts +33 -0
- package/src/formatNumberText/formatNumberText.cjs +20 -0
- package/src/formatNumberText/formatNumberText.d.ts +5 -0
- package/src/formatNumberText/formatNumberText.js +16 -0
- package/src/formatNumberText/index.d.ts +1 -0
- package/src/get/get.cjs +24 -0
- package/src/get/get.d.ts +7 -0
- package/src/get/get.js +20 -0
- package/src/get/index.d.ts +1 -0
- package/src/getProvidersFromWindow.cjs +39 -0
- package/src/getProvidersFromWindow.d.ts +7 -0
- package/src/getProvidersFromWindow.js +35 -0
- package/src/getTLD/getTLD.cjs +12 -0
- package/src/getTLD/getTLD.d.ts +1 -0
- package/src/getTLD/getTLD.js +8 -0
- package/src/getTLD/index.d.ts +1 -0
- package/src/handleMobileWalletRedirect/handleMobileWalletRedirect.cjs +20 -0
- package/src/handleMobileWalletRedirect/handleMobileWalletRedirect.d.ts +4 -0
- package/src/handleMobileWalletRedirect/handleMobileWalletRedirect.js +16 -0
- package/src/handleMobileWalletRedirect/index.d.ts +1 -0
- package/src/hexToString/hexToString.cjs +32 -0
- package/src/hexToString/hexToString.d.ts +12 -0
- package/src/hexToString/hexToString.js +28 -0
- package/src/hexToString/index.d.ts +1 -0
- package/src/index.cjs +151 -0
- package/src/index.d.ts +32 -0
- package/src/index.js +66 -0
- package/src/isFunction/index.d.ts +1 -0
- package/src/isFunction/isFunction.cjs +9 -0
- package/src/isFunction/isFunction.d.ts +1 -0
- package/src/isFunction/isFunction.js +5 -0
- package/src/isHex/index.d.ts +1 -0
- package/src/isHex/isHex.cjs +20 -0
- package/src/isHex/isHex.d.ts +6 -0
- package/src/isHex/isHex.js +16 -0
- package/src/isLedgerAddressViaVerifiedCredentials.cjs +13 -0
- package/src/isLedgerAddressViaVerifiedCredentials.d.ts +2 -0
- package/src/isLedgerAddressViaVerifiedCredentials.js +9 -0
- package/src/isMobile.cjs +146 -0
- package/src/isMobile.d.ts +18 -0
- package/src/isMobile.js +133 -0
- package/src/last.cjs +22 -0
- package/src/last.d.ts +15 -0
- package/src/last.js +18 -0
- package/src/localStorageAsync.cjs +38 -0
- package/src/localStorageAsync.d.ts +3 -0
- package/src/localStorageAsync.js +32 -0
- package/src/logger/index.d.ts +1 -0
- package/src/logger/logger.cjs +10 -0
- package/src/logger/logger.d.ts +2 -0
- package/src/logger/logger.js +6 -0
- package/src/nativeMobileOauthStateParam.cjs +13 -0
- package/src/nativeMobileOauthStateParam.d.ts +14 -0
- package/src/nativeMobileOauthStateParam.js +9 -0
- package/src/parseChainId.cjs +8 -0
- package/src/parseChainId.d.ts +1 -0
- package/src/parseChainId.js +4 -0
- package/src/parseEvmNetworks.cjs +17 -0
- package/src/parseEvmNetworks.d.ts +2 -0
- package/src/parseEvmNetworks.js +13 -0
- package/src/pipe/index.d.ts +1 -0
- package/src/pipe/pipe.cjs +40 -0
- package/src/pipe/pipe.d.ts +27 -0
- package/src/pipe/pipe.js +36 -0
- package/src/retryableFn.cjs +73 -0
- package/src/retryableFn.d.ts +19 -0
- package/src/retryableFn.js +68 -0
- package/src/runSafe/index.d.ts +1 -0
- package/src/runSafe/runSafe.cjs +25 -0
- package/src/runSafe/runSafe.d.ts +8 -0
- package/src/runSafe/runSafe.js +21 -0
- package/src/services/FetchService/FetchService.cjs +29 -0
- package/src/services/FetchService/FetchService.d.ts +10 -0
- package/src/services/FetchService/FetchService.js +25 -0
- package/src/services/FetchService/index.d.ts +2 -0
- package/src/services/FetchService/types.d.ts +3 -0
- package/src/services/Oauth2Service/Oauth2Service.cjs +38 -0
- package/src/services/Oauth2Service/Oauth2Service.d.ts +34 -0
- package/src/services/Oauth2Service/Oauth2Service.js +34 -0
- package/src/services/Oauth2Service/createWindowOauth2Service/createWindowOauth2Service.cjs +192 -0
- package/src/services/Oauth2Service/createWindowOauth2Service/createWindowOauth2Service.d.ts +2 -0
- package/src/services/Oauth2Service/createWindowOauth2Service/createWindowOauth2Service.js +188 -0
- package/src/services/Oauth2Service/createWindowOauth2Service/index.d.ts +1 -0
- package/src/services/Oauth2Service/index.d.ts +2 -0
- package/src/services/PlatformService/PlatformService.cjs +40 -0
- package/src/services/PlatformService/PlatformService.d.ts +11 -0
- package/src/services/PlatformService/PlatformService.js +36 -0
- package/src/services/PlatformService/createBrowserPlatformService/createBrowserPlatformService.cjs +30 -0
- package/src/services/PlatformService/createBrowserPlatformService/createBrowserPlatformService.d.ts +2 -0
- package/src/services/PlatformService/createBrowserPlatformService/createBrowserPlatformService.js +26 -0
- package/src/services/PlatformService/createBrowserPlatformService/index.d.ts +1 -0
- package/src/services/PlatformService/index.d.ts +3 -0
- package/src/services/PlatformService/types.d.ts +25 -0
- package/src/sleep/index.d.ts +1 -0
- package/src/sleep/sleep.cjs +13 -0
- package/src/sleep/sleep.d.ts +11 -0
- package/src/sleep/sleep.js +9 -0
- package/src/template/index.d.ts +1 -0
- package/src/template/template.cjs +25 -0
- package/src/template/template.d.ts +18 -0
- package/src/template/template.js +21 -0
- package/src/trimEnd/index.d.ts +1 -0
- package/src/trimEnd/trimEnd.cjs +14 -0
- package/src/trimEnd/trimEnd.d.ts +1 -0
- package/src/trimEnd/trimEnd.js +10 -0
- package/src/uniq/index.d.ts +1 -0
- package/src/uniq/uniq.cjs +33 -0
- package/src/uniq/uniq.d.ts +17 -0
- package/src/uniq/uniq.js +29 -0
- package/src/wrapMethodWithCallback/index.d.ts +1 -0
- package/src/wrapMethodWithCallback/wrapMethodWithCallback.cjs +47 -0
- package/src/wrapMethodWithCallback/wrapMethodWithCallback.d.ts +36 -0
- package/src/wrapMethodWithCallback/wrapMethodWithCallback.js +43 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
type ExtractPlaceholders<T extends string> = T extends `${infer _Start}{{${infer Placeholder}}}${infer Rest}` ? Placeholder | ExtractPlaceholders<Rest> : never;
|
|
2
|
+
/**
|
|
3
|
+
* Creates a template function that replaces placeholders with corresponding values from a data object.
|
|
4
|
+
* @param {string} templateText - The template string containing placeholders in the form {{placeholder}}.
|
|
5
|
+
* @returns {(data) => string} - A function that replaces the placeholders with the values from the data object.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* // Basic usage
|
|
9
|
+
* const compiled = template('Test text {{placeholder}} value');
|
|
10
|
+
* console.log(compiled({ placeholder: 'test' })); // Output: 'Test text test value'
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // Multiple placeholders
|
|
14
|
+
* const compiled = template('Test text {{placeholder}} value {{placeholder2}}');
|
|
15
|
+
* console.log(compiled({ placeholder: 'test', placeholder2: 'test2' })); // Output: 'Test text test value test2'
|
|
16
|
+
*/
|
|
17
|
+
export declare const template: <T extends string>(templateText: T) => (data: { [key in ExtractPlaceholders<T>]: string; }) => string;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/**
|
|
3
|
+
* Creates a template function that replaces placeholders with corresponding values from a data object.
|
|
4
|
+
* @param {string} templateText - The template string containing placeholders in the form {{placeholder}}.
|
|
5
|
+
* @returns {(data) => string} - A function that replaces the placeholders with the values from the data object.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* // Basic usage
|
|
9
|
+
* const compiled = template('Test text {{placeholder}} value');
|
|
10
|
+
* console.log(compiled({ placeholder: 'test' })); // Output: 'Test text test value'
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // Multiple placeholders
|
|
14
|
+
* const compiled = template('Test text {{placeholder}} value {{placeholder2}}');
|
|
15
|
+
* console.log(compiled({ placeholder: 'test', placeholder2: 'test2' })); // Output: 'Test text test value test2'
|
|
16
|
+
*/
|
|
17
|
+
const template = (templateText) => {
|
|
18
|
+
return (data) => templateText.replace(/{{(\w+?)}}/g, (match, key) => key in data ? data[key] : match);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export { template };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { trimEnd } from './trimEnd';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
const trimEnd = (text, char = '') => {
|
|
7
|
+
let endIndex = text.length - 1;
|
|
8
|
+
while (endIndex >= 0 && text[endIndex] === char) {
|
|
9
|
+
endIndex--;
|
|
10
|
+
}
|
|
11
|
+
return text.slice(0, endIndex + 1);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
exports.trimEnd = trimEnd;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const trimEnd: (text: string, char?: string) => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { uniq } from './uniq';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Removes duplicate elements from an array.
|
|
8
|
+
*
|
|
9
|
+
* @param {T[]} array - The array to filter for unique elements.
|
|
10
|
+
* @template T - The type of elements in the array.
|
|
11
|
+
* @returns {T[]} A new array containing only the unique elements of the given array.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // returns [1, 2, 3, 4, 5]
|
|
15
|
+
* uniq([1, 2, 2, 3, 4, 4, 5]);
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // returns ['apple', 'banana', 'orange']
|
|
19
|
+
* uniq(['apple', 'banana', 'apple', 'orange']);
|
|
20
|
+
*
|
|
21
|
+
*/
|
|
22
|
+
const uniq = (array) => {
|
|
23
|
+
const seen = new Set();
|
|
24
|
+
return array.filter((item) => {
|
|
25
|
+
if (seen.has(item)) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
seen.add(item);
|
|
29
|
+
return true;
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
exports.uniq = uniq;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes duplicate elements from an array.
|
|
3
|
+
*
|
|
4
|
+
* @param {T[]} array - The array to filter for unique elements.
|
|
5
|
+
* @template T - The type of elements in the array.
|
|
6
|
+
* @returns {T[]} A new array containing only the unique elements of the given array.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* // returns [1, 2, 3, 4, 5]
|
|
10
|
+
* uniq([1, 2, 2, 3, 4, 4, 5]);
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // returns ['apple', 'banana', 'orange']
|
|
14
|
+
* uniq(['apple', 'banana', 'apple', 'orange']);
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
export declare const uniq: <T>(array: T[]) => T[];
|
package/src/uniq/uniq.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/**
|
|
3
|
+
* Removes duplicate elements from an array.
|
|
4
|
+
*
|
|
5
|
+
* @param {T[]} array - The array to filter for unique elements.
|
|
6
|
+
* @template T - The type of elements in the array.
|
|
7
|
+
* @returns {T[]} A new array containing only the unique elements of the given array.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // returns [1, 2, 3, 4, 5]
|
|
11
|
+
* uniq([1, 2, 2, 3, 4, 4, 5]);
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // returns ['apple', 'banana', 'orange']
|
|
15
|
+
* uniq(['apple', 'banana', 'apple', 'orange']);
|
|
16
|
+
*
|
|
17
|
+
*/
|
|
18
|
+
const uniq = (array) => {
|
|
19
|
+
const seen = new Set();
|
|
20
|
+
return array.filter((item) => {
|
|
21
|
+
if (seen.has(item)) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
seen.add(item);
|
|
25
|
+
return true;
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { uniq };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { wrapMethodWithCallback } from './wrapMethodWithCallback';
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Wraps an existing method of an object with a callback function. This allows for additional
|
|
8
|
+
* processing or behavior to be executed around the original method. The original method is
|
|
9
|
+
* called within the callback, and its parameters and return value can be accessed and/or modified.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of the object whose method is being wrapped.
|
|
12
|
+
* @template K - The key type of the method within the object.
|
|
13
|
+
* @template F - The type of the function, inferred from the method of the object.
|
|
14
|
+
*
|
|
15
|
+
* @param {T} obj - The object containing the method to be wrapped.
|
|
16
|
+
* @param {K} key - The key of the method in the object to be wrapped.
|
|
17
|
+
* @param {(original: F, ...args: Parameters<F>) => ReturnType<F>} callback - The callback
|
|
18
|
+
* function to wrap around the original method. It receive1`s the original method and its
|
|
19
|
+
* parameters as arguments.
|
|
20
|
+
*
|
|
21
|
+
* @returns {void} - The function does not return a value; it modifies the method of the object in place.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* // Example usage
|
|
25
|
+
* const myObject = {
|
|
26
|
+
* greet(name) {
|
|
27
|
+
* return `Hello, ${name}!`;
|
|
28
|
+
* }
|
|
29
|
+
* };
|
|
30
|
+
*
|
|
31
|
+
* wrapMethodWithCallback(myObject, 'greet', (original, ...args) => {
|
|
32
|
+
* console.log('Greet method called with args:', args);
|
|
33
|
+
* return original(...args).toUpperCase();
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* console.log(myObject.greet('Alice')); // Logs: 'Greet method called with args: ["Alice"]'
|
|
37
|
+
* // Returns: 'HELLO, ALICE!'
|
|
38
|
+
*/
|
|
39
|
+
const wrapMethodWithCallback = (obj, key, callback) => {
|
|
40
|
+
const original = obj[key];
|
|
41
|
+
const boundOriginal = original.bind(obj);
|
|
42
|
+
Object.assign(obj, {
|
|
43
|
+
[key]: (...args) => callback(boundOriginal, ...args),
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
exports.wrapMethodWithCallback = wrapMethodWithCallback;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
type Method = (...args: any[]) => any;
|
|
2
|
+
/**
|
|
3
|
+
* Wraps an existing method of an object with a callback function. This allows for additional
|
|
4
|
+
* processing or behavior to be executed around the original method. The original method is
|
|
5
|
+
* called within the callback, and its parameters and return value can be accessed and/or modified.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The type of the object whose method is being wrapped.
|
|
8
|
+
* @template K - The key type of the method within the object.
|
|
9
|
+
* @template F - The type of the function, inferred from the method of the object.
|
|
10
|
+
*
|
|
11
|
+
* @param {T} obj - The object containing the method to be wrapped.
|
|
12
|
+
* @param {K} key - The key of the method in the object to be wrapped.
|
|
13
|
+
* @param {(original: F, ...args: Parameters<F>) => ReturnType<F>} callback - The callback
|
|
14
|
+
* function to wrap around the original method. It receive1`s the original method and its
|
|
15
|
+
* parameters as arguments.
|
|
16
|
+
*
|
|
17
|
+
* @returns {void} - The function does not return a value; it modifies the method of the object in place.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // Example usage
|
|
21
|
+
* const myObject = {
|
|
22
|
+
* greet(name) {
|
|
23
|
+
* return `Hello, ${name}!`;
|
|
24
|
+
* }
|
|
25
|
+
* };
|
|
26
|
+
*
|
|
27
|
+
* wrapMethodWithCallback(myObject, 'greet', (original, ...args) => {
|
|
28
|
+
* console.log('Greet method called with args:', args);
|
|
29
|
+
* return original(...args).toUpperCase();
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* console.log(myObject.greet('Alice')); // Logs: 'Greet method called with args: ["Alice"]'
|
|
33
|
+
* // Returns: 'HELLO, ALICE!'
|
|
34
|
+
*/
|
|
35
|
+
export declare const wrapMethodWithCallback: <T extends object, K extends { [P in keyof T]: T[P] extends Method ? P : never; }[keyof T], F extends Method = T[K] extends Method ? T[K] : never>(obj: T, key: K, callback: (original: F, ...args: Parameters<F>) => ReturnType<F>) => void;
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/**
|
|
3
|
+
* Wraps an existing method of an object with a callback function. This allows for additional
|
|
4
|
+
* processing or behavior to be executed around the original method. The original method is
|
|
5
|
+
* called within the callback, and its parameters and return value can be accessed and/or modified.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The type of the object whose method is being wrapped.
|
|
8
|
+
* @template K - The key type of the method within the object.
|
|
9
|
+
* @template F - The type of the function, inferred from the method of the object.
|
|
10
|
+
*
|
|
11
|
+
* @param {T} obj - The object containing the method to be wrapped.
|
|
12
|
+
* @param {K} key - The key of the method in the object to be wrapped.
|
|
13
|
+
* @param {(original: F, ...args: Parameters<F>) => ReturnType<F>} callback - The callback
|
|
14
|
+
* function to wrap around the original method. It receive1`s the original method and its
|
|
15
|
+
* parameters as arguments.
|
|
16
|
+
*
|
|
17
|
+
* @returns {void} - The function does not return a value; it modifies the method of the object in place.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // Example usage
|
|
21
|
+
* const myObject = {
|
|
22
|
+
* greet(name) {
|
|
23
|
+
* return `Hello, ${name}!`;
|
|
24
|
+
* }
|
|
25
|
+
* };
|
|
26
|
+
*
|
|
27
|
+
* wrapMethodWithCallback(myObject, 'greet', (original, ...args) => {
|
|
28
|
+
* console.log('Greet method called with args:', args);
|
|
29
|
+
* return original(...args).toUpperCase();
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* console.log(myObject.greet('Alice')); // Logs: 'Greet method called with args: ["Alice"]'
|
|
33
|
+
* // Returns: 'HELLO, ALICE!'
|
|
34
|
+
*/
|
|
35
|
+
const wrapMethodWithCallback = (obj, key, callback) => {
|
|
36
|
+
const original = obj[key];
|
|
37
|
+
const boundOriginal = original.bind(obj);
|
|
38
|
+
Object.assign(obj, {
|
|
39
|
+
[key]: (...args) => callback(boundOriginal, ...args),
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export { wrapMethodWithCallback };
|