Sometimes you need the wpautop function from WordPress because you’re working with WXR files in javascript.
Lifted shamelessly from WordPress core.
I don’t support this code. If there are bugs then open an issue with WordPress core; if this code is out of date then make a PR and I’ll be happy to update this package.