fix: treat null mappings from extend() as character removal#207
Open
abhu85 wants to merge 1 commit into
Open
Conversation
extend({ '%': null }) worked in 1.6.5 to drop a character (e.g. "100%"
-> "100"), but since 1.6.6 it throws "TypeError: Cannot read properties
of null (reading 'replace')": the charMap lookup yields null, the two
`=== undefined` fallbacks don't catch it, and null.replace() throws.
Coerce a null mapping to an empty string so the character is removed,
restoring the earlier behavior and preventing the crash.
Fixes simov#177
Collaborator
|
This change seems OK to me, but also as an FYI: If you want to switch to the import slug from 'slug';
import slugify from 'slugify';
// Default behavior of slug() is already what you want.
console.log(slug('100%')); // 100
console.log(slugify('100%')); // 100percent
// Extend works the same way for both libraries.
slug.extend({ '%': ' percent' })
slugify.extend({ '%': ' percent' })
console.log(slug('100%')); // 100-percent
console.log(slugify('100%')); // 100-percent
// Null currently throws in slugify, but works as expected in slug.
slug.extend({ '%': null })
slugify.extend({ '%': null })
console.log(slug('100%')); // 100
console.log(slugify('100%')); // Throws TypeErrorAll that said, I like this code change. For selfish reasons, I'm in favor of anything that brings |
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.
Fixes #177
Problem
extend({ '%': null })was used in 1.6.5 to make a character drop out of the slug (e.g."100%"→"100"). Since 1.6.6 the same call throws:In the reduce that builds the slug, the lookup resolves to
null:Fix
Coerce a
nullmapping to an empty string, so the character is removed — restoring the pre-1.6.6 behavior and preventing the crash:undefinedmappings are unchanged (they still fall back to the original character), and string/empty-string mappings are unaffected.Tests
Added a regression test (mirrors the existing
extend()tests, with the usualrequire.cachereset):Verified it fails without the fix (the reported
TypeError) and passes with it. Full suite: 46 passing.