Files
shields/frontend/lib/badge-url.js
Paul Melnikow 7a664ca3e8 Run prettier (#1866)
Merging this separately so the commit with the tooling change is readable. This is a follow-on to #1167 which turned prettier on.
2018-08-08 17:57:14 -04:00

55 lines
1.3 KiB
JavaScript

import resolveUrl from './resolve-url'
export default function resolveBadgeUrl(url, baseUrl, options) {
const { longCache, style, queryParams: inQueryParams } = options || {}
const outQueryParams = Object.assign({}, inQueryParams)
if (longCache) {
outQueryParams.maxAge = '2592000'
}
if (style) {
outQueryParams.style = style
}
return resolveUrl(url, baseUrl, outQueryParams)
}
export function encodeField(s) {
return encodeURIComponent(s.replace(/-/g, '--').replace(/_/g, '__'))
}
export function staticBadgeUrl(baseUrl, subject, status, color, options) {
const path = [subject, status, color].map(encodeField).join('-')
return resolveUrl(`/badge/${path}.svg`, baseUrl, options)
}
// Options can include: { prefix, suffix, color, longCache, style, queryParams }
export function dynamicBadgeUrl(
baseUrl,
datatype,
label,
dataUrl,
query,
options = {}
) {
const { prefix, suffix, color, queryParams = {}, ...rest } = options
Object.assign(queryParams, {
label,
url: dataUrl,
query,
})
if (color) {
queryParams.colorB = color
}
if (prefix) {
queryParams.prefix = prefix
}
if (suffix) {
queryParams.suffix = suffix
}
const outOptions = Object.assign({ queryParams }, rest)
return resolveBadgeUrl(`/badge/dynamic/${datatype}.svg`, baseUrl, outOptions)
}