Created
August 12, 2021 08:36
-
-
Save david50407/6bf0c7d8d915709ccf7cea1cf8adbbfd to your computer and use it in GitHub Desktop.
util.polyfills
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export {} | |
declare global { | |
interface Object { | |
/** | |
* Yields this to the `interceptor`, and returns this. | |
* The primary purpose of this method is to "tap into" a method chain, | |
* in order to perform operations on intermediate results within the chain. | |
* | |
* @returns this | |
* | |
* @example | |
* Chains methods and prints out each step: | |
* ```ts | |
* [1, 2, 3].tap(console.log) | |
* .map(x => x * 2) | |
* .tap(console.log) | |
* .map(x => x + 1) | |
* ``` | |
* | |
* @experimental | |
*/ | |
tap<T>(this: T, interceptor: (this: T, obj: T) => void): T | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
if (!('tap' in Object.prototype)) { | |
// eslint-disable-next-line no-extend-native | |
Object.defineProperty(Object.prototype, 'tap', { | |
value: function(interceptor) { | |
interceptor.call(this, this); | |
return this; | |
} | |
}); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment