Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: type checking & type generation #41

Merged
merged 6 commits into from
Oct 10, 2020
Merged
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
13 changes: 13 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": [
"standard",
"plugin:@typescript-eslint/recommended"
],
"plugins": [
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/ban-ts-comment": "off"
}
}
27 changes: 27 additions & 0 deletions .github/workflows/typecheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
on:
push:
branches:
- master
- main
- default
pull_request:
branches:
- '**'

name: Typecheck
jobs:
check:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]
steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install
- name: Typecheck
uses: gozala/[email protected]
25 changes: 20 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
"main": "index.js",
"type": "module",
"scripts": {
"build": "npm_config_yes=true npx ipjs@latest build --tests",
"build": "npm run build:js && npm run build:types",
"build:js": "npm_config_yes=true npx ipjs@latest build --tests",
"build:types": "tsc --emitDeclarationOnly --declarationDir dist/types",
"build:vendor": "npm run build:vendor:varint && npm run build:vendor:base-x",
"build:vendor:varint": "npx brrp -x varint > vendor/varint.js",
"build:vendor:base-x": "npx brrp -x @multiformats/base-x > vendor/base-x.js",
"publish": "npm_config_yes=true npx ipjs@latest publish",
"lint": "standard",
"test:cjs": "npm run build && mocha dist/cjs/node-test/test-*.js && npm run test:cjs:browser",
"check": "tsc --noEmit --noErrorTruncation",
"test:cjs": "npm run build:js && mocha dist/cjs/node-test/test-*.js && npm run test:cjs:browser",
"test:node": "hundreds mocha test/test-*.js",
"test:cjs:browser": "polendina --cleanup dist/cjs/browser-test/test-*.js",
"test": "npm run lint && npm run test:node && npm run test:cjs",
Expand Down Expand Up @@ -78,11 +81,16 @@
"hundreds": "0.0.7",
"mocha": "^8.1.1",
"polendina": "^1.0.0",
"standard": "^14.3.4"
"standard": "^14.3.4",
"typescript": "^4.0.3",
"@types/node": "14.11.7",
"@typescript-eslint/eslint-plugin": "^4.4.0",
"@typescript-eslint/parser": "^4.4.0"
},
"standard": {
"ignore": [
"dist"
"dist",
"vendor"
]
},
"dependencies": {
Expand All @@ -100,5 +108,12 @@
"bugs": {
"url": "https://github.com/multiformats/js-multiformats/issues"
},
"homepage": "https://github.com/multiformats/js-multiformats#readme"
"homepage": "https://github.com/multiformats/js-multiformats#readme",
"typesVersions": {
"*": {
"*": [
"dist/types/*"
]
}
}
}
7 changes: 4 additions & 3 deletions src/bases/base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-check

/**
* @typedef {import('./interface').BaseEncoder} BaseEncoder
* @typedef {import('./interface').BaseDecoder} BaseDecoder
Expand Down Expand Up @@ -162,7 +160,7 @@ class ComposedDecoder {
* @returns {Uint8Array}
*/
decode (input) {
const prefix = input[0]
const prefix = /** @type {Prefix} */ (input[0])
const decoder = this.decoders[prefix]
if (decoder) {
return decoder.decode(input)
Expand Down Expand Up @@ -211,6 +209,9 @@ export class Codec {
return this.encoder.encode(input)
}

/**
* @param {string} input
*/
decode (input) {
return this.decoder.decode(input)
}
Expand Down
10 changes: 8 additions & 2 deletions src/bases/base32.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @ts-check

import { withAlphabet } from './base.js'

/**
* @param {string} input
* @param {input} alphabet
*/
function decode (input, alphabet) {
input = input.replace(new RegExp('=', 'g'), '')
const length = input.length
Expand All @@ -25,6 +27,10 @@ function decode (input, alphabet) {
return output
}

/**
* @param {Uint8Array} buffer
* @param {string} alphabet
*/
function encode (buffer, alphabet) {
const length = buffer.byteLength
const view = new Uint8Array(buffer)
Expand Down
8 changes: 6 additions & 2 deletions src/bases/base58.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// @ts-check

import baseX from '../../vendor/base-x.js'
import { coerce } from '../bytes.js'
import { from } from './base.js'

/**
* @param {string} alphabet
*/
const implement = (alphabet) => {
const { encode, decode } = baseX(alphabet)
return {
encode,
/**
* @param {string} text
*/
decode: text => coerce(decode(text))
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/bases/base64-browser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// @ts-check

/* globals btoa, atob */
import b64 from './base64.js'

const { base64, base64pad, base64url, base64urlpad, __browser } = b64({
encode: b => btoa([].reduce.call(b, (p, c) => p + String.fromCharCode(c), '')),
encode: b => btoa(b.reduce((p, c) => p + String.fromCharCode(c), '')),
decode: str => Uint8Array.from(atob(str), c => c.charCodeAt(0)),
__browser: true
})
Expand Down
Loading