Sugar icon indicating copy to clipboard operation
Sugar copied to clipboard

Simplify polyfills to work like other Sugar methods.

Open andrewplummer opened this issue 7 years ago • 1 comments

Requiring or including Sugar polyfills currently immediately apply the polyfill to the global object. For example, the es6 module (which is included in the default package) immediately applies the polyfill for Array#find and others.

Although polyfills are safer to apply to the global object than other methods, considering that Sugar has made concessions about not modifying the global scope by default for other methods, as well as the concept of ponyfills becoming a thing, it may be a good time to reconsider making an exception for polyfills. This would also have the advantage of being simpler to handle in the code, and simpler to grasp for users as well. Polyfills would of course use native methods when they are available.

One potential issue here is that Sugar provides "enhanced" methods, such as array methods like every, which allow shortcuts like every(1) etc. Since these "ponyfills" could be called directly through the global object (i.e. Sugar.Array.every) it would become necessary to create a distinction between the ponyfill and the enhanced method signatures.

andrewplummer avatar Jan 15 '17 15:01 andrewplummer

One idea to handle enhancements is to have an enhance method on both the Sugar global and namespaces like Sugar.Array. This could accept a prefix option that would allow enhanced methods to exist separate to polyfills/ponyfills:

Sugar.Array.enhance({
  prefix: 'enhanced'
});
Sugar.Array.every(...); // The ponyfill method
Sugar.Array.enhancedEvery(...); // The enhanced method

Without the prefix, the method would simply be overwritten for people who prefer that. This is nice because it would maintain parity with enhance (#562).

andrewplummer avatar Jan 16 '17 14:01 andrewplummer