From 4176e7f139bf430ef80b9ca8fb0105121304d180 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Wed, 21 Jan 2026 20:53:40 -0500 Subject: [PATCH 1/4] feat: add TypeScript definitions --- .github/workflows/ci.yml | 3 +++ index.d.ts | 34 ++++++++++++++++++++++++++++++++++ package.json | 13 ++++++++++--- test/types.ts | 9 +++++++++ tsconfig.json | 13 +++++++++++++ 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 index.d.ts create mode 100644 test/types.ts create mode 100644 tsconfig.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8beddcf..53cd7f5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -205,6 +205,9 @@ jobs: npm test fi + - name: Run type tests + run: npm run test-types + - name: Lint code if: steps.list_env.outputs.eslint != '' run: npm run lint diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..9d6c039 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,34 @@ +/** + * When ranges are returned, the array has a "type" property which is the type of + * range that is required (most commonly, "bytes"). Each array element is an object + * with a "start" and "end" property for the portion of the range. + * + * @returns `-1` when unsatisfiable and `-2` when syntactically invalid, ranges otherwise. + */ +declare function RangeParser( + size: number, + str: string, + options?: RangeParser.Options, +): RangeParser.Result | RangeParser.Ranges; + +declare namespace RangeParser { + interface Ranges extends Array { + type: string; + } + interface Range { + start: number; + end: number; + } + interface Options { + /** + * The "combine" option can be set to `true` and overlapping & adjacent ranges + * will be combined into a single range. + */ + combine?: boolean | undefined; + } + type ResultUnsatisfiable = -1; + type ResultInvalid = -2; + type Result = ResultUnsatisfiable | ResultInvalid; +} + +export = RangeParser; \ No newline at end of file diff --git a/package.json b/package.json index 4c41fff..ffa5414 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "url": "https://opencollective.com/express" }, "devDependencies": { + "@arethetypeswrong/cli": "^0.18.2", "deep-equal": "1.0.1", "eslint": "6.0.1", "eslint-config-standard": "13.0.1", @@ -28,14 +29,19 @@ "eslint-plugin-node": "9.1.0", "eslint-plugin-promise": "4.2.1", "eslint-plugin-standard": "4.0.0", + "expect-type": "~1.2.0", "mocha": "6.1.4", - "nyc": "14.1.1" + "nyc": "14.1.1", + "typescript": "~5.9.2" }, "files": [ - "HISTORY.md", + "index.d.ts", "LICENSE", "index.js" ], + "type": "commonjs", + "main": "./index.js", + "typings": "./index.d.ts", "engines": { "node": ">= 0.6" }, @@ -43,6 +49,7 @@ "lint": "eslint --plugin markdown --ext js,md .", "test": "mocha --reporter spec", "test-ci": "nyc --reporter=lcovonly --reporter=text npm test", - "test-cov": "nyc --reporter=html --reporter=text npm test" + "test-cov": "nyc --reporter=html --reporter=text npm test", + "test-types": "tsc && attw --pack" } } diff --git a/test/types.ts b/test/types.ts new file mode 100644 index 0000000..d953270 --- /dev/null +++ b/test/types.ts @@ -0,0 +1,9 @@ +import * as RangeParser from "../index.js"; +import { expectTypeOf } from "expect-type" + +expectTypeOf(RangeParser).toBeFunction(); +expectTypeOf(RangeParser).toEqualTypeOf<( + size: number, + str: string, + options?: RangeParser.Options, +) => RangeParser.Result | RangeParser.Ranges>(); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..a2009aa --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "es5", + "lib": [ "es2015" ], + "module": "commonjs", + "noEmit": true, + "strict": true + }, + "include": [ + "./test/**/*.ts", + "./**/*.d.ts" + ] +} \ No newline at end of file From b60a628d7d1346c5ac8edc4de7bb0a7d62d99d88 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Wed, 21 Jan 2026 20:57:45 -0500 Subject: [PATCH 2/4] fix: update npm-rm list for Node.js jobs and ensure type tests run conditionally --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 53cd7f5..c8f3f3e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -47,7 +47,7 @@ jobs: - name: Node.js 0.8 node-version: "0.8" npm-i: mocha@2.5.3 - npm-rm: nyc + npm-rm: nyc typescript @arethetypeswrong/cli expect-type - name: Node.js 0.10 node-version: "0.10" @@ -206,6 +206,7 @@ jobs: fi - name: Run type tests + if: steps.list_env.outputs.typescript != '' run: npm run test-types - name: Lint code From 2e17199c34acfd6addfbcefb3c4cd12b6483feac Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Wed, 21 Jan 2026 21:03:25 -0500 Subject: [PATCH 3/4] feat: restructure TypeScript test job in CI workflow --- .github/workflows/ci.yml | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c8f3f3e..449735f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -205,10 +205,6 @@ jobs: npm test fi - - name: Run type tests - if: steps.list_env.outputs.typescript != '' - run: npm run test-types - - name: Lint code if: steps.list_env.outputs.eslint != '' run: npm run lint @@ -232,3 +228,19 @@ jobs: with: github-token: ${{ secrets.GITHUB_TOKEN }} parallel-finished: true + + test_typescript: + runs-on: 'ubuntu-latest' + steps: + - uses: actions/checkout@v6 + with: + persist-credentials: false + + - uses: actions/setup-node@v6 + + - name: Install + run: | + npm install --ignore-scripts + + - name: Run typescript tests + run: npm run test-types \ No newline at end of file From 557daa9f681e8fecba88fffbd27facb288307aae Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Wed, 21 Jan 2026 21:08:50 -0500 Subject: [PATCH 4/4] feat: add npm version fixes for Node.js jobs in CI workflow --- .github/workflows/ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 449735f..caa6808 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -76,6 +76,8 @@ jobs: - name: Node.js 5.x node-version: "5.12" npm-i: mocha@5.2.0 nyc@11.9.0 + # fixes https://github.com/npm/cli/issues/681 + npm-version: "npm@3.10.10" - name: Node.js 6.x node-version: "6.17" @@ -156,6 +158,10 @@ jobs: fi dirname "$(nvm which ${{ matrix.node-version }})" >> "$GITHUB_PATH" + - name: Npm version fixes + if: ${{matrix.npm-version != ''}} + run: npm install -g ${{ matrix.npm-version }} + - name: Configure npm run: | if [[ "$(npm config get package-lock)" == "true" ]]; then