@quentinadam/hex 0.1.5 → 0.1.7
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/dist/hex.d.ts +1 -0
- package/dist/hex.d.ts.map +1 -0
- package/dist/hex.js +1 -0
- package/dist/hex.js.map +1 -0
- package/package.json +5 -3
- package/src/hex.ts +27 -0
package/dist/hex.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../src/hex.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAAC,WAAW,CAAC,CAQ3D;AAED;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,WAAW,CAAC,GAAG,MAAM,CAE9D"}
|
package/dist/hex.js
CHANGED
package/dist/hex.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hex.js","sourceRoot":"","sources":["../src/hex.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,qBAAqB,CAAC;AAEzC;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CAAC,GAAW;IAChC,MAAM,CAAC,gBAAgB,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,6CAA6C,CAAC,CAAC;IAClF,MAAM,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,gCAAgC,CAAC,CAAC;IAC/D,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IAC9C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QACvC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IACpD,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CAAC,MAA+B;IACpD,OAAO,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACvF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quentinadam/hex",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "A simple library to encode and decode hex strings",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Quentin Adam",
|
|
@@ -11,10 +11,12 @@
|
|
|
11
11
|
"type": "module",
|
|
12
12
|
"exports": "./dist/hex.js",
|
|
13
13
|
"files": [
|
|
14
|
+
"src",
|
|
14
15
|
"dist",
|
|
15
|
-
"README.md"
|
|
16
|
+
"README.md",
|
|
17
|
+
"!**/*.test.ts"
|
|
16
18
|
],
|
|
17
19
|
"dependencies": {
|
|
18
|
-
"@quentinadam/assert": "^0.1.
|
|
20
|
+
"@quentinadam/assert": "^0.1.16"
|
|
19
21
|
}
|
|
20
22
|
}
|
package/src/hex.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import assert from '@quentinadam/assert';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Decodes a hex encoded string into a Uint8Array buffer.
|
|
5
|
+
*
|
|
6
|
+
* @param string The hex encoded string.
|
|
7
|
+
* @returns The decoded buffer.
|
|
8
|
+
*/
|
|
9
|
+
export function decode(hex: string): Uint8Array<ArrayBuffer> {
|
|
10
|
+
assert(/^[0-9a-fA-F]*$/.test(hex), 'Hex string must only contain hex characters');
|
|
11
|
+
assert(hex.length % 2 === 0, 'Hex string length must be even');
|
|
12
|
+
const buffer = new Uint8Array(hex.length / 2);
|
|
13
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
14
|
+
buffer[i / 2] = parseInt(hex.slice(i, i + 2), 16);
|
|
15
|
+
}
|
|
16
|
+
return buffer;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Encodes a Uint8Array buffer into a hex string.
|
|
21
|
+
*
|
|
22
|
+
* @param buffer The buffer to encode.
|
|
23
|
+
* @returns The hex encoded string.
|
|
24
|
+
*/
|
|
25
|
+
export function encode(buffer: Uint8Array<ArrayBuffer>): string {
|
|
26
|
+
return Array.from(buffer).map((byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
27
|
+
}
|