Skip to content
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

Consistent version formatting #1246

Merged
merged 6 commits into from
Nov 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/color-formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ function version(version) {
let first = version[0];
if (first === 'v') {
first = version[1];
} else if (/^[0-9]/.test(version)) {
version = 'v' + version;
}
if (first === '0' || (version.indexOf('-') !== -1)) {
return { version: version, color: 'orange' };
if (first === '0' || /alpha|beta|snapshot|dev|pre/.test(version.toLowerCase())) {
return 'orange';
} else {
return { version: version, color: 'blue' };
return 'blue';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for separating these responsibilities. 🍻

}
}

Expand Down
16 changes: 15 additions & 1 deletion lib/color-formatters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const { test, given, forCases } = require('sazerac');
const {
coveragePercentage,
colorScale,
age
age,
version
} = require('./color-formatters');

describe('Color formatters', function() {
Expand Down Expand Up @@ -52,4 +53,17 @@ describe('Color formatters', function() {
given(monthsAgo(2)).describe('when given a Date two months ago').expect('yellowgreen');
given(monthsAgo(15)).describe('when given a Date 15 months ago').expect('orange');
});

test(version, () => {
given('1.0').expect('blue');

forCases([
given('0.9'),
given('1.0-Beta'),
given('1.1-alpha'),
given('6.0-SNAPSHOT'),
given('1.0.1-dev'),
given('2.1.6-prerelease'),
]).expect('orange');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

});
});
15 changes: 14 additions & 1 deletion lib/text-formatters.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Commonly-used functions for formatting text in badge labels. Includes
* ordinal numbers, currency codes, star ratings, etc.
* ordinal numbers, currency codes, star ratings, versions, etc.
*/
'use strict';

Expand Down Expand Up @@ -63,6 +63,18 @@ function omitv(version) {
return version;
}

// Add a starting v to the version unless:
// - it does not start with a digit
// - it is a date (yyyy-mm-dd)
const ignoredVersionPatterns = /^[^0-9]|[0-9]{4}-[0-9]{2}-[0-9]{2}/;
function addv(version) {
if (version.startsWith('v') || ignoredVersionPatterns.test(version)) {
return version;
} else {
return `v${version}`;
}
}

function maybePluralize(singular, countable, plural) {
plural = plural || `${singular}s`;

Expand Down Expand Up @@ -91,6 +103,7 @@ module.exports = {
ordinalNumber,
metric,
omitv,
addv,
maybePluralize,
formatDate
};
8 changes: 8 additions & 0 deletions lib/text-formatters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
ordinalNumber,
metric,
omitv,
addv,
maybePluralize,
formatDate
} = require('./text-formatters');
Expand Down Expand Up @@ -54,6 +55,13 @@ describe('Text formatters', function() {
given('v1.0.1').expect('1.0.1');
});

test(addv, () => {
given('1.0.0').expect('v1.0.0');
given('v0.6').expect('v0.6');
given('hello').expect('hello');
given('2017-05-05-Release-2.3.17').expect('2017-05-05-Release-2.3.17');
});

test(maybePluralize, () => {
given('foo', []).expect('foos');
given('foo', [123]).expect('foo');
Expand Down
Loading