This merges the `node-8` branch. The heavy lift was by @Daniel15 with refactoring from me and a patch by @RedSparr0w. * New API for registering services (#963) * Disable Node 6 tests on node-8 branch (#1423) * BaseService: Factor out methods _regex and _namedParamsForMatch (#1425) - Adjust test grouping - Rename data -> queryParams, text -> message * BaseService tests: Use Chai (#1450) * BaseService: make serviceData and badgeData explicit and declarative (#1451) * fix isValidStyle test (#1544) * Run tests in Node 9, not Node 6 (#1543)
119 lines
2.4 KiB
JavaScript
119 lines
2.4 KiB
JavaScript
'use strict';
|
|
|
|
const logos = require('./load-logos')();
|
|
|
|
function toArray(val) {
|
|
if (val === undefined) {
|
|
return [];
|
|
} else if (Object(val) instanceof Array) {
|
|
return val;
|
|
} else {
|
|
return [val];
|
|
}
|
|
}
|
|
|
|
function isDataUri(s) {
|
|
return s !== undefined && /^(data:)([^;]+);([^,]+),(.+)$/.test(s);
|
|
}
|
|
|
|
function prependPrefix(s, prefix) {
|
|
if (s === undefined) {
|
|
return undefined;
|
|
}
|
|
|
|
s = '' + s;
|
|
|
|
if (s.startsWith(prefix)) {
|
|
return s;
|
|
} else {
|
|
return prefix + s;
|
|
}
|
|
}
|
|
|
|
function isSixHex (s){
|
|
return s !== undefined && /^[0-9a-fA-F]{6}$/.test(s);
|
|
}
|
|
|
|
function makeColor(color) {
|
|
if (isSixHex(color)) {
|
|
return '#' + color;
|
|
} else {
|
|
return color;
|
|
}
|
|
}
|
|
|
|
function makeColorB(defaultColor, overrides) {
|
|
return makeColor(overrides.colorB || defaultColor);
|
|
}
|
|
|
|
function setBadgeColor(badgeData, color) {
|
|
if (isSixHex(color)) {
|
|
badgeData.colorB = '#' + color;
|
|
delete badgeData.colorscheme;
|
|
} else {
|
|
badgeData.colorscheme = color;
|
|
delete badgeData.colorB;
|
|
}
|
|
return badgeData;
|
|
}
|
|
|
|
function makeLabel(defaultLabel, overrides) {
|
|
return overrides.label || defaultLabel;
|
|
}
|
|
|
|
function makeLogo(defaultNamedLogo, overrides) {
|
|
if (overrides.logo === undefined){
|
|
return logos[defaultNamedLogo];
|
|
}
|
|
|
|
// +'s are replaced with spaces when used in query params, this returns them to +'s, then removes remaining whitespace - #1546
|
|
const maybeDataUri = prependPrefix(overrides.logo, 'data:').replace(/ /g, '+').replace(/\s/g, '');
|
|
|
|
if (isDataUri(maybeDataUri)) {
|
|
return maybeDataUri;
|
|
} else {
|
|
return logos[overrides.logo];
|
|
}
|
|
}
|
|
|
|
// Generate the initial badge data. Pass the URL query parameters, which
|
|
// override the default label.
|
|
//
|
|
// The following parameters are supported:
|
|
//
|
|
// - label
|
|
// - style
|
|
// - logo
|
|
// - logoWidth
|
|
// - link
|
|
// - colorA
|
|
// - colorB
|
|
// - maxAge
|
|
//
|
|
// Note: maxAge is handled by cache(), not this function.
|
|
function makeBadgeData(defaultLabel, overrides) {
|
|
return {
|
|
text: [makeLabel(defaultLabel, overrides), 'n/a'],
|
|
colorscheme: 'lightgrey',
|
|
template: overrides.style,
|
|
logo: makeLogo(undefined, overrides),
|
|
logoWidth: +overrides.logoWidth,
|
|
links: toArray(overrides.link),
|
|
colorA: makeColor(overrides.colorA),
|
|
colorB: makeColor(overrides.colorB),
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
toArray,
|
|
prependPrefix,
|
|
isDataUri,
|
|
isSixHex,
|
|
makeLabel,
|
|
makeLogo,
|
|
makeBadgeData,
|
|
makeColor,
|
|
makeColorB,
|
|
setBadgeColor
|
|
};
|