Skip to content

Commit bb4204e

Browse files
Extract common utilities to a module.
1 parent 3228a68 commit bb4204e

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed
File renamed without changes.

src/appended-script.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Module to add and remove Python scripts into and from a MicroPython hex.
33
*/
44
import MemoryMap from 'nrf-intel-hex';
5+
import { bytesToStr, strToBytes } from './common';
56

67
const enum UserCodeBlock {
78
/** User script located at specific flash address. */
@@ -27,34 +28,6 @@ const enum UserCodeBlock {
2728
*/
2829
const HEX_INSERTION_POINT = ':::::::::::::::::::::::::::::::::::::::::::\n';
2930

30-
/**
31-
* Converts a string into a byte array of characters.
32-
* TODO: Update to encode to UTF-8 correctly.
33-
* @param str - String to convert to bytes.
34-
* @returns A byte array with the encoded data.
35-
*/
36-
function strToBytes(str: string): Uint8Array {
37-
const data: Uint8Array = new Uint8Array(str.length);
38-
for (let i: number = 0; i < str.length; i++) {
39-
// TODO: This will only keep the LSB from the UTF-16 code points
40-
data[i] = str.charCodeAt(i);
41-
}
42-
return data;
43-
}
44-
45-
/**
46-
* Converts a byte array into a string of characters.
47-
* TODO: This currently only deals with single byte characters, so needs to
48-
* be expanded to support UTF-8 characters longer than 1 byte.
49-
* @param byteArray - Array of bytes to convert.
50-
* @returns String output from the conversion.
51-
*/
52-
function bytesToStr(byteArray: Uint8Array): string {
53-
const result: string[] = [];
54-
byteArray.forEach((element) => result.push(String.fromCharCode(element)));
55-
return result.join('');
56-
}
57-
5831
/**
5932
* Removes the old insertion line the input Intel Hex string contains it.
6033
* @param intelHex String with the intel hex lines.

src/common.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* General utilities.
3+
*/
4+
5+
/**
6+
* Converts a string into a byte array of characters.
7+
* TODO: Update to encode to UTF-8 correctly.
8+
* @param str - String to convert to bytes.
9+
* @returns A byte array with the encoded data.
10+
*/
11+
function strToBytes(str: string): Uint8Array {
12+
const data: Uint8Array = new Uint8Array(str.length);
13+
for (let i: number = 0; i < str.length; i++) {
14+
// TODO: This will only keep the LSB from the UTF-16 code points
15+
data[i] = str.charCodeAt(i);
16+
}
17+
return data;
18+
}
19+
20+
/**
21+
* Converts a byte array into a string of characters.
22+
* TODO: This currently only deals with single byte characters, so needs to
23+
* be expanded to support UTF-8 characters longer than 1 byte.
24+
* @param byteArray - Array of bytes to convert.
25+
* @returns String output from the conversion.
26+
*/
27+
function bytesToStr(byteArray: Uint8Array): string {
28+
const result: string[] = [];
29+
byteArray.forEach((element) => result.push(String.fromCharCode(element)));
30+
return result.join('');
31+
}
32+
33+
export { strToBytes, bytesToStr };

0 commit comments

Comments
 (0)