Skip to content

Commit

Permalink
Expose logoBase64 and links in badge-maker NPM package (#10283)
Browse files Browse the repository at this point in the history
* expose `logoBase64` and `links` in badge-maker NPM package

* add test assertion for logoBase64

* pass (public) `logoBase64` as (internal) `logo`

* badge-maker 4.0.0 release

---------

Co-authored-by: Sergey Kupletsky <s.kupletsky@gmail.com>
  • Loading branch information
chris48s and zavoloklom committed Jun 24, 2024
1 parent 677e713 commit 5c81299
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 7 deletions.
8 changes: 7 additions & 1 deletion badge-maker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Changelog

## 4.0.0 [WIP]
## 4.0.0

### Breaking Changes

- Drop compatibility with Node < 16

### Features

- Add `links` and `logoBase64` params

## 3.3.1

- Improve font measuring in for-the-badge and social styles
Expand Down
2 changes: 2 additions & 0 deletions badge-maker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ The format is the following:
message: 'passed', // (Required) Badge message
labelColor: '#555', // (Optional) Label color
color: '#4c1', // (Optional) Message color
logoBase64: 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI2NCIgaGVpZ2h0PSI2NCI+PHJlY3Qgd2lkdGg9IjY0IiBoZWlnaHQ9IjY0IiByeD0iOCIgZmlsbD0iI2IxY2U1NiIvPjxwYXRoIGQ9Ik04IDBoMjR2NjRIOGMtNC40MzIgMC04LTMuNTY4LTgtOFY4YzAtNC40MzIgMy41NjgtOCA4LTh6IiBmaWxsPSIjNWQ1ZDVkIi8+PC9zdmc+' // (Optional) Any custom logo can be passed in a URL parameter by base64 encoding
links: ['https://example.com', 'https://example.com'], // (Optional) Links array of maximum two links

// (Optional) One of: 'plastic', 'flat', 'flat-square', 'for-the-badge' or 'social'
// Each offers a different visual design.
Expand Down
2 changes: 2 additions & 0 deletions badge-maker/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ interface Format {
labelColor?: string
color?: string
style?: 'plastic' | 'flat' | 'flat-square' | 'for-the-badge' | 'social'
logoBase64?: string
links?: Array<string>
}

export declare class ValidationError extends Error {}
Expand Down
37 changes: 33 additions & 4 deletions badge-maker/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,30 @@ function _validate(format) {
throw new ValidationError('Field `message` is required')
}

const stringFields = ['labelColor', 'color', 'message', 'label']
const stringFields = ['labelColor', 'color', 'message', 'label', 'logoBase64']
stringFields.forEach(function (field) {
if (field in format && typeof format[field] !== 'string') {
throw new ValidationError(`Field \`${field}\` must be of type string`)
}
})

if ('links' in format) {
if (!Array.isArray(format.links)) {
throw new ValidationError('Field `links` must be an array of strings')
} else {
if (format.links.length > 2) {
throw new ValidationError(
'Field `links` must not have more than 2 elements',
)
}
format.links.forEach(function (field) {
if (typeof field !== 'string') {
throw new ValidationError('Field `links` must be an array of strings')
}
})
}
}

const styleValues = [
'plastic',
'flat',
Expand All @@ -38,11 +55,21 @@ function _validate(format) {
}

function _clean(format) {
const expectedKeys = ['label', 'message', 'labelColor', 'color', 'style']
const expectedKeys = [
'label',
'message',
'labelColor',
'color',
'style',
'logoBase64',
'links',
]

const cleaned = {}
Object.keys(format).forEach(key => {
if (format[key] != null && expectedKeys.includes(key)) {
if (format[key] != null && key === 'logoBase64') {
cleaned.logo = format[key]
} else if (format[key] != null && expectedKeys.includes(key)) {
cleaned[key] = format[key]
} else {
throw new ValidationError(
Expand All @@ -65,7 +92,9 @@ function _clean(format) {
* @param {string} format.message (Required) Badge message (e.g: 'passing')
* @param {string} format.labelColor (Optional) Label color
* @param {string} format.color (Optional) Message color
* @param {string} format.style (Optional) Visual style e.g: 'flat'
* @param {string} format.style (Optional) Visual style (e.g: 'flat')
* @param {string} format.logoBase64 (Optional) Logo data URL
* @param {Array} format.links (Optional) Links array (e.g: ['https://example.com', 'https://example.com'])
* @returns {string} Badge in SVG format
* @see https://github.com/badges/shields/tree/master/badge-maker/README.md
*/
Expand Down
30 changes: 30 additions & 0 deletions badge-maker/lib/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ describe('makeBadge function', function () {
style: 'flat',
}),
).to.satisfy(isSvg)
expect(
makeBadge({
label: 'build',
message: 'passed',
color: 'green',
style: 'flat',
labelColor: 'blue',
logoBase64: 'data:image/svg+xml;base64,PHN2ZyB4bWxu',
links: ['https://example.com', 'https://example.com'],
}),
)
.to.satisfy(isSvg)
// explicitly make an assertion about logoBase64
// this param is not a straight passthrough
.and.to.include('data:image/svg+xml;base64,PHN2ZyB4bWxu')
})

it('should throw a ValidationError with invalid inputs', function () {
Expand All @@ -46,6 +61,21 @@ describe('makeBadge function', function () {
expect(() =>
makeBadge({ label: 'build', message: 'passed', labelColor: 7 }),
).to.throw(ValidationError, 'Field `labelColor` must be of type string')
expect(() =>
makeBadge({ label: 'build', message: 'passed', logoBase64: 7 }),
).to.throw(ValidationError, 'Field `logoBase64` must be of type string')
expect(() =>
makeBadge({ label: 'build', message: 'passed', links: 'test' }),
).to.throw(ValidationError, 'Field `links` must be an array of strings')
expect(() =>
makeBadge({ label: 'build', message: 'passed', links: [1] }),
).to.throw(ValidationError, 'Field `links` must be an array of strings')
expect(() =>
makeBadge({ label: 'build', message: 'passed', links: ['1', '2', '3'] }),
).to.throw(
ValidationError,
'Field `links` must not have more than 2 elements',
)
expect(() =>
makeBadge({ label: 'build', message: 'passed', format: 'png' }),
).to.throw(ValidationError, "Unexpected field 'format'")
Expand Down
2 changes: 1 addition & 1 deletion badge-maker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "badge-maker",
"version": "3.3.1",
"version": "4.0.0",
"description": "Shields.io badge library",
"keywords": [
"GitHub",
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5c81299

Please sign in to comment.