Skip to content

Commit

Permalink
Refactor View.prototype.resolve to improve readability and efficiency
Browse files Browse the repository at this point in the history
- Simplified logic for resolving file paths.
- Used optional chaining for cleaner code.
- Maintained functionality while enhancing clarity.
- Updated JSDoc to specify the return type: {string|undefined}.
  • Loading branch information
Ayoub-Mabrouk committed Oct 29, 2024
1 parent b31910c commit c6ef7b9
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,27 +163,19 @@ View.prototype.render = function render(options, callback) {
*
* @param {string} dir
* @param {string} file
* @return {string|undefined}
* @private
*/

View.prototype.resolve = function resolve(dir, file) {
var ext = this.ext;

// <path>.<ext>
var path = join(dir, file);
var stat = tryStat(path);
const ext = this.ext;

if (stat && stat.isFile()) {
return path;
}
let path = join(dir, file);
if (tryStat(path)?.isFile()) return path;

// <path>/index.<ext>
path = join(dir, basename(file, ext), 'index' + ext);
stat = tryStat(path);
path = join(dir, basename(file, ext), `index${ext}`);
if (tryStat(path)?.isFile()) return path;

if (stat && stat.isFile()) {
return path;
}
return undefined;
};

/**
Expand Down

0 comments on commit c6ef7b9

Please sign in to comment.