Skip to content

Commit 22a2466

Browse files
authored
Merge pull request #11 from Gozala/fix-types
fix: type declarations
2 parents 0f10077 + d883fe9 commit 22a2466

File tree

11 files changed

+112
-5
lines changed

11 files changed

+112
-5
lines changed

.github/workflows/node.js.yml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,29 @@ jobs:
3737
- name: Type Check
3838
run: npm run check --if-present
3939
- name: Test ES
40-
if: ${{matrix.node-version < 12}}
40+
if: ${{!startsWith(matrix.node-version, '10.')}}
4141
run: npm run test:es --if-present
4242
- name: Test CommonJS
4343
run: npm run test:cjs --if-present
44+
validate-types:
45+
name: Check type use
46+
runs-on: ubuntu-latest
47+
strategy:
48+
matrix:
49+
test-package: ['ts', 'esm', 'cjs']
50+
steps:
51+
- uses: actions/checkout@v2
52+
- name: Cache node_modules
53+
id: cache-modules
54+
uses: actions/cache@v1
55+
with:
56+
path: node_modules
57+
key: 12.x-${{ runner.OS }}-build-${{ hashFiles('package.json') }}
58+
- name: Install
59+
if: steps.cache-modules.outputs.cache-hit != 'true'
60+
run: npm install
61+
- name: Test ${{ matrix.test-package }}
62+
run: npm run test:types:${{matrix.test-package}}
4463
publish:
4564
name: Publish
4665
needs: test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
test/**/package-lock.json

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
"test:browser": "playwright-test test/test-*.cjs",
2424
"test:es": "mocha test/test-*.spec.js",
2525
"test:cjs": "npm run test:node && npm run test:browser",
26-
"test": "npm run test:es && npm run test:cjs"
26+
"test:types:ts": "npm test --prefix test/ts-use",
27+
"test:types:esm": "npm test --prefix test/esm-use",
28+
"test:types:cjs": "npm test --prefix test/cjs-use",
29+
"test:types": "npm run test:types:ts && npm run test:types:esm && npm run test:types:cjs",
30+
"test": "npm run test:es && npm run test:cjs && npm run test:types"
2731
},
2832
"license": "MIT",
2933
"author": "Irakli Gozalishvili <[email protected]>",

src/lib.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type Encoder = TextEncoder
2-
type Decoder = TextDecoder
1+
declare var TextEncoder: typeof self.TextEncoder
2+
declare var TextDecoder: typeof self.TextDecoder
33

4-
export { Encoder as TextEncoder, Decoder as TextDecoder }
4+
export { TextEncoder, TextDecoder }

test/cjs-use/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "cjs-use",
3+
"private": true,
4+
"dependencies": {
5+
"web-encoding": "file:../.."
6+
},
7+
"scripts": {
8+
"test": "npm install && npx typescript --noEmit --allowJs --checkJs src/main.js"
9+
}
10+
}

test/cjs-use/src/main.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @ts-check
2+
3+
const { TextEncoder, TextDecoder } = require("web-encoding")
4+
5+
/**
6+
* @param {string} text
7+
* @returns {Uint8Array}
8+
*/
9+
const encode = (text) =>
10+
new TextEncoder().encode(text)
11+
12+
/**
13+
* @param {Uint8Array} bytes
14+
* @returns {string}
15+
*/
16+
const decode = (bytes) =>
17+
new TextDecoder().decode(bytes)
18+
19+
module.exports = { encode, decode }

test/esm-use/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "esm-use",
3+
"private": true,
4+
"dependencies": {
5+
"web-encoding": "file:../.."
6+
},
7+
"scripts": {
8+
"test": "npm install && npx typescript --noEmit --allowJs --checkJs src/main.js"
9+
}
10+
}

test/esm-use/src/main.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @ts-check
2+
3+
import { TextEncoder, TextDecoder } from "web-encoding"
4+
5+
/**
6+
* @param {string} text
7+
* @returns {Uint8Array}
8+
*/
9+
export const encode = (text) =>
10+
new TextEncoder().encode(text)
11+
12+
/**
13+
* @param {Uint8Array} bytes
14+
* @returns {string}
15+
*/
16+
export const decode = (bytes) =>
17+
new TextDecoder().decode(bytes)

test/ts-use/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "ts-use",
3+
"private": true,
4+
"dependencies": {
5+
"web-encoding": "file:../.."
6+
},
7+
"scripts": {
8+
"test": "npm install && npx typescript --noEmit"
9+
}
10+
}

test/ts-use/src/main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { TextEncoder, TextDecoder } from "web-encoding"
2+
3+
export const encode = (text:string):Uint8Array =>
4+
new TextEncoder().encode(text)
5+
6+
export const decode = (bytes:Uint8Array):string =>
7+
new TextDecoder().decode(bytes)

0 commit comments

Comments
 (0)