@pezkuwi/x-textencoder 14.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # @pezkuwi/x-textencoder
2
+
3
+ A cross-environment TextEncoder.
4
+
5
+ Install it via `yarn add @pezkuwi/x-textencoder`
6
+
7
+ ```js
8
+ import { TextEncoder } from '@pezkuwi/x-textencoder';
9
+
10
+ ...
11
+ const encoder = new TextEncoder();
12
+ ```
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "author": "Jaco Greeff <jacogr@gmail.com>",
3
+ "bugs": "https://github.com/pezkuwichain/pezkuwi-common/issues",
4
+ "description": "A TextDecoder replacement",
5
+ "engines": {
6
+ "node": ">=18"
7
+ },
8
+ "homepage": "https://github.com/pezkuwichain/pezkuwi-common/tree/master/packages/x-textencoder#readme",
9
+ "license": "Apache-2.0",
10
+ "name": "@pezkuwi/x-textencoder",
11
+ "repository": {
12
+ "directory": "packages/x-textencoder",
13
+ "type": "git",
14
+ "url": "https://github.com/pezkuwichain/pezkuwi-common.git"
15
+ },
16
+ "sideEffects": false,
17
+ "type": "module",
18
+ "version": "14.0.1",
19
+ "browser": "browser.js",
20
+ "main": "node.js",
21
+ "react-native": "react-native.js",
22
+ "dependencies": {
23
+ "@pezkuwi/x-global": "14.0.1",
24
+ "tslib": "^2.8.0"
25
+ }
26
+ }
package/src/browser.ts ADDED
@@ -0,0 +1,10 @@
1
+ // Copyright 2017-2025 @polkadot/x-textencoder authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { extractGlobal } from '@pezkuwi/x-global';
5
+
6
+ import { TextEncoder as Fallback } from './fallback.js';
7
+
8
+ export { packageInfo } from './packageInfo.js';
9
+
10
+ export const TextEncoder = /*#__PURE__*/ extractGlobal('TextEncoder', Fallback);
@@ -0,0 +1,14 @@
1
+ // Copyright 2017-2025 @polkadot/x-textencoder authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@polkadot/dev-test/globals.d.ts" />
5
+
6
+ import { TextEncoder } from './fallback.js';
7
+
8
+ describe('TextEncoder (fallback)', (): void => {
9
+ it('encodes correctly', (): void => {
10
+ expect(
11
+ new TextEncoder().encode('abc')
12
+ ).toEqual(new Uint8Array([97, 98, 99]));
13
+ });
14
+ });
@@ -0,0 +1,16 @@
1
+ // Copyright 2017-2025 @polkadot/x-textencoder authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // This is very limited, only handling Ascii values
5
+ export class TextEncoder {
6
+ encode (value: string): Uint8Array {
7
+ const count = value.length;
8
+ const u8a = new Uint8Array(count);
9
+
10
+ for (let i = 0; i < count; i++) {
11
+ u8a[i] = value.charCodeAt(i);
12
+ }
13
+
14
+ return u8a;
15
+ }
16
+ }
package/src/mod.ts ADDED
@@ -0,0 +1,4 @@
1
+ // Copyright 2017-2025 @polkadot/x-textencoder authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export * from './browser.js';
@@ -0,0 +1,25 @@
1
+ // Copyright 2017-2025 @polkadot/x-textencoder authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@polkadot/dev-test/globals.d.ts" />
5
+
6
+ import { xglobal } from '@pezkuwi/x-global';
7
+
8
+ // @ts-expect-error Clearing this, it is obviously not valid in normal code
9
+ xglobal.TextEncoder = undefined;
10
+
11
+ describe('TextEncoder (node)', (): void => {
12
+ let TE: typeof TextEncoder;
13
+
14
+ beforeEach(async (): Promise<void> => {
15
+ const node = await import('./node.js');
16
+
17
+ TE = node.TextEncoder;
18
+ });
19
+
20
+ it('encodes correctly', (): void => {
21
+ expect(
22
+ new TE().encode('abc')
23
+ ).toEqual(new Uint8Array([97, 98, 99]));
24
+ });
25
+ });
package/src/node.ts ADDED
@@ -0,0 +1,23 @@
1
+ // Copyright 2017-2025 @polkadot/x-textencoder authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import util from 'node:util';
5
+
6
+ import { extractGlobal } from '@pezkuwi/x-global';
7
+
8
+ export { packageInfo } from './packageInfo.js';
9
+
10
+ class Fallback {
11
+ #encoder: util.TextEncoder;
12
+
13
+ constructor () {
14
+ this.#encoder = new util.TextEncoder();
15
+ }
16
+
17
+ // For a Jest 26.0.1 environment, Buffer !== Uint8Array
18
+ encode (value: string): Uint8Array {
19
+ return Uint8Array.from(this.#encoder.encode(value));
20
+ }
21
+ }
22
+
23
+ export const TextEncoder = /*#__PURE__*/ extractGlobal('TextEncoder', Fallback);
@@ -0,0 +1,6 @@
1
+ // Copyright 2017-2025 @polkadot/x-textencoder authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // Do not edit, auto-generated by @polkadot/dev
5
+
6
+ export const packageInfo = { name: '@polkadot/x-textencoder', path: 'auto', type: 'auto', version: '14.0.1' };
@@ -0,0 +1,4 @@
1
+ // Copyright 2017-2025 @polkadot/x-textencoder authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export * from './browser.js';
package/src/shim.ts ADDED
@@ -0,0 +1,7 @@
1
+ // Copyright 2017-2025 @polkadot/x-textencoder authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { exposeGlobal } from '@pezkuwi/x-global';
5
+ import { TextEncoder } from '@pezkuwi/x-textencoder';
6
+
7
+ exposeGlobal('TextEncoder', TextEncoder);
@@ -0,0 +1,8 @@
1
+ // Copyright 2017-2025 @polkadot/x-textencoder authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { TextEncoder as BrowserTE } from './browser.js';
5
+ import { TextEncoder as NodeTE } from './node.js';
6
+
7
+ console.log(new BrowserTE().encode('abc'));
8
+ console.log(new NodeTE().encode('abc'));
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "baseUrl": "..",
5
+ "outDir": "./build",
6
+ "rootDir": "./src"
7
+ },
8
+ "exclude": [
9
+ "**/mod.ts",
10
+ "**/*.spec.ts"
11
+ ],
12
+ "references": [
13
+ { "path": "../x-global/tsconfig.build.json" }
14
+ ]
15
+ }
@@ -0,0 +1,17 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "baseUrl": "..",
5
+ "outDir": "./build",
6
+ "rootDir": "./src",
7
+ "emitDeclarationOnly": false,
8
+ "noEmit": true
9
+ },
10
+ "include": [
11
+ "**/*.spec.ts"
12
+ ],
13
+ "references": [
14
+ { "path": "../x-global/tsconfig.build.json" },
15
+ { "path": "../x-textencoder/tsconfig.build.json" }
16
+ ]
17
+ }