From 01b317c95bd12853121fe95647f3067a794c0ec4 Mon Sep 17 00:00:00 2001 From: Matthew Bergman Date: Tue, 1 Dec 2026 15:35:28 -0500 Subject: [PATCH 1/4] feat: Add JSDoc & generate TypeScript types --- index.d.ts | 12 ++++++++++++ index.d.ts.map | 1 + index.js | 11 +++++++++++ node.d.ts | 7 +++++++ package-lock.json | 17 ++++++++++++++++- package.json | 12 +++++++++--- 6 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 index.d.ts create mode 100644 index.d.ts.map create mode 100644 node.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..3be522d --- /dev/null +++ b/index.d.ts @@ -0,0 +1,12 @@ +/** + * Replaces the extension from `path` with `extension` and returns the updated path string. + * + * @param {string} npath The user-provided filepath. + * @param {string} ext String to use as the extension. + * @returns {string | undefined} The updated path if found. + */ +export default function replaceExt( + npath: 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..df698af --- /dev/null +++ b/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,0CAJW,MAAM,OACN,MAAM,GACJ,MAAM,GAAG,SAAS,CAsB9B"} \ No newline at end of file diff --git a/index.js b/index.js index 1f6e1ca..1613528 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,12 @@ import path from "node:path"; +/** + * Replaces the extension from `path` with `extension` and returns the updated path string. + * + * @param {string} npath The user-provided filepath. + * @param {string} ext String to use as the extension. + * @returns {string | undefined} The updated path if found. + */ export default function replaceExt(npath, ext) { if (typeof npath !== "string") { return npath; @@ -22,6 +29,10 @@ export default function replaceExt(npath, ext) { return nFilepath; } +/** * + * @param {string} fpath provided filepath. + * @returns {boolean} Result if path starts with a dot. + */ function startsWithSingleDot(fpath) { var first2chars = fpath.slice(0, 2); return first2chars === "." + path.sep || first2chars === "./"; diff --git a/node.d.ts b/node.d.ts new file mode 100644 index 0000000..8c85bfa --- /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(...paths: 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..d0c78eb 100644 --- a/package.json +++ b/package.json @@ -17,22 +17,28 @@ "node": ">= 22.15.0" }, "type": "module", + "types": "./index.d.ts", "main": "./index.js", "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", From a448578ff83ed3c3d685649e8f992b1e00676694 Mon Sep 17 00:00:00 2001 From: Matthew Bergman Date: Tue, 1 Dec 2026 15:51:51 -0500 Subject: [PATCH 2/4] chore: add check-types workflow. --- .github/workflows/dev.yml | 3 +++ 1 file changed, 3 insertions(+) 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: From 9d9d894c52594510e984dab77f87263fa91ea6ca Mon Sep 17 00:00:00 2001 From: Matthew Bergman Date: Tue, 1 Dec 2026 15:55:26 -0500 Subject: [PATCH 3/4] chore: explicitly import path functions so jsdoc variables are cleaner. --- index.d.ts | 4 ++-- index.d.ts.map | 2 +- index.js | 33 ++++++++++++++++----------------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/index.d.ts b/index.d.ts index 3be522d..842f2e8 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,12 +1,12 @@ /** * Replaces the extension from `path` with `extension` and returns the updated path string. * - * @param {string} npath The user-provided filepath. + * @param {string} path The user-provided file * @param {string} ext String to use as the extension. * @returns {string | undefined} The updated path if found. */ export default function replaceExt( - npath: string, + path: string, ext: string, ): string | undefined; //# sourceMappingURL=index.d.ts.map diff --git a/index.d.ts.map b/index.d.ts.map index df698af..9a93437 100644 --- a/index.d.ts.map +++ b/index.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,0CAJW,MAAM,OACN,MAAM,GACJ,MAAM,GAAG,SAAS,CAsB9B"} \ No newline at end of file +{"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 1613528..bd39d6c 100644 --- a/index.js +++ b/index.js @@ -1,39 +1,38 @@ -import path from "node:path"; - +import { basename, extname, dirname, join, sep } from "node:path"; /** * Replaces the extension from `path` with `extension` and returns the updated path string. * - * @param {string} npath The user-provided filepath. + * @param {string} path The user-provided file * @param {string} ext String to use as the extension. * @returns {string | undefined} The updated path if found. */ -export default function replaceExt(npath, ext) { - if (typeof npath !== "string") { - return npath; +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 nFileName = basename(path, extname(path)) + ext; + var nFilepath = join(dirname(path), nFileName); - // Because `path.join` removes the head './' from the given path. + // Because `join` removes the head './' from the given // 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 + nFilepath; } return nFilepath; } /** * - * @param {string} fpath provided filepath. + * @param {string} path provided file * @returns {boolean} Result if path starts with a dot. */ -function startsWithSingleDot(fpath) { - var first2chars = fpath.slice(0, 2); - return first2chars === "." + path.sep || first2chars === "./"; +function startsWithSingleDot(path) { + var first2chars = path.slice(0, 2); + return first2chars === "." + sep || first2chars === "./"; } From a4d501e0fa97c40b2869501f8f5c2f462873d9e6 Mon Sep 17 00:00:00 2001 From: Matthew Bergman Date: Tue, 1 Dec 2026 19:46:19 -0500 Subject: [PATCH 4/4] chore: update jsdoc and types. --- index.d.ts | 6 +++--- index.js | 18 +++++++++--------- node.d.ts | 2 +- package.json | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/index.d.ts b/index.d.ts index 842f2e8..3ea58c6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,9 +1,9 @@ /** - * Replaces the extension from `path` with `extension` and returns the updated path string. + * Produces a new file path where the extension is replaced with the provided extension. * - * @param {string} path The user-provided file + * @param {string} path The user-provided filepath. * @param {string} ext String to use as the extension. - * @returns {string | undefined} The updated path if found. + * @returns {string | undefined} The updated path if provided or `undefined` otherwise. */ export default function replaceExt( path: string, diff --git a/index.js b/index.js index bd39d6c..c36bac5 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,10 @@ import { basename, extname, dirname, join, sep } from "node:path"; /** - * Replaces the extension from `path` with `extension` and returns the updated path string. + * Produces a new file path where the extension is replaced with the provided extension. * - * @param {string} path The user-provided file + * @param {string} path The user-provided filepath. * @param {string} ext String to use as the extension. - * @returns {string | undefined} The updated path if found. + * @returns {string | undefined} The updated path if provided or `undefined` otherwise. */ export default function replaceExt(path, ext) { if (typeof path !== "string") { @@ -15,21 +15,21 @@ export default function replaceExt(path, ext) { return path; } - var nFileName = basename(path, extname(path)) + ext; - var nFilepath = join(dirname(path), nFileName); + var fileName = basename(path, extname(path)) + ext; + var filepath = join(dirname(path), fileName); - // Because `join` removes the head './' from the given + // 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(path)) { - return "." + sep + nFilepath; + return "." + sep + filepath; } - return nFilepath; + return filepath; } /** * - * @param {string} path provided file + * @param {string} path provided filepath * @returns {boolean} Result if path starts with a dot. */ function startsWithSingleDot(path) { diff --git a/node.d.ts b/node.d.ts index 8c85bfa..2dba34b 100644 --- a/node.d.ts +++ b/node.d.ts @@ -2,6 +2,6 @@ declare module "node:path" { export const sep: "\\" | "/"; export function dirname(path: string): string; export function extname(path: string): string; - export function join(...paths: string[]): string; + export function join(p1: string, p2: string): string; export function basename(path: string, suffix?: string | undefined): string; } diff --git a/package.json b/package.json index d0c78eb..c569378 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,8 @@ "node": ">= 22.15.0" }, "type": "module", - "types": "./index.d.ts", "main": "./index.js", + "types": "./index.d.ts", "exports": "./index.js", "files": [ "LICENSE",