Skip to content
Open
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
3 changes: 3 additions & 0 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ jobs:
- name: Check format
run: npm run format-check

- name: Check types
run: npm run types-check

- name: Coveralls
uses: coverallsapp/github-action@8d6379e14d29928660c4ba802d8e85393440b329 # v2.3.8
with:
Expand Down
12 changes: 12 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Produces a new file path where the extension is replaced with the provided extension.
*
* @param {string} path The user-provided filepath.
* @param {string} ext String to use as the extension.
* @returns {string | undefined} The updated path if provided or `undefined` otherwise.
*/
export default function replaceExt(
path: string,
ext: string,
): string | undefined;
//# sourceMappingURL=index.d.ts.map
1 change: 1 addition & 0 deletions index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 26 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
import path from "node:path";

export default function replaceExt(npath, ext) {
if (typeof npath !== "string") {
return npath;
import { basename, extname, dirname, join, sep } from "node:path";
/**
* Produces a new file path where the extension is replaced with the provided extension.
*
* @param {string} path The user-provided filepath.
* @param {string} ext String to use as the extension.
* @returns {string | undefined} The updated path if provided or `undefined` otherwise.
*/
export default function replaceExt(path, ext) {
if (typeof path !== "string") {
return path;
}

if (npath.length === 0) {
return npath;
if (path.length === 0) {
return path;
}

var nFileName = path.basename(npath, path.extname(npath)) + ext;
var nFilepath = path.join(path.dirname(npath), nFileName);
var fileName = basename(path, extname(path)) + ext;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we change this to filename not fileName? or should it be filePath.

var filepath = join(dirname(path), fileName);

// Because `path.join` removes the head './' from the given path.
// Because `join` removes the head './' from the given path.
// This removal can cause a problem when passing the result to `require` or
// `import`.
if (startsWithSingleDot(npath)) {
return "." + path.sep + nFilepath;
if (startsWithSingleDot(path)) {
return "." + sep + filepath;
}

return nFilepath;
return filepath;
}

function startsWithSingleDot(fpath) {
var first2chars = fpath.slice(0, 2);
return first2chars === "." + path.sep || first2chars === "./";
/** *
* @param {string} path provided filepath
* @returns {boolean} Result if path starts with a dot.
*/
function startsWithSingleDot(path) {
var first2chars = path.slice(0, 2);
return first2chars === "." + sep || first2chars === "./";
}
7 changes: 7 additions & 0 deletions node.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare module "node:path" {
export const sep: "\\" | "/";
export function dirname(path: string): string;
export function extname(path: string): string;
export function join(p1: string, p2: string): string;
export function basename(path: string, suffix?: string | undefined): string;
}
17 changes: 16 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,27 @@
},
"type": "module",
"main": "./index.js",
"types": "./index.d.ts",
"exports": "./index.js",
"files": [
"LICENSE",
"index.js"
"index.js",
"index.d.ts",
"index.d.ts.map"
],
"scripts": {
"format-check": "prettier . --check",
"format-write": "prettier . --write",
"lint": "eslint .",
"test": "node --test --experimental-test-coverage --test-reporter=spec --test-reporter-destination=stdout --test-reporter=lcov --test-reporter-destination=test/lcov.info \"test/*.js\""
"test": "node --test --experimental-test-coverage --test-reporter=spec --test-reporter-destination=stdout --test-reporter=lcov --test-reporter-destination=test/lcov.info \"test/*.js\"",
"types-check": "tsc index.js --checkJs --noEmit --types ./node.d.ts",
"types-write": "tsc index.js --checkJs --declaration --declarationMap --emitDeclarationOnly --types ./node.d.ts"
},
"devDependencies": {
"eslint": "10.11.0",
"eslint-config-gulp": "7.0.0",
"prettier": "3.9.6"
"prettier": "3.9.6",
"typescript": "6.0.3"
},
"keywords": [
"gulp",
Expand Down
Loading