From 3618aa63806c470eee0be780f6afee243fcc0588 Mon Sep 17 00:00:00 2001 From: Abhishek Chauhan Date: Sat, 6 Jun 2026 22:54:33 -0500 Subject: [PATCH] fix: treat null mappings from extend() as character removal 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 #177 --- slugify.js | 2 ++ test/slugify.js | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/slugify.js b/slugify.js index f5a6a1e..0befa5e 100644 --- a/slugify.js +++ b/slugify.js @@ -36,6 +36,8 @@ var appendChar = locale[ch]; if (appendChar === undefined) appendChar = charMap[ch]; if (appendChar === undefined) appendChar = ch; + // A null mapping (e.g. extend({ '%': null })) removes the character. + if (appendChar === null) appendChar = ''; if (appendChar === replacement) appendChar = ' '; return result + appendChar // remove not allowed characters diff --git a/test/slugify.js b/test/slugify.js index e2581d2..0635c26 100644 --- a/test/slugify.js +++ b/test/slugify.js @@ -285,4 +285,16 @@ describe('slugify', () => { delete require.cache[require.resolve('../')] slugify = require('../') }) + + it('removes characters mapped to null via extend()', () => { + // https://github.com/simov/slugify/issues/177 + slugify.extend({ '|': null, '%': null, $: null }) + t.equal(slugify('100%'), '100') + t.equal(slugify('a|b$c'), 'abc') + + delete require.cache[require.resolve('../')] + slugify = require('../') + + t.equal(slugify('100%'), '100percent') + }) })