Skip to content

Commit 52d7006

Browse files
authored
feat: react native support #13
2 parents 075f2fe + eb7a793 commit 52d7006

File tree

6 files changed

+5225
-488
lines changed

6 files changed

+5225
-488
lines changed

.github/workflows/node.js.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ jobs:
4141
run: npm run test:es --if-present
4242
- name: Test CommonJS
4343
run: npm run test:cjs --if-present
44+
- name: Test React Native
45+
run: npm run test:react-native --if-present
4446
validate-types:
4547
name: Check type use
4648
runs-on: ubuntu-latest

Readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
This package provides [TextEncoder][] and [TextDecoder][] [Encoding Standard][]
88
APIs in a universal package. In the browsers it just exposes existing globals,
99
in nodejs it exposes globals in newer node versions and ones from `util` module
10-
in older versions.
10+
in older versions, and in the React Native environments it exposes these from
11+
the [@zxing/text-encoding](https://www.npmjs.com/package/@zxing/text-encoding)
12+
polyfill (installed as an optional dependency).
1113

1214
Package also works as ES module and CommonJS module.
1315

package.json

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
],
99
"type": "module",
1010
"browser": "./src/lib.browser.js",
11+
"react-native": "./src/lib.react-native.js",
1112
"main": "./src/lib.cjs",
1213
"module": "./src/lib.js",
1314
"types": "./src/lib.d.ts",
@@ -21,19 +22,39 @@
2122
"scripts": {
2223
"test:node": "mocha test/test-*.spec.cjs",
2324
"test:browser": "playwright-test test/test-*.cjs",
24-
"test:es": "mocha test/test-*.spec.js",
25+
"test:react-native": "jest test/test-lib.react-native.spec.js",
26+
"test:es": "mocha test/test-lib.spec.js",
2527
"test:cjs": "npm run test:node && npm run test:browser",
2628
"test:types:ts": "npm test --prefix test/ts-use",
2729
"test:types:esm": "npm test --prefix test/esm-use",
2830
"test:types:cjs": "npm test --prefix test/cjs-use",
2931
"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"
32+
"test": "npm run test:es && npm run test:cjs && npm run test:react-native && npm run test:types"
3133
},
3234
"license": "MIT",
3335
"author": "Irakli Gozalishvili <dev@gozala.io>",
3436
"homepage": "https://github.com/gozala/web-encoding",
3537
"devDependencies": {
38+
"jest": "^26.6.3",
39+
"metro-react-native-babel-preset": "^0.64.0",
3640
"mocha": "8.2.1",
37-
"playwright-test": "1.2.0"
41+
"playwright-test": "1.2.0",
42+
"react-native": "^0.63.4"
43+
},
44+
"optionalDependencies": {
45+
"@zxing/text-encoding": "0.9.0"
46+
},
47+
"jest": {
48+
"preset": "react-native",
49+
"transform": {
50+
"\\.js$": [
51+
"babel-jest",
52+
{
53+
"presets": [
54+
"module:metro-react-native-babel-preset"
55+
]
56+
}
57+
]
58+
}
3859
}
3960
}

src/lib.react-native.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict"
2+
3+
const textEncoding = require("@zxing/text-encoding")
4+
exports.TextEncoder = textEncoding.TextEncoder
5+
exports.TextDecoder = textEncoding.TextDecoder

test/test-lib.react-native.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.react-native.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)