diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 3f4dfb1..dc49dd4 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -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: diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..3ea58c6 --- /dev/null +++ b/index.d.ts @@ -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 diff --git a/index.d.ts.map b/index.d.ts.map new file mode 100644 index 0000000..9a93437 --- /dev/null +++ b/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AACA;;;;;;GAMG;AACH,yCAJW,MAAM,OACN,MAAM,GACJ,MAAM,GAAG,SAAS,CAsB9B"} \ No newline at end of file diff --git a/index.js b/index.js index 1f6e1ca..c36bac5 100644 --- a/index.js +++ b/index.js @@ -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; + 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 === "./"; } diff --git a/node.d.ts b/node.d.ts new file mode 100644 index 0000000..2dba34b --- /dev/null +++ b/node.d.ts @@ -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; +} diff --git a/package-lock.json b/package-lock.json index d2737b2..e0a4d7d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,8 @@ "devDependencies": { "eslint": "10.11.0", "eslint-config-gulp": "7.0.0", - "prettier": "3.9.6" + "prettier": "3.9.6", + "typescript": "6.0.3" }, "engines": { "node": ">= 22.15.0" @@ -994,6 +995,20 @@ "node": ">= 0.8.0" } }, + "node_modules/typescript": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-6.0.3.tgz", + "integrity": "sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/uri-js": { "version": "4.4.1", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", diff --git a/package.json b/package.json index ac6aa44..c569378 100644 --- a/package.json +++ b/package.json @@ -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",