string.prototype.replaceall
Version:
Spec-compliant polyfill for String.prototype.replaceAll ESnext proposal
62 lines (46 loc) • 2.98 kB
Markdown
for String.prototype.replaceAll. Invoke its "shim" method to shim `String.prototype.replaceAll` if it is unavailable or noncompliant.
This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment, and complies with the [proposed spec](https://github.com/tc39/proposal-string-replaceall).
Most common usage:
```js
const assert = require('assert');
const replaceAll = require('string.prototype.replaceall');
const str = 'aabc';
// replaceAll and replace are the same, when given a global regex to replace
assert.equal(replaceAll(str, /a/g, 'z'), str.replace(/a/g, 'z'));
// replace, with a string, replaces once
assert.equal(str.replace('a', 'z'), 'zabc');
// replaceAll, with a string, replaces all
assert.equal(replaceAll(str, 'a', 'z'), 'zzbc');
replaceAll.shim(); // will be a no-op if not needed
// replaceAll and replace are the same, when given a global regex to replace
assert.equal(str.replaceAll(/a/g, 'z'), str.replace(/a/g, 'z'));
// replace, with a string, replaces once
assert.equal(str.replace('a', 'z'), 'zabc');
// replaceAll, with a string, replaces all
assert.equal(str.replaceAll('a', 'z'), 'zzbc');
```
Simply clone the repo, `npm install`, and run `npm test`
[ ]: https://npmjs.com/package/string.prototype.replaceall
[ ]: https://versionbadg.es/es-shims/String.prototype.replaceAll.svg
[ ]: https://david-dm.org/es-shims/String.prototype.replaceAll.svg
[ ]: https://david-dm.org/es-shims/String.prototype.replaceAll
[ ]: https://david-dm.org/es-shims/String.prototype.replaceAll/dev-status.svg
[ ]: https://david-dm.org/es-shims/String.prototype.replaceAll#info=devDependencies
[ ]: https://nodei.co/npm/string.prototype.replaceall.png?downloads=true&stars=true
[ ]: https://img.shields.io/npm/l/string.prototype.replaceall.svg
[ ]: LICENSE
[ ]: https://img.shields.io/npm/dm/string.prototype.replaceall.svg
[ ]: https://npm-stat.com/charts.html?package=string.prototype.replaceall
[ ]: https://codecov.io/gh/es-shims/string.prototype.replaceall/branch/main/graphs/badge.svg
[ ]: https://app.codecov.io/gh/es-shims/string.prototype.replaceall/
[ ]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/string.prototype.replaceall
[ ]: https://github.com/es-shims/string.prototype.replacealstring.prototype.replaceall
[![github actions][actions-image]][actions-url]
[![coverage][codecov-image]][codecov-url]
[![dependency status][deps-svg]][deps-url]
[![dev dependency status][dev-deps-svg]][dev-deps-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]
[![npm badge][npm-badge-png]][package-url]
ES Proposal spec-compliant shim