fix: preserve a multi-character replacement so slug stays idempotent#553
Open
spokodev wants to merge 1 commit into
Open
fix: preserve a multi-character replacement so slug stays idempotent#553spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
slug already preserves a replacement string that is already present in the
input (so that re-slugifying an existing slug is a no-op), but the check
only worked for a single-character replacement: it tested `char.includes`
on one code unit, which can never match a replacement longer than one
character. A multi-character replacement was therefore stripped as
disallowed punctuation:
slug('foo bar', { replacement: '__' }) // 'foo__bar'
slug(slug('foo bar', { replacement: '__' }), ...) // 'foobar' (separators lost)
Detect the replacement with a fixed-length look-ahead (like the existing
multicharmap handling), advancing past it, so any-length replacement is
preserved and slug is idempotent. Single-character replacements and the
empty replacement are unaffected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
slugpreserves a replacement string that is already present in the input, so that re-slugifying an existing slug is a no-op (idempotent). That preservation only works for a single-character replacement, because it testschar.includes(opts.replacement)against one code unit at a time, which can never match a replacement longer than one character.As a result a multi-character replacement is treated as disallowed punctuation and stripped:
A single-character replacement round-trips correctly (
'_'→ idempotent), so this is an inconsistency rather than intended behavior. The README documentsreplacementas an arbitrary string with no single-character restriction.Fix
Detect the replacement with a fixed-length look-ahead and advance past it, mirroring the existing
multicharmaphandling (i += len - 1). This preserves a replacement of any length while leaving single-character and empty replacements unchanged.Test
Added an idempotence assertion for a multi-character replacement. It fails before the change and passes after. The full test suite is green and
c8 --100coverage is preserved (100%).