Skip to content
Draft
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
55 changes: 42 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
"use strict";

var os = require("os");
var fs = require("fs");
var path = require("path");

var isPlainObject = require("is-plain-object").isPlainObject;
var pick = require("object.pick");
var defaults = require("object.defaults/immutable");
var expandTilde = require("expand-tilde");
var parsePath = require("parse-filepath");

function fined(pathObj, defaultObj) {
var expandedPath = expandPath(pathObj, defaultObj);
return expandedPath ? findWithExpandedPath(expandedPath) : null;
}

function expandPath(pathObj, defaultObj) {

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 24 on windows-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 24 on windows-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 24 on ubuntu-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 24 on ubuntu-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 26 on ubuntu-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 26 on ubuntu-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 22 on macos-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 22 on macos-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 26 on macos-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 26 on macos-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 22 on ubuntu-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 22 on ubuntu-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 24 on macos-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 24 on macos-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 22 on windows-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 22 on windows-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 26 on windows-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30

Check warning on line 12 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 26 on windows-latest

Function 'expandPath' has too many statements (32). Maximum allowed is 30
if (!isPlainObject(defaultObj)) {
if (!isObject(defaultObj)) {
defaultObj = {};
}

if (isString(pathObj)) {
pathObj = { path: pathObj };
}

if (!isPlainObject(pathObj)) {
if (!isObject(pathObj)) {
pathObj = {};
}

Expand Down Expand Up @@ -53,8 +48,8 @@

var findUp = !!pathObj.findUp;

var parsed = parsePath(filePath);
if (parsed.isAbsolute) {
var parsed = path.parse(filePath);
if (path.isAbsolute(filePath)) {
filePath = filePath.slice(parsed.root.length);
findUp = false;
basedir = parsed.root;
Expand Down Expand Up @@ -101,7 +96,7 @@
try {
fs.statSync(filepath);
return { path: filepath, extension: extArr[i] };
} catch (e) {
} catch {
// Ignore error
}
}
Expand Down Expand Up @@ -134,7 +129,7 @@
return exts.length > 0 ? exts : [""];
}

if (isPlainObject(exts)) {
if (isObject(exts)) {
exts = Object.keys(exts);
return exts.length > 0 ? exts : [""];
}
Expand All @@ -143,7 +138,7 @@
}

function createExtensionMap(exts) {
if (!isPlainObject(exts)) {
if (!isObject(exts)) {
return null;
}

Expand All @@ -154,6 +149,23 @@
return exts;
}

function defaults(userObj, defaultObj) {
var filteredEntries = Object.entries(userObj).filter(
([_, value]) => value != null,

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 24 on windows-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 24 on windows-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 24 on ubuntu-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 24 on ubuntu-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 26 on ubuntu-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 26 on ubuntu-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 22 on macos-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 22 on macos-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 26 on macos-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 26 on macos-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 22 on ubuntu-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 22 on ubuntu-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 24 on macos-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 24 on macos-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 22 on windows-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 22 on windows-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 26 on windows-latest

'_' is defined but never used

Check warning on line 154 in index.js

View workflow job for this annotation

GitHub Actions / Tests for Node 26 on windows-latest

'_' is defined but never used
);
var filteredObj = Object.fromEntries(filteredEntries);
return Object.assign({}, defaultObj, filteredObj);
}

function pick(map, match) {
var entries = Object.entries(map).filter(([key]) => key === match);
return Object.fromEntries(entries);
}

function isObject(obj) {
return obj && typeof obj === "object" && !Array.isArray(obj);
}

function isEmpty(object) {
return !Object.keys(object).length;
}
Expand All @@ -170,4 +182,21 @@
return false;
}

/*
* Copyright (c) 2015 Jon Schlinkert.
* Licensed under the MIT license.
*/
function expandTilde(filepath) {
var home = os.homedir();

if (filepath.charCodeAt(0) === 126 /* ~ */) {
if (filepath.charCodeAt(1) === 43 /* + */) {
return path.join(process.cwd(), filepath.slice(2));
}
return home ? path.join(home, filepath.slice(1)) : filepath;
}

return filepath;
}

module.exports = fined;
Loading
Loading