Repository navigation
The compatibility promise. Every call with three strings (or String objects) and an integer position from 0 to the text's length returns exactly what 1.0.4 returned, and require('replace-string-at-position') still returns the function. The test suite checks this against 71 answers recorded from the published 1.0.4, on both builds and every supported Node line. Every other call now throws. Those calls are listed under Changed. Most of them returned damaged text, but a few happened to give a sensible answer, and those throw now too: a number as newString (('item #0', '0', 5, 6) gave 'item #5'), a Number object or true as the position, an array such as ['b'] as sourceString. Convert such arguments with String() or Number() first. A String object is read by its own string value: an overridden valueOf, Symbol.toPrimitive or substring on it is ignored, and a Proxy is refused, where 1.0.4 called them.
Changed (breaking)
- Needs Node 20 or later. The package has an
exportsmap, so deep imports such asreplace-string-at-position/index.jsno longer resolve; import the package by its name. - A missing or non-number
positionthrows aTypeError. 1.0.4 returned the text twice for a missing position (('abc', 'b', 'X')gave'abcXabc'), and joined a string position as text ('1'gave'aX'for'abcdef'). Booleans,null, arrays, BigInts andNumberobjects are refused as well. - A negative,
NaNor fractionalpositionthrows aRangeError. 1.0.4 spliced negative andNaNpositions at the start and removed the wrong number of characters, and it truncated fractions. - A
positiongreater than the text's length throws aRangeError. 1.0.4 appended the new text at the end. - Every text argument must be a string or a
Stringobject, otherwise aTypeErrornames the argument. 1.0.4 putnull,undefined, numbers and objects into the result as text ('anullcdef'), read thelengthof arrays and array-likes, and failed with the engine's own error for anullor non-string first argument. - The command-line tool that the old README described is gone. It was never installed, because the package had no
binentry.
Added
- ES module build with a default export and the named export
replaceStringAtPosition. - The CommonJS function also carries
.defaultand.replaceStringAtPosition, both pointing to itself. - TypeScript declarations for both builds.
- Runs in browsers, Bun, Deno and workers: the package uses no Node or DOM APIs.
- Published from GitHub Actions through npm trusted publishing, with provenance.
Kept
- The text at the position is not compared with
sourceString; only its length counts, and asourceStringthat runs past the end removes the rest of the text. - Positions and lengths count UTF-16 code units, as
String.prototype.slicedoes, so a position inside an emoji splits it. - Extra arguments are ignored.