-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a render helper for downloads badges, run [amo ansible apm chromewebstore clojars conda crates docker dub eclipse gem githubdownloads] #7163
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
caff35c
refactor: add render helper for downloads badges
calebcartwright 1171d41
refactor: use new helper in some download badge classes
calebcartwright 3eda04b
doc renderer function
calebcartwright cc39d75
Merge branch 'master' into downloads-renderer
repo-ranger[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { downloadCount } from './color-formatters.js' | ||
import { metric } from './text-formatters.js' | ||
|
||
/** | ||
* Handles rendering concerns of badges that display | ||
* download counts, with override/customization support | ||
* | ||
* @param {object} attrs Refer to individual attrs | ||
* @param {number} attrs.downloads Number of downloads | ||
* @param {string} [attrs.interval] Period or interval the downloads occurred | ||
* (e.g. day, week, month). If provided then this Will be reflected | ||
* in the badge message unless overridden by other message-related parameters | ||
* @param {string} [attrs.version] Version or tag that was downloaded | ||
* which will be reflected in the badge label (unless the label is overridden) | ||
* @param {string} [attrs.labelOverride] If provided then the badge label is set to this | ||
* value overriding any other label-related parameters | ||
* @param {string} [attrs.colorOverride] If provided then the badge color is set to this | ||
* value instead of the color being based on the count of downloads | ||
* @param {string} [attrs.messageSuffixOverride] If provided then the badge message will | ||
* will have this value added to the download count, separated with a space | ||
* @returns {object} Badge | ||
*/ | ||
function renderDownloadsBadge({ | ||
downloads, | ||
interval, | ||
version, | ||
labelOverride, | ||
colorOverride, | ||
messageSuffixOverride, | ||
}) { | ||
let messageSuffix = '' | ||
if (messageSuffixOverride) { | ||
messageSuffix = ` ${messageSuffixOverride}` | ||
} else if (interval) { | ||
messageSuffix = `/${interval}` | ||
} | ||
|
||
let label | ||
if (labelOverride) { | ||
label = labelOverride | ||
} else if (version) { | ||
label = `downloads@${version}` | ||
} | ||
|
||
return { | ||
label, | ||
color: colorOverride || downloadCount(downloads), | ||
message: `${metric(downloads)}${messageSuffix}`, | ||
} | ||
} | ||
|
||
export { renderDownloadsBadge } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { test, given } from 'sazerac' | ||
import { renderDownloadsBadge } from './downloads.js' | ||
import { downloadCount } from './color-formatters.js' | ||
import { metric } from './text-formatters.js' | ||
|
||
const downloads = 2345 | ||
const message = metric(downloads) | ||
const color = downloadCount(downloads) | ||
|
||
describe('downloads', function () { | ||
test(renderDownloadsBadge, () => { | ||
given({ downloads }).expect({ label: undefined, color, message }) | ||
given({ downloads, labelOverride: 'recent downloads' }).expect({ | ||
label: 'recent downloads', | ||
color, | ||
message, | ||
}) | ||
given({ downloads, version: 'v1.0.0' }).expect({ | ||
label: '[email protected]', | ||
color, | ||
message, | ||
}) | ||
given({ | ||
downloads, | ||
messageSuffixOverride: '[foo.tar.gz]', | ||
interval: 'week', | ||
}).expect({ | ||
label: undefined, | ||
color, | ||
message: `${message} [foo.tar.gz]`, | ||
}) | ||
given({ downloads, interval: 'year' }).expect({ | ||
label: undefined, | ||
color, | ||
message: `${message}/year`, | ||
}) | ||
given({ downloads, colorOverride: 'pink' }).expect({ | ||
label: undefined, | ||
color: 'pink', | ||
message, | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this helper is a good example of a function that would benefit from a docstring comment explaining what each of these 6 params does, which ones are required/optional, etc as it is a bit more complex than something like
renderLicenseBadge
orrenderVersionBadge
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point, and there's not actually any input validation in here currently. Do you think it'd be worthwhile to add some? E.g. ensure downloads is provided and numerical (or at least numerically coercible)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah its a good question that I don't have a completely solid answer to.
Just looking over
renderVersionBadge
,renderLicenseBadge
,renderBuildStatusBadge
they're a bit of a mixed bag.I reckon its mostly fine to delegate responsibility for passing sensible values to
renderDownloadsBadge
to the service layer. My instinct is that at this layer of the application we probably don't want to be throwing a lot of exceptions that we need to remember to catch in the service layer but we probably do have some formatting helpers that do throw under some circumstances. Maybe we should be aiming to guard against outputting stuff likemessage: '12k/undefined'
though.Dunno how much help that is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documented in 3eda04b. I couldn't quite convince myself to add any input validation and exception throwing so have opted to leave that possibility for a future day should the need arise