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
24 changes: 23 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

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

This is so that CI passes we don’t need to test the types on every Node.js version.


- name: Node.js 0.10
node-version: "0.10"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -228,3 +234,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
34 changes: 34 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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<Range> {
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;
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -28,21 +29,27 @@
"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"
},
"scripts": {
"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"
}
}
9 changes: 9 additions & 0 deletions test/types.ts
Original file line number Diff line number Diff line change
@@ -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>();
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es5",
"lib": [ "es2015" ],
"module": "commonjs",
"noEmit": true,
"strict": true
},
"include": [
"./test/**/*.ts",
"./**/*.d.ts"
]
}
Loading