Minor refactor of examples preparation (#1632)
This cleans up the work from #1582, clarifying concerns, removing a bit of duplication, and renaming for clarity.
This commit is contained in:
@@ -2250,44 +2250,21 @@ const allBadgeExamples = [
|
||||
}
|
||||
];
|
||||
|
||||
function makeFullUrl(partialUrl, base) {
|
||||
return '/' + [base, partialUrl].filter(Boolean).join('/');
|
||||
}
|
||||
|
||||
function prepareExample({ title, previewUrl, exampleUrl, documentation }, ServiceClass) {
|
||||
if (! previewUrl) {
|
||||
throw Error(`Example for ${ServiceClass.name} is missing required previewUrl`);
|
||||
}
|
||||
|
||||
const fullUrl = partial => makeFullUrl(partial, ServiceClass.url.base);
|
||||
|
||||
return {
|
||||
title: title ? `${ServiceClass.name} ${title}` : ServiceClass.name,
|
||||
previewUri: `${fullUrl(previewUrl)}.svg`,
|
||||
exampleUri: exampleUrl ? `${fullUrl(exampleUrl)}.svg` : undefined,
|
||||
documentation,
|
||||
};
|
||||
}
|
||||
|
||||
function getCategory(wantedCategory) {
|
||||
function findCategory(wantedCategory) {
|
||||
return allBadgeExamples.find(thisCat => thisCat.category.id === wantedCategory);
|
||||
}
|
||||
|
||||
function loadExamples() {
|
||||
loadServiceClasses().forEach(ServiceClass => {
|
||||
const { category: wantedCategory, examples: theseExamples } = ServiceClass;
|
||||
|
||||
const category = getCategory(wantedCategory);
|
||||
const category = findCategory(ServiceClass.category);
|
||||
if (category === undefined) {
|
||||
throw Error(`Unknown category ${wantedCategory} referenced in ${ServiceClass.name}`);
|
||||
throw Error(`Unknown category ${ServiceClass.category} referenced in ${ServiceClass.name}`);
|
||||
}
|
||||
|
||||
const prepared = theseExamples.map(
|
||||
inExample => prepareExample(inExample, ServiceClass));
|
||||
const prepared = ServiceClass.prepareExamples();
|
||||
category.examples = category.examples.concat(prepared);
|
||||
});
|
||||
}
|
||||
loadExamples();
|
||||
|
||||
module.exports = allBadgeExamples;
|
||||
module.exports.getCategory = getCategory;
|
||||
module.exports.findCategory = findCategory;
|
||||
|
||||
@@ -6,7 +6,7 @@ const allBadgeExamples = require('./all-badge-examples');
|
||||
|
||||
describe('The badge examples', function () {
|
||||
it('should include AppVeyor, which is added automatically', function () {
|
||||
const { examples } = allBadgeExamples.getCategory('build');
|
||||
const { examples } = allBadgeExamples.findCategory('build');
|
||||
|
||||
const appVeyorBuildExamples = examples.filter(ex => ex.title.includes('AppVeyor'))
|
||||
.filter(ex => ! ex.title.includes('tests'));
|
||||
|
||||
@@ -70,12 +70,37 @@ module.exports = class BaseService {
|
||||
return [];
|
||||
}
|
||||
|
||||
static _makeFullUrl(partialUrl) {
|
||||
return '/' + [this.url.base, partialUrl].filter(Boolean).join('/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of examples. Each example is prepared according to the
|
||||
* schema in `lib/all-badge-examples.js`. Four keys are supported:
|
||||
* - title
|
||||
* - previewUrl
|
||||
* - exampleUrl
|
||||
* - documentation
|
||||
*/
|
||||
static prepareExamples() {
|
||||
return this.examples.map(({ title, previewUrl, exampleUrl, documentation }) => {
|
||||
if (! previewUrl) {
|
||||
throw Error(`Example for ${this.name} is missing required previewUrl`);
|
||||
}
|
||||
|
||||
return {
|
||||
title: title ? `${this.name} ${title}` : this.name,
|
||||
previewUri: `${this._makeFullUrl(previewUrl)}.svg`,
|
||||
exampleUri: exampleUrl ? `${this._makeFullUrl(exampleUrl)}.svg` : undefined,
|
||||
documentation,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
static get _regex() {
|
||||
const { base, format } = this.url;
|
||||
// Regular expressions treat "/" specially, so we need to escape them
|
||||
const escapedPath = format.replace(/\//g, '\\/');
|
||||
const joined = [base, escapedPath].filter(Boolean).join('/');
|
||||
const fullRegex = `^/${joined}.(svg|png|gif|jpg|json)$`;
|
||||
const escapedPath = this.url.format.replace(/\//g, '\\/');
|
||||
const fullRegex = `^${this._makeFullUrl(escapedPath)}.(svg|png|gif|jpg|json)$`;
|
||||
return new RegExp(fullRegex);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ function loadServiceClasses() {
|
||||
|
||||
function loadTesters() {
|
||||
return glob.sync(`${__dirname}/**/*.tester.js`)
|
||||
.map(name => require(name));
|
||||
.map(path => require(path));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user