Skip to content

🦄 Like polyfill but with pony pureness

Notifications You must be signed in to change notification settings

mathiasbynens/ponyfill

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Ponyfill

Like polyfill but with pony pureness




Pony pureness, really?

While polyfills are naughty, ponyfills are pure, just like ponies.

How are ponyfills better than polyfills?

A polyfill is code that adds missing functionality by monkey patching an API. Unfortunately, it usually globally patches built-ins, which affects all code running in the environment. This is especially problematic when a polyfill is not fully spec compliant (which in some cases is impossible), as it could cause very hard to debug bugs and inconsistencies. Or when the spec for a new feature changes and your code depends on behavior that a module somewhere else in the dependency tree polyfills differently. In general, you should not modify API's you don't own.

A ponyfill, in contrast, doesn't monkey patch anything, but instead exports the functionality as a normal module, so you can use it locally without affecting other code.

tl;dr; Polyfills are naughty as they patch native APIs, while ponyfills are pure and don't affect the environment.

Polyfill

Number.isNaN = Number.isNaN || function (value) {
	return value !== value;
}
require('is-nan-polyfill');

Number.isNan(5);

Ponyfill

module.exports = Number.isNaN || function (value) {
	return value !== value;
}
var isNanPonyfill = require('is-nan-ponyfill');

isNanPonyfill(5);

Only use the native API in your ponyfill (the Number.isNaN || part) when the spec is stable, to ensure it doesn't have differing behavior depending on the environment.

Where can I find ponyfills?

Search npm.

Resources

License

CC0

To the extent possible under law, Sindre Sorhus has waived all copyright and related or neighboring rights to this work.

Header based on work by Mary Winkler.

About

🦄 Like polyfill but with pony pureness

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published