Skip to content

Instantly share code, notes, and snippets.

@jaredrethman
Last active October 27, 2019 01:08
Show Gist options
  • Save jaredrethman/65268af9654b3e43e1d01a068bcc9347 to your computer and use it in GitHub Desktop.
Save jaredrethman/65268af9654b3e43e1d01a068bcc9347 to your computer and use it in GitHub Desktop.
Simple ES6+ Polyfill test. Transpile using babel.
window.POLYFILL_ALL_THE_THINGS = {
reduce: ['foo', 'bar', 'baz'].reduce( ( acc, v ) => {
return { ...acc, [`${v}_key`]: `${v}_value` };
}, [] ),
reduceRight: ['foo', 'bar', 'baz'].reduceRight( ( acc, v ) => {
return { ...acc, [`${v}_key`]: `${v}_value` };
}, {} ),
map: ['foo', 'bar', 'baz'].map( str => {
return str += ' 1';
} ),
filter: ['foo', 'bar', 'baz'].filter( str => {
return str.includes( 'ba' );
} ),
promise: new Promise( ( resolve ) => {
setTimeout( () => {
resolve( 'foo' );
}, 300 );
} ),
};
const {
reduce,
reduceRight,
filter,
map,
promise,
} = window.POLYFILL_ALL_THE_THINGS;
const consoleStyle = 'background:#ffcc3d;color:#ffffff;font-size:16px';
console.log( '%cnew Promise():', consoleStyle );
promise.then( r => {
console.log( `Promise resolved: ${r}` );
} );
console.log( '%carray.reduce():', consoleStyle );
console.log( reduce );
console.log( '%carray.reduceRight():', consoleStyle );
console.log( reduceRight );
console.log( '%carray.filter():', consoleStyle );
console.log( filter );
console.log( '%carray.map():', consoleStyle );
console.log( map );
/*
* Expected output - https://tinyurl.com/y5dtn8u9
* > 1%
* last 2 versions
* Firefox ESR
* IE 11
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment