From f2efd751b51e2ccd472d183a5dca3d14665295cd Mon Sep 17 00:00:00 2001 From: Paul Melnikow Date: Mon, 2 Apr 2018 07:03:52 -0500 Subject: [PATCH] Minor refactor of examples preparation (#1632) This cleans up the work from #1582, clarifying concerns, removing a bit of duplication, and renaming for clarity. --- lib/all-badge-examples.js | 33 +++++---------------------------- lib/all-badge-examples.spec.js | 2 +- services/base.js | 33 +++++++++++++++++++++++++++++---- services/index.js | 2 +- 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/lib/all-badge-examples.js b/lib/all-badge-examples.js index 94c25feca2..e34e690707 100644 --- a/lib/all-badge-examples.js +++ b/lib/all-badge-examples.js @@ -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; diff --git a/lib/all-badge-examples.spec.js b/lib/all-badge-examples.spec.js index 7615510858..eb3dc77406 100644 --- a/lib/all-badge-examples.spec.js +++ b/lib/all-badge-examples.spec.js @@ -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')); diff --git a/services/base.js b/services/base.js index 23f527dbac..ef322de423 100644 --- a/services/base.js +++ b/services/base.js @@ -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); } diff --git a/services/index.js b/services/index.js index de64d865d9..493795ae5a 100644 --- a/services/index.js +++ b/services/index.js @@ -15,7 +15,7 @@ function loadServiceClasses() { function loadTesters() { return glob.sync(`${__dirname}/**/*.tester.js`) - .map(name => require(name)); + .map(path => require(path)); } module.exports = {