Skip to content

Commit 9cdcb3b

Browse files
authored
feat: type checking & type generation (#41)
2 parents 5785509 + 1ce2823 commit 9cdcb3b

20 files changed

+400
-120
lines changed

.eslintrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": [
3+
"standard",
4+
"plugin:@typescript-eslint/recommended"
5+
],
6+
"plugins": [
7+
"@typescript-eslint"
8+
],
9+
"rules": {
10+
"@typescript-eslint/explicit-module-boundary-types": "off",
11+
"@typescript-eslint/ban-ts-comment": "off"
12+
}
13+
}

.github/workflows/typecheck.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
on:
2+
push:
3+
branches:
4+
- master
5+
- main
6+
- default
7+
pull_request:
8+
branches:
9+
- '**'
10+
11+
name: Typecheck
12+
jobs:
13+
check:
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
node-version: [12.x]
18+
steps:
19+
- uses: actions/checkout@v1
20+
- name: Use Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v1
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
- name: Install dependencies
25+
run: npm install
26+
- name: Typecheck
27+
uses: gozala/[email protected]

package.json

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
"main": "index.js",
66
"type": "module",
77
"scripts": {
8-
"build": "npm_config_yes=true npx ipjs@latest build --tests",
8+
"build": "npm run build:js && npm run build:types",
9+
"build:js": "npm_config_yes=true npx ipjs@latest build --tests",
10+
"build:types": "tsc --emitDeclarationOnly --declarationDir dist/types",
911
"build:vendor": "npm run build:vendor:varint && npm run build:vendor:base-x",
1012
"build:vendor:varint": "npx brrp -x varint > vendor/varint.js",
1113
"build:vendor:base-x": "npx brrp -x @multiformats/base-x > vendor/base-x.js",
1214
"publish": "npm_config_yes=true npx ipjs@latest publish",
1315
"lint": "standard",
14-
"test:cjs": "npm run build && mocha dist/cjs/node-test/test-*.js && npm run test:cjs:browser",
16+
"check": "tsc --noEmit --noErrorTruncation",
17+
"test:cjs": "npm run build:js && mocha dist/cjs/node-test/test-*.js && npm run test:cjs:browser",
1518
"test:node": "hundreds mocha test/test-*.js",
1619
"test:cjs:browser": "polendina --cleanup dist/cjs/browser-test/test-*.js",
1720
"test": "npm run lint && npm run test:node && npm run test:cjs",
@@ -78,11 +81,16 @@
7881
"hundreds": "0.0.7",
7982
"mocha": "^8.1.1",
8083
"polendina": "^1.0.0",
81-
"standard": "^14.3.4"
84+
"standard": "^14.3.4",
85+
"typescript": "^4.0.3",
86+
"@types/node": "14.11.7",
87+
"@typescript-eslint/eslint-plugin": "^4.4.0",
88+
"@typescript-eslint/parser": "^4.4.0"
8289
},
8390
"standard": {
8491
"ignore": [
85-
"dist"
92+
"dist",
93+
"vendor"
8694
]
8795
},
8896
"dependencies": {
@@ -100,5 +108,12 @@
100108
"bugs": {
101109
"url": "https://github.com/multiformats/js-multiformats/issues"
102110
},
103-
"homepage": "https://github.com/multiformats/js-multiformats#readme"
111+
"homepage": "https://github.com/multiformats/js-multiformats#readme",
112+
"typesVersions": {
113+
"*": {
114+
"*": [
115+
"dist/types/*"
116+
]
117+
}
118+
}
104119
}

src/bases/base.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @ts-check
2-
31
/**
42
* @typedef {import('./interface').BaseEncoder} BaseEncoder
53
* @typedef {import('./interface').BaseDecoder} BaseDecoder
@@ -162,7 +160,7 @@ class ComposedDecoder {
162160
* @returns {Uint8Array}
163161
*/
164162
decode (input) {
165-
const prefix = input[0]
163+
const prefix = /** @type {Prefix} */ (input[0])
166164
const decoder = this.decoders[prefix]
167165
if (decoder) {
168166
return decoder.decode(input)
@@ -211,6 +209,9 @@ export class Codec {
211209
return this.encoder.encode(input)
212210
}
213211

212+
/**
213+
* @param {string} input
214+
*/
214215
decode (input) {
215216
return this.decoder.decode(input)
216217
}

src/bases/base32.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// @ts-check
2-
31
import { withAlphabet } from './base.js'
42

3+
/**
4+
* @param {string} input
5+
* @param {input} alphabet
6+
*/
57
function decode (input, alphabet) {
68
input = input.replace(new RegExp('=', 'g'), '')
79
const length = input.length
@@ -25,6 +27,10 @@ function decode (input, alphabet) {
2527
return output
2628
}
2729

30+
/**
31+
* @param {Uint8Array} buffer
32+
* @param {string} alphabet
33+
*/
2834
function encode (buffer, alphabet) {
2935
const length = buffer.byteLength
3036
const view = new Uint8Array(buffer)

src/bases/base58.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
// @ts-check
2-
31
import baseX from '../../vendor/base-x.js'
42
import { coerce } from '../bytes.js'
53
import { from } from './base.js'
64

5+
/**
6+
* @param {string} alphabet
7+
*/
78
const implement = (alphabet) => {
89
const { encode, decode } = baseX(alphabet)
910
return {
1011
encode,
12+
/**
13+
* @param {string} text
14+
*/
1115
decode: text => coerce(decode(text))
1216
}
1317
}

src/bases/base64-browser.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
// @ts-check
2-
31
/* globals btoa, atob */
42
import b64 from './base64.js'
53

64
const { base64, base64pad, base64url, base64urlpad, __browser } = b64({
7-
encode: b => btoa([].reduce.call(b, (p, c) => p + String.fromCharCode(c), '')),
5+
encode: b => btoa(b.reduce((p, c) => p + String.fromCharCode(c), '')),
86
decode: str => Uint8Array.from(atob(str), c => c.charCodeAt(0)),
97
__browser: true
108
})

0 commit comments

Comments
 (0)