Skip to content

Commit 4176e7f

Browse files
committed
feat: add TypeScript definitions
1 parent 098e332 commit 4176e7f

5 files changed

Lines changed: 69 additions & 3 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ jobs:
205205
npm test
206206
fi
207207
208+
- name: Run type tests
209+
run: npm run test-types
210+
208211
- name: Lint code
209212
if: steps.list_env.outputs.eslint != ''
210213
run: npm run lint

index.d.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* When ranges are returned, the array has a "type" property which is the type of
3+
* range that is required (most commonly, "bytes"). Each array element is an object
4+
* with a "start" and "end" property for the portion of the range.
5+
*
6+
* @returns `-1` when unsatisfiable and `-2` when syntactically invalid, ranges otherwise.
7+
*/
8+
declare function RangeParser(
9+
size: number,
10+
str: string,
11+
options?: RangeParser.Options,
12+
): RangeParser.Result | RangeParser.Ranges;
13+
14+
declare namespace RangeParser {
15+
interface Ranges extends Array<Range> {
16+
type: string;
17+
}
18+
interface Range {
19+
start: number;
20+
end: number;
21+
}
22+
interface Options {
23+
/**
24+
* The "combine" option can be set to `true` and overlapping & adjacent ranges
25+
* will be combined into a single range.
26+
*/
27+
combine?: boolean | undefined;
28+
}
29+
type ResultUnsatisfiable = -1;
30+
type ResultInvalid = -2;
31+
type Result = ResultUnsatisfiable | ResultInvalid;
32+
}
33+
34+
export = RangeParser;

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"url": "https://opencollective.com/express"
2121
},
2222
"devDependencies": {
23+
"@arethetypeswrong/cli": "^0.18.2",
2324
"deep-equal": "1.0.1",
2425
"eslint": "6.0.1",
2526
"eslint-config-standard": "13.0.1",
@@ -28,21 +29,27 @@
2829
"eslint-plugin-node": "9.1.0",
2930
"eslint-plugin-promise": "4.2.1",
3031
"eslint-plugin-standard": "4.0.0",
32+
"expect-type": "~1.2.0",
3133
"mocha": "6.1.4",
32-
"nyc": "14.1.1"
34+
"nyc": "14.1.1",
35+
"typescript": "~5.9.2"
3336
},
3437
"files": [
35-
"HISTORY.md",
38+
"index.d.ts",
3639
"LICENSE",
3740
"index.js"
3841
],
42+
"type": "commonjs",
43+
"main": "./index.js",
44+
"typings": "./index.d.ts",
3945
"engines": {
4046
"node": ">= 0.6"
4147
},
4248
"scripts": {
4349
"lint": "eslint --plugin markdown --ext js,md .",
4450
"test": "mocha --reporter spec",
4551
"test-ci": "nyc --reporter=lcovonly --reporter=text npm test",
46-
"test-cov": "nyc --reporter=html --reporter=text npm test"
52+
"test-cov": "nyc --reporter=html --reporter=text npm test",
53+
"test-types": "tsc && attw --pack"
4754
}
4855
}

test/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as RangeParser from "../index.js";
2+
import { expectTypeOf } from "expect-type"
3+
4+
expectTypeOf(RangeParser).toBeFunction();
5+
expectTypeOf(RangeParser).toEqualTypeOf<(
6+
size: number,
7+
str: string,
8+
options?: RangeParser.Options,
9+
) => RangeParser.Result | RangeParser.Ranges>();

tsconfig.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": [ "es2015" ],
5+
"module": "commonjs",
6+
"noEmit": true,
7+
"strict": true
8+
},
9+
"include": [
10+
"./test/**/*.ts",
11+
"./**/*.d.ts"
12+
]
13+
}

0 commit comments

Comments
 (0)