Skip to content

Commit 31f113a

Browse files
committed
Trying to optimize and improve readability through better abstraction
Trying to optimise code, and improve abstraction for better readability Hopefully responding to the concerns of @RubenVerborgh: #1109 (comment) and #1109 (comment)
1 parent d3e6506 commit 31f113a

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

lib/resource-mapper.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ResourceMapper {
2525
this._defaultContentType = defaultContentType
2626
this._indexFilename = indexFilename
2727
this._types = { ...types, ...overrideTypes }
28-
this._suffixes = fileSuffixes
28+
this._isControlFile = new RegExp(`.(?:${fileSuffixes.join('|')})$`)
2929

3030
// If the host needs to be replaced on every call, pre-split the root URL
3131
if (includeHost) {
@@ -92,10 +92,13 @@ class ResourceMapper {
9292

9393
async _getMatchingFile (folder, filename, isIndex) {
9494
const files = await this._readdir(folder)
95-
96-
// Find a file with the same name (minus the dollar extension)
97-
return (files.find(f => this._removeDollarExtension(f) === filename ||
98-
(isIndex && f.startsWith(this._indexFilename + '.') && !this._suffixes.some(suffix => f.endsWith(suffix)))))
95+
const hasSameName = (f) => {
96+
return this._removeDollarExtension(f) === filename
97+
}
98+
const isIndexFile = (f) => {
99+
return isIndex && f.startsWith(this._indexFilename + '.') && !this._isControlFile.test(f)
100+
}
101+
return files.find(f => hasSameName(f) || isIndexFile(f))
99102
}
100103

101104
async getRepresentationUrlForResource (resourceUrl) {

test/unit/resource-mapper-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,18 @@ describe('ResourceMapper', () => {
286286
contentType: 'application/octet-stream'
287287
})
288288

289+
itMapsUrl(mapper, 'a URL of that has an accompanying acl file, but no actual file',
290+
{
291+
url: 'http://localhost/space/'
292+
},
293+
[
294+
`${rootPath}space/index.acl`
295+
],
296+
{
297+
path: `${rootPath}space/`,
298+
contentType: 'application/octet-stream'
299+
})
300+
289301
itMapsUrl(mapper, 'a URL ending with a slash to an index file for text/html when index.html not is available',
290302
{
291303
url: 'http://localhost/space/',
@@ -297,6 +309,20 @@ describe('ResourceMapper', () => {
297309
contentType: 'text/html'
298310
})
299311

312+
itMapsUrl(mapper, 'a URL of that has an accompanying meta file, but no actual file',
313+
{
314+
url: 'http://localhost/space/',
315+
contentType: 'text/html',
316+
createIfNotExists: true
317+
},
318+
[
319+
`${rootPath}space/index.meta`
320+
],
321+
{
322+
path: `${rootPath}space/index.html`,
323+
contentType: 'text/html'
324+
})
325+
300326
itMapsUrl(mapper, 'a URL ending with a slash to a folder when index is skipped',
301327
{
302328
url: 'http://localhost/space/',

0 commit comments

Comments
 (0)