Refactor badge color functions (#2742)
- Replace the idea of color schemes with the idea of named colors (since none of our colorschemes have used `colorA`)
- Pass through the normalized color to `_shields_test` to harmonize with BaseService and simplify testing
- Update service tests
- Move responsibility for color generation into the npm package
- Remove several color helper functions and their tests
- Update gh-badge public API to accept `color` and `labelColor`
This is a precursor to refactoring some of the logo code for #2473.
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
'use strict'
|
||||
|
||||
const isCSSColor = require('is-css-color')
|
||||
const { toSvgColor } = require('../gh-badges/lib/color')
|
||||
const logos = require('./load-logos')()
|
||||
const simpleIcons = require('./load-simple-icons')()
|
||||
const { svg2base64, isDataUri } = require('./logo-helper')
|
||||
const colorschemes = require('../gh-badges/lib/colorscheme.json')
|
||||
|
||||
function toArray(val) {
|
||||
if (val === undefined) {
|
||||
@@ -30,43 +29,6 @@ function prependPrefix(s, prefix) {
|
||||
}
|
||||
}
|
||||
|
||||
function isHexColor(s = '') {
|
||||
return /^([\da-f]{3}){1,2}$/i.test(s)
|
||||
}
|
||||
|
||||
function makeColor(color) {
|
||||
if (isHexColor(color)) {
|
||||
return `#${color}`
|
||||
} else if (colorschemes[color] !== undefined) {
|
||||
return colorschemes[color].colorB
|
||||
} else if (isCSSColor(color)) {
|
||||
return color
|
||||
} else {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
function makeColorB(defaultColor, overrides) {
|
||||
return makeColor(overrides.colorB || defaultColor)
|
||||
}
|
||||
|
||||
function setBadgeColor(badgeData, color) {
|
||||
if (isHexColor(color)) {
|
||||
badgeData.colorB = `#${color}`
|
||||
delete badgeData.colorscheme
|
||||
} else if (colorschemes[color] !== undefined) {
|
||||
badgeData.colorscheme = color
|
||||
delete badgeData.colorB
|
||||
} else if (isCSSColor(color)) {
|
||||
badgeData.colorB = color
|
||||
delete badgeData.colorscheme
|
||||
} else {
|
||||
badgeData.colorscheme = 'red'
|
||||
delete badgeData.colorB
|
||||
}
|
||||
return badgeData
|
||||
}
|
||||
|
||||
function makeLabel(defaultLabel, overrides) {
|
||||
return `${
|
||||
overrides.label === undefined
|
||||
@@ -80,18 +42,18 @@ function getShieldsIcon(icon = '', color = '') {
|
||||
if (!logos[icon]) {
|
||||
return undefined
|
||||
}
|
||||
color = makeColor(color)
|
||||
color = toSvgColor(color)
|
||||
return color
|
||||
? logos[icon].svg.replace(/fill="(.+?)"/g, `fill="${color}"`)
|
||||
: logos[icon].base64
|
||||
}
|
||||
|
||||
function getSimpleIcon(icon = '', color = null) {
|
||||
function getSimpleIcon(icon = '', color) {
|
||||
icon = typeof icon === 'string' ? icon.toLowerCase().replace(/ /g, '-') : ''
|
||||
if (!simpleIcons[icon]) {
|
||||
return undefined
|
||||
}
|
||||
color = makeColor(color)
|
||||
color = toSvgColor(color)
|
||||
return color
|
||||
? simpleIcons[icon].svg.replace('<svg', `<svg fill="${color}"`)
|
||||
: simpleIcons[icon].base64
|
||||
@@ -144,19 +106,15 @@ function makeBadgeData(defaultLabel, overrides) {
|
||||
logoPosition: +overrides.logoPosition,
|
||||
logoWidth: +overrides.logoWidth,
|
||||
links: toArray(overrides.link),
|
||||
colorA: makeColor(overrides.colorA),
|
||||
colorB: makeColor(overrides.colorB),
|
||||
colorA: overrides.colorA,
|
||||
colorB: overrides.colorB,
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
toArray,
|
||||
prependPrefix,
|
||||
isHexColor,
|
||||
makeLabel,
|
||||
makeLogo,
|
||||
makeBadgeData,
|
||||
makeColor,
|
||||
makeColorB,
|
||||
setBadgeColor,
|
||||
}
|
||||
|
||||
@@ -4,12 +4,9 @@ const { expect } = require('chai')
|
||||
const { test, given, forCases } = require('sazerac')
|
||||
const {
|
||||
prependPrefix,
|
||||
isHexColor,
|
||||
makeLabel,
|
||||
makeLogo,
|
||||
makeBadgeData,
|
||||
makeColor,
|
||||
setBadgeColor,
|
||||
} = require('./badge-data')
|
||||
|
||||
describe('Badge data helpers', function() {
|
||||
@@ -21,11 +18,6 @@ describe('Badge data helpers', function() {
|
||||
given(undefined, 'data:').expect(undefined)
|
||||
})
|
||||
|
||||
test(isHexColor, () => {
|
||||
forCases([given('f00bae'), given('4c1')]).expect(true)
|
||||
forCases([given('f00bar'), given(''), given(undefined)]).expect(false)
|
||||
})
|
||||
|
||||
test(makeLabel, () => {
|
||||
given('my badge', {}).expect('my badge')
|
||||
given('My bAdge', {}).expect('my badge')
|
||||
@@ -70,40 +62,8 @@ describe('Badge data helpers', function() {
|
||||
logoPosition: 10,
|
||||
logoWidth: 25,
|
||||
links: ['https://example.com/'],
|
||||
colorA: '#007ec6',
|
||||
colorB: '#f00bae',
|
||||
})
|
||||
})
|
||||
|
||||
test(makeColor, () => {
|
||||
given('red').expect('#e05d44')
|
||||
given('blue').expect('#007ec6')
|
||||
given('4c1').expect('#4c1')
|
||||
given('f00f00').expect('#f00f00')
|
||||
given('papayawhip').expect('papayawhip')
|
||||
given('purple').expect('purple')
|
||||
})
|
||||
|
||||
test(setBadgeColor, () => {
|
||||
given({}, 'red').expect({ colorscheme: 'red' })
|
||||
given({}, 'f00f00').expect({ colorB: '#f00f00' })
|
||||
given({ colorB: '#f00f00', colorscheme: 'blue' }, 'red').expect({
|
||||
colorscheme: 'red',
|
||||
})
|
||||
given({ colorB: '#f00f00', colorscheme: 'blue' }, 'blue').expect({
|
||||
colorscheme: 'blue',
|
||||
})
|
||||
given({ colorB: '#f00f00', colorscheme: 'blue' }, 'papayawhip').expect({
|
||||
colorB: 'papayawhip',
|
||||
})
|
||||
given({ colorB: '#f00f00', colorscheme: 'blue' }, 'purple').expect({
|
||||
colorB: 'purple',
|
||||
})
|
||||
given({ colorB: '#b00b00', colorscheme: 'blue' }, '4c1').expect({
|
||||
colorB: '#4c1',
|
||||
})
|
||||
given({ colorB: '#b00b00', colorscheme: 'blue' }, 'f00f00').expect({
|
||||
colorB: '#f00f00',
|
||||
colorA: 'blue',
|
||||
colorB: 'f00bae',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -161,7 +161,7 @@ module.exports = class Server {
|
||||
const format = match[1]
|
||||
const badgeData = makeBadgeData('404', query)
|
||||
badgeData.text[1] = 'badge not found'
|
||||
badgeData.colorscheme = 'red'
|
||||
badgeData.colorB = 'red'
|
||||
// Add format to badge data.
|
||||
badgeData.format = format
|
||||
const svg = makeBadge(badgeData)
|
||||
|
||||
Reference in New Issue
Block a user