@quentinadam/hex 0.1.2

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,22 @@
1
+ # @quentinadam/hex
2
+
3
+ [![JSR][jsr-image]][jsr-url] [![NPM][npm-image]][npm-url] [![CI][ci-image]][ci-url]
4
+
5
+ A simple library to encode and decode hex strings.
6
+
7
+ ## Usage
8
+
9
+ ```ts
10
+ import * as hex from '@quentinadam/hex';
11
+
12
+ hex.decode('000102'); // returns Uint8Array([0, 1, 2])
13
+
14
+ hex.encode(new Uint8Array([0, 1, 2])); // returns '000102'
15
+ ```
16
+
17
+ [ci-image]: https://img.shields.io/github/actions/workflow/status/quentinadam/deno-hex/ci.yml?branch=main&logo=github&style=flat-square
18
+ [ci-url]: https://github.com/quentinadam/deno-hex/actions/workflows/ci.yml
19
+ [npm-image]: https://img.shields.io/npm/v/@quentinadam/hex.svg?style=flat-square
20
+ [npm-url]: https://npmjs.org/package/@quentinadam/hex
21
+ [jsr-image]: https://jsr.io/badges/@quentinadam/hex?style=flat-square
22
+ [jsr-url]: https://jsr.io/@quentinadam/hex
package/dist/hex.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Decodes a hex encoded string into a Uint8Array buffer.
3
+ *
4
+ * @param string The hex encoded string.
5
+ * @returns The decoded buffer.
6
+ */
7
+ export declare function decode(hex: string): Uint8Array;
8
+ /**
9
+ * Encodes a Uint8Array buffer into a hex string.
10
+ *
11
+ * @param buffer The buffer to encode.
12
+ * @returns The hex encoded string.
13
+ */
14
+ export declare function encode(buffer: Uint8Array): string;
package/dist/hex.js ADDED
@@ -0,0 +1,25 @@
1
+ import assert from '@quentinadam/assert';
2
+ /**
3
+ * Decodes a hex encoded string into a Uint8Array buffer.
4
+ *
5
+ * @param string The hex encoded string.
6
+ * @returns The decoded buffer.
7
+ */
8
+ export function decode(hex) {
9
+ assert(/^[0-9a-fA-F]*$/.test(hex), 'Hex string must only contain hex characters');
10
+ assert(hex.length % 2 === 0, 'Hex string length must be even');
11
+ const buffer = new Uint8Array(hex.length / 2);
12
+ for (let i = 0; i < hex.length; i += 2) {
13
+ buffer[i / 2] = parseInt(hex.slice(i, i + 2), 16);
14
+ }
15
+ return buffer;
16
+ }
17
+ /**
18
+ * Encodes a Uint8Array buffer into a hex string.
19
+ *
20
+ * @param buffer The buffer to encode.
21
+ * @returns The hex encoded string.
22
+ */
23
+ export function encode(buffer) {
24
+ return Array.from(buffer).map((byte) => byte.toString(16).padStart(2, '0')).join('');
25
+ }
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@quentinadam/hex",
3
+ "version": "0.1.2",
4
+ "description": "A simple library to encode and decode hex strings",
5
+ "license": "MIT",
6
+ "author": "Quentin Adam",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/quentinadam/deno-hex.git"
10
+ },
11
+ "type": "module",
12
+ "exports": "./dist/hex.js",
13
+ "files": [
14
+ "dist",
15
+ "README.md"
16
+ ],
17
+ "dependencies": {
18
+ "@quentinadam/assert": "^0.1.10"
19
+ }
20
+ }