Skip to content

Commit

Permalink
add prefix override to renderVersionBadge
Browse files Browse the repository at this point in the history
adds tests for all options with prefix as well

used for badges#2026 but also usefull for usage letting people override v prefix for versions all over the project once badges#2026 is done as requested for example in badges#10574
  • Loading branch information
jNullj committed Oct 16, 2024
1 parent ec97261 commit 1ae6c44
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion services/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ function rangeStart(v) {
* @param {string} options.version - The version number to display on the badge
* @param {string} [options.tag] - The tag to display on the badge, such as "alpha" or "beta"
* @param {string} [options.defaultLabel] - The default label to display on the badge, such as "npm" or "github"
* @param {string} [options.prefix] - The prefix to display on the message, such as ">=", "v", overrides the default behavior of using addv
* @param {string} [options.postfix] - The postfix to display on the message, such as "tested"
* @param {Function} [options.versionFormatter=versionColor] - The function to use to format the color of the badge based on the version number
* @returns {object} A badge object that has three properties: label, message, and color
Expand All @@ -246,12 +247,15 @@ function renderVersionBadge({
version,
tag,
defaultLabel,
prefix,
postfix,
versionFormatter = versionColor,
}) {
return {
label: tag ? `${defaultLabel}@${tag}` : defaultLabel,
message: addv(version) + (postfix ? ` ${postfix}` : ''),
message:
(prefix ? `${prefix}${version}` : addv(version)) +
(postfix ? ` ${postfix}` : ''),
color: versionFormatter(version),
}
}
Expand Down
44 changes: 44 additions & 0 deletions services/version.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,49 @@ describe('Version helpers', function () {
message: 'v1.2.3 tested',
color: 'blue',
})
given({ version: '1.2.3', prefix: '^' }).expect({
label: undefined,
message: '^1.2.3',
color: 'blue',
})
given({
version: '1.2.3',
tag: 'alpha',
defaultLabel: 'npm',
prefix: '^',
}).expect({
label: 'npm@alpha',
message: '^1.2.3',
color: 'blue',
})
given({
version: '1.2.3',
defaultLabel: 'npm',
prefix: '^',
}).expect({
label: 'npm',
message: '^1.2.3',
color: 'blue',
})
given({
version: '1.2.3',
prefix: '^',
postfix: 'tested',
}).expect({
label: undefined,
message: '^1.2.3 tested',
color: 'blue',
})
given({
version: '1.2.3',
tag: 'beta',
defaultLabel: 'github',
prefix: '^',
postfix: 'tested',
}).expect({
label: 'github@beta',
message: '^1.2.3 tested',
color: 'blue',
})
})
})

0 comments on commit 1ae6c44

Please sign in to comment.