diff --git a/core/badge-urls/path-helpers.js b/core/badge-urls/path-helpers.js index 81c7222fe4..b7b3bded44 100644 --- a/core/badge-urls/path-helpers.js +++ b/core/badge-urls/path-helpers.js @@ -5,11 +5,8 @@ function escapeFormat(t) { return ( t - // Inline single underscore. - .replace(/([^_])_([^_])/g, '$1 $2') - // Leading or trailing underscore. - .replace(/([^_])_$/, '$1 ') - .replace(/^_([^_])/, ' $1') + // Single underscore. + .replace(/(^|[^_])((?:__)*)_(?!_)/g, '$1$2 ') // Double underscore and double dash. .replace(/__/g, '_') .replace(/--/g, '-') diff --git a/core/badge-urls/path-helpers.spec.js b/core/badge-urls/path-helpers.spec.js new file mode 100644 index 0000000000..ef7a0068e1 --- /dev/null +++ b/core/badge-urls/path-helpers.spec.js @@ -0,0 +1,21 @@ +'use strict' + +const { test, given } = require('sazerac') +const { escapeFormat } = require('./path-helpers') + +describe('Badge URL helper functions', function () { + test(escapeFormat, () => { + given('_single leading underscore').expect(' single leading underscore') + given('single trailing underscore_').expect('single trailing underscore ') + given('__double leading underscores').expect('_double leading underscores') + given('double trailing underscores__').expect( + 'double trailing underscores_' + ) + given('treble___underscores').expect('treble_ underscores') + given('fourfold____underscores').expect('fourfold__underscores') + given('double--dashes').expect('double-dashes') + given('treble---dashes').expect('treble--dashes') + given('fourfold----dashes').expect('fourfold--dashes') + given('once_in_a_blue--moon').expect('once in a blue-moon') + }) +})