Skip to content

Commit a6b98cc

Browse files
committed
feat: initial version
0 parents  commit a6b98cc

File tree

10 files changed

+3451
-0
lines changed

10 files changed

+3451
-0
lines changed

.github/workflows/node.js.yml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: Node.js CI
5+
6+
on:
7+
push:
8+
branches: [default]
9+
pull_request:
10+
branches: [default]
11+
12+
jobs:
13+
test:
14+
name: Build & Test
15+
runs-on: ${{ matrix.os }}
16+
17+
strategy:
18+
matrix:
19+
node-version: [10.x, 12.x, 14.x]
20+
os: [ubuntu-latest, windows-latest, macos-latest]
21+
22+
steps:
23+
- uses: actions/checkout@v2
24+
- name: Use Node.js ${{ matrix.node-version }}
25+
uses: actions/setup-node@v1
26+
with:
27+
node-version: ${{ matrix.node-version }}
28+
- name: Cache node_modules
29+
id: cache-modules
30+
uses: actions/cache@v1
31+
with:
32+
path: node_modules
33+
key: ${{ matrix.node-version }}-${{ runner.OS }}-build-${{ hashFiles('package.json') }}
34+
- name: Install
35+
if: steps.cache-modules.outputs.cache-hit != 'true'
36+
run: npm install
37+
- name: Type Check
38+
run: npm run check --if-present
39+
- name: Test ES
40+
run: npm run test:es --if-present
41+
- name: Test CommonJS
42+
run: npm run test:cjs --if-present
43+
publish:
44+
name: Publish
45+
needs: test
46+
runs-on: ubuntu-latest
47+
if: github.event_name == 'push' && github.ref == 'refs/heads/default'
48+
steps:
49+
- uses: actions/checkout@v2
50+
- name: Cache node_modules
51+
id: cache-modules
52+
uses: actions/cache@v1
53+
with:
54+
path: node_modules
55+
key: 12.x-${{ runner.OS }}-build-${{ hashFiles('package.json') }}
56+
- name: Install
57+
if: steps.cache-modules.outputs.cache-hit != 'true'
58+
run: npm install
59+
- name: Test
60+
run: npm test
61+
62+
- name: Publish
63+
uses: mikeal/merge-release@master
64+
env:
65+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
66+
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

Readme.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# web-encoding
2+
3+
![Node.js CI][node.js ci]
4+
[![package][version.icon] ![downloads][downloads.icon]][package.url]
5+
[![styled with prettier][prettier.icon]][prettier.url]
6+
7+
This package provides [TextEncoder][] and [TextDecoder][] [Encoding Standard][]
8+
APIs in a universal package. In the browsers it just exposes existing globals,
9+
in nodejs it exposes globals in newer node versions and ones from `util` module
10+
in older versions.
11+
12+
Package also works as ES module and CommonJS module.
13+
14+
### Usage
15+
16+
```js
17+
import { TextEncoder, TextDecoder } from "web-encoding"
18+
```
19+
20+
## Install
21+
22+
npm install web-encoding
23+
24+
[node.js ci]: https://github.com/Gozala/web-encoding/workflows/Node.js%20CI/badge.svg
25+
[version.icon]: https://img.shields.io/npm/v/web-encoding.svg
26+
[downloads.icon]: https://img.shields.io/npm/dm/web-encoding.svg
27+
[package.url]: https://npmjs.org/package/web-encoding
28+
[downloads.image]: https://img.shields.io/npm/dm/web-encoding.svg
29+
[downloads.url]: https://npmjs.org/package/web-encoding
30+
[prettier.icon]: https://img.shields.io/badge/styled_with-prettier-ff69b4.svg
31+
[prettier.url]: https://github.com/prettier/prettier
32+
[ts-jsdoc]: https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html
33+
[textencoder]: https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder
34+
[textdecoder]: https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
35+
[encoding standard]: https://encoding.spec.whatwg.org/

package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "web-encoding",
3+
"version": "1.0.0",
4+
"description": "TextEncoder and TextDecoder APIs from Encoding Standard APIs in a universal package",
5+
"keywords": [
6+
"TextEncoder",
7+
"TextDecoder"
8+
],
9+
"type": "module",
10+
"browser": "./src/lib.browser.js",
11+
"main": "./src/lib.cjs",
12+
"module": "./src/lib.js",
13+
"types": "./src/lib.d.ts",
14+
"exports": {
15+
".": {
16+
"import": "./src/lib.js",
17+
"require": "./src/lib.cjs.js",
18+
"browser": "./src/lib.browser.js"
19+
}
20+
},
21+
"scripts": {
22+
"test:node": "mocha test/test-*.spec.cjs",
23+
"test:browser": "polendina --cleanup test/test-*.cjs",
24+
"test:es": "mocha test/test-*.spec.js",
25+
"test:cjs": "npm run test:node && npm run test:browser",
26+
"test": "npm run test:es && npm run test:cjs"
27+
},
28+
"license": "MIT",
29+
"author": "Irakli Gozalishvili <[email protected]>",
30+
"homepage": "https://github.com/gozala/web-encoding",
31+
"devDependencies": {
32+
"mocha": "^8.0.1",
33+
"polendina": "^1.0.0"
34+
}
35+
}

src/lib.browser.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict"
2+
3+
exports.TextEncoder = TextEncoder
4+
exports.TextDecoder = TextDecoder

src/lib.cjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict"
2+
3+
exports.TextEncoder =
4+
typeof TextEncoder !== "undefined" ? TextEncoder : require("util").TextEncoder
5+
6+
exports.TextDecoder =
7+
typeof TextDecoder !== "undefined" ? TextDecoder : require("util").TextDecoder

src/lib.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { TextEncoder, TextDecoder }

src/lib.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict"
2+
3+
// In node `export { TextEncoder }` throws:
4+
// "Export 'TextEncoder' is not definedin module"
5+
// To workaround we first define constants and then export with as.
6+
const Encoder = TextEncoder
7+
const Decoder = TextDecoder
8+
9+
export { Encoder as TextEncoder, Decoder as TextDecoder }

test/test-lib.spec.cjs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const { TextEncoder, TextDecoder } = require("..")
2+
const assert = require("assert")
3+
4+
describe("text encode/decode", () => {
5+
const data = Uint8Array.from([
6+
104,
7+
101,
8+
108,
9+
108,
10+
111,
11+
32,
12+
119,
13+
111,
14+
114,
15+
108,
16+
100,
17+
])
18+
19+
it("can encode text", () => {
20+
const bytes = new TextEncoder().encode("hello world")
21+
assert.deepStrictEqual(bytes, data)
22+
})
23+
24+
it("can decode text", () => {
25+
const text = new TextDecoder().decode(data)
26+
assert.equal(text, "hello world")
27+
})
28+
})

test/test-lib.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { TextEncoder, TextDecoder } from "../src/lib.js"
2+
import assert from "assert"
3+
4+
describe("text encode/decode", () => {
5+
const data = Uint8Array.from([
6+
104,
7+
101,
8+
108,
9+
108,
10+
111,
11+
32,
12+
119,
13+
111,
14+
114,
15+
108,
16+
100,
17+
])
18+
19+
it("can encode text", () => {
20+
const bytes = new TextEncoder().encode("hello world")
21+
assert.deepStrictEqual(bytes, data)
22+
})
23+
24+
it("can decode text", () => {
25+
const text = new TextDecoder().decode(data)
26+
assert.equal(text, "hello world")
27+
})
28+
})

0 commit comments

Comments
 (0)