Skip to content

Instantly share code, notes, and snippets.

@david50407
Created August 12, 2021 08:36
Show Gist options
  • Save david50407/6bf0c7d8d915709ccf7cea1cf8adbbfd to your computer and use it in GitHub Desktop.
Save david50407/6bf0c7d8d915709ccf7cea1cf8adbbfd to your computer and use it in GitHub Desktop.
util.polyfills
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
}
}
(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