Skip to content

Commit b503647

Browse files
committed
Added convert function and improved tests
1 parent b96a92a commit b503647

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

README.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,51 @@ yarn add ninprint
5555
```
5656

5757
## Examples
58+
59+
You can `print` directly to the console:
60+
5861
```javascript
59-
const { print: ninprint } = require("ninprint");
62+
const { print } = require("ninprint");
6063

61-
ninprint("Hello world.");
64+
print("Hello world.");
6265
// Output:
6366
// H e l l o [SPACE] w o r l d .
6467

65-
ninprint("Line feed:\n");
68+
print("Line feed:\n");
6669
// Output:
6770
// L i n e [SPACE] f e e d : [LF]
71+
```
72+
73+
You can use `convert` to return a string:
6874

69-
ninprint("Carriage return:\r");
75+
```javascript
76+
const { convert } = require("ninprint");
77+
78+
const test1 = convert("Carriage return:\r");
79+
console.log(test1);
7080
// Output:
7181
// C a r r i a g e [SPACE] r e t u r n : [CR]
7282

73-
ninprint("Null character:\0");
83+
const test2 = convert("Null character:\0");
84+
console.log(test2);
7485
// Output:
7586
// N u l l [SPACE] c h a r a c t e r : [NUL]
87+
```
7688

77-
ninprint(new Buffer([0x00, 0x01, 0x02, 0x03, 0x04, 0x05]));
89+
You can also pass a `Buffer` to `print`/`convert`:
90+
91+
```javascript
92+
93+
const { print, convert } = require("ninprint");
94+
95+
print(new Buffer([0x00, 0x01, 0x02, 0x03, 0x04, 0x05]));
7896
// Output:
7997
// [NUL] [SOH] [STX] [ETX] [EOT] [ENQ]
98+
99+
const test3 = convert(new Buffer([0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B]));
100+
console.log(test3);
101+
// Output:
102+
// [ACK] [BEL] [BS] [TAB] [LF] [VT]
80103
```
81104

82105
# License

index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
const characterTable = require("./character-table.json");
22

3-
const print = (value, encoding = "ascii") => {
3+
const print = (value, encoding) => console.log(convert(value, encoding));
4+
5+
const convert = (value, encoding = "ascii") => {
46

57
// Value is a String
68
if (typeof(value) === "string" || value instanceof String)
7-
return printFromString(value);
9+
return convertString(value);
810

911
// Value is a Buffer, first convert to String
1012
else if (value instanceof Buffer)
11-
return printFromString(value.toString(encoding));
13+
return convertString(value.toString(encoding));
1214

1315
// Value has an unknown type
1416
else
1517
throw `Value passed must be a String or Buffer`;
16-
}
18+
};
1719

18-
const printFromString = string => {
20+
const convertString = string => {
1921

2022
const symbols = [];
2123

2224
for (let i = 0; i < string.length; i++) {
2325

2426
const charCode = string.charCodeAt(i);
25-
const symbol = characterTable[charCode].default;
27+
const character = characterTable[charCode];
28+
const symbol = character && character.default || string[i];
2629

27-
symbols.push(symbol || string[i]);
30+
symbols.push(symbol);
2831
}
2932

30-
console.log(symbols.join(" "));
33+
return symbols.join(" ");
3134
};
3235

3336
module.exports = {
34-
print: print
37+
print: print,
38+
convert: convert
3539
};

test.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
const { print: ninprint } = require("./");
1+
const { print: ninprint, convert } = require("./");
22

3-
ninprint("Hello world.");
4-
ninprint("Line feed:\n");
5-
ninprint("Carriage return:\r");
6-
ninprint("Null character:\0");
7-
ninprint(new Buffer([0x00, 0x01, 0x02, 0x03, 0x04, 0x05]));
3+
const assert = (condition, message) => {
4+
5+
if (!condition)
6+
throw new Error(message || "Assertion failed");
7+
};
8+
9+
assert(convert("Hello world.") === "H e l l o [SPACE] w o r l d .");
10+
assert(convert("Line feed:\n") === "L i n e [SPACE] f e e d : [LF]");
11+
assert(convert("Carriage return:\r") === "C a r r i a g e [SPACE] r e t u r n : [CR]");
12+
assert(convert("Null character:\0") === "N u l l [SPACE] c h a r a c t e r : [NUL]");
13+
assert(convert(new Buffer([0x00, 0x01, 0x02, 0x03, 0x04, 0x05])) === "[NUL] [SOH] [STX] [ETX] [EOT] [ENQ]");
14+
15+
console.log("All tests passed");

0 commit comments

Comments
 (0)