fix: default allowedChars when undefined in slugify - #252
Open
spokodev wants to merge 1 commit into
Open
Conversation
slugify() merges options with `{ ...this.options, ...opts }`, so passing
`allowedChars: undefined` (e.g. spreading optional config) overrides the
default with `undefined`, which is then coerced into the character-class
template literal as the string "undefined":
slugify('Hello World', { allowedChars: undefined }) // => 'e-d'
Only the letters of "undefined" survive the `[^undefined]+` filter. The
adjacent `separator` and `ignore` lookups already fall back when undefined
(`?? '-'`, `?? []`); allowedChars was missing the same guard. Add it.
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
slugify()merges options withdeepClone({ ...this.options, ...opts }), so an explicitly-passedallowedChars: undefinedoverrides the default withundefined. Thatundefinedis then interpolated into the character-class template literal as the string"undefined":The
[^${opt.allowedChars}]+regex becomes[^undefined]+, so only the letters of the word undefined (u n d e f i) survive —Hello World→e-d.This is easy to hit in real code, where options are often spread from optional config:
Fix
The adjacent
separatorandignorelookups on the same call already guard againstundefined(opt.separator ?? '-',opt.ignore ?? []);allowedCharswas simply missing the same fallback. Add it:??covers bothundefinedandnull; an explicitallowedChars: ''is left untouched (still means "allow nothing").Tests
Added a case next to the existing
separator: undefined/ignore: undefinedcoverage, assertingallowedCharsundefined/null falls back to the default. It fails before the change (expected 'e-d' to be 'hello-world') and passes after. Full suite green (86/86),biome checkclean.