I went down a rabbit hole while trying to untangle the bug in the dockbit and bitrise examples https://github.com/badges/shields/pull/2234#pullrequestreview-169997546. The URL generation code is spaghetti-like, with functions, many of which I wrote, with opaque names, doing similar but not identical things, and making slightly incompatible assumptions about the way query strings are handled. I got a bit lost and need to take a step back. Meanwhile, this is a small piece of work I did that’s worth keeping. It doesn’t scratch the surface of the tangle, but it does remove a bit of duplication. It also makes a minor stylistic ES6 change in the handling of default arguments. Ref: #2027
32 lines
646 B
JavaScript
32 lines
646 B
JavaScript
'use strict'
|
|
|
|
const queryString = require('query-string')
|
|
|
|
function encodeField(s) {
|
|
return encodeURIComponent(s.replace(/-/g, '--').replace(/_/g, '__'))
|
|
}
|
|
|
|
function staticBadgeUrl({
|
|
baseUrl,
|
|
label,
|
|
message,
|
|
color = 'lightgray',
|
|
style,
|
|
format = 'svg',
|
|
}) {
|
|
if (!label || !message) {
|
|
throw Error('label and message are required')
|
|
}
|
|
const path = [label, message, color].map(encodeField).join('-')
|
|
const outQueryString = queryString.stringify({
|
|
style,
|
|
})
|
|
const suffix = outQueryString ? `?${outQueryString}` : ''
|
|
return `/badge/${path}.${format}${suffix}`
|
|
}
|
|
|
|
module.exports = {
|
|
encodeField,
|
|
staticBadgeUrl,
|
|
}
|