Spec-compliant polyfill for String.prototype.replaceAll ESnext proposal
The npm package "string.prototype.replaceall" offers a robust solution for developers needing a spec-compliant polyfill that aligns with the ESnext proposal for String.prototype.replaceAll. This module enhances JavaScript's string manipulation capabilities by allowing developers to seamlessly replace all occurrences of a substring with a new string, without the need for regular expressions or complex looping mechanisms. The utility of this package is crucial for maintaining clean and efficient code, particularly in applications where string data requires standardized formatting or dynamic content replacement across various segments of an application.
To integrate "string.prototype.replaceall" into your project, simply execute the command `npm install string.prototype.replaceall` in your project's root directory. This installation process adds the package to your node_modules folder and updates your project's package.json dependencies, ensuring that your application remains consistent with the latest standards proposed for JavaScript string manipulation. By incorporating this package, developers can avoid the pitfalls of incomplete or incorrect implementations of string replacement functions, leading to more reliable and maintainable codebases.
The "string.prototype.replaceall" module is not only a practical addition to any developer's toolkit but also a testament to the evolving JavaScript ecosystem's commitment to backward compatibility and future readiness. As web and application development grows increasingly complex, having access to tools that simplify and standardize common tasks like string replacement is invaluable. This package ensures that developers can focus on creating innovative features and enhancing user experience, rather than getting bogged down by the intricacies of basic string operations.
Core dependencies of this npm package and its dev dependencies.
call-bind, define-properties, es-abstract, es-errors, es-object-atoms, get-intrinsic, has-symbols, is-regex, @es-shims/api, @ljharb/eslint-config, aud, auto-changelog, es5-shim, es6-shim, eslint, evalmd, functions-have-names, in-publish, npmignore, nyc, safe-publish-latest, tape
A README file for the string.prototype.replaceall code repository. View Code
ES Proposal spec-compliant shim 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 interface. It works in an ES3-supported environment, and complies with the proposed spec.
Most common usage:
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