Skip to content

Commit e2657b7

Browse files
Move util functions out of app.js
1 parent b773678 commit e2657b7

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

app.js

+2-29
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import fs from 'fs';
2-
import utf from 'utf-8';
2+
import dotenv from 'dotenv';
33
import Snoowrap from 'snoowrap';
44
import snoostorm from 'snoostorm';
5-
import dotenv from 'dotenv';
5+
import decode from './util/decode.mjs';
66

77
// Awaiting top level await 😭
88
let ignoredUsers;
@@ -33,30 +33,3 @@ comments.on('item', (comment) => {
3333
comment.reply(`That translates to: "${translated}". ${botNotice}`);
3434
}
3535
});
36-
37-
function decode(string) {
38-
const delimited = /^(?:[01]{8} ){3,}$/gm;
39-
const nonDelimited = /^(?:[01]{8}){3,}$/gm;
40-
const byteRegex = /[01]{8}/gm;
41-
42-
string = string.trim();
43-
44-
if (delimited.test(`${string} `) || nonDelimited.test(string)) {
45-
const bytes = string.replace(/ /g, '').match(byteRegex);
46-
return decodeBytes(bytes);
47-
}
48-
49-
return '';
50-
}
51-
52-
function decodeBytes(bytes) {
53-
let decoded;
54-
55-
try {
56-
decoded = utf.getStringFromBytes(bytes.map(byte => parseInt(byte, 2)));
57-
} catch (error) {
58-
decoded = '';
59-
}
60-
61-
return decoded;
62-
}

util/decode-bytes.mjs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import utf from 'utf-8';
2+
3+
function decodeBytes(bytes) {
4+
let decoded;
5+
6+
try {
7+
decoded = utf.getStringFromBytes(bytes.map(byte => parseInt(byte, 2)));
8+
} catch (error) {
9+
decoded = '';
10+
}
11+
12+
return decoded;
13+
}
14+
15+
export default decodeBytes;

util/decode.mjs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import decodeBytes from './decode-bytes.mjs';
2+
3+
function decode(string) {
4+
const delimited = /^(?:[01]{8} ){3,}$/gm;
5+
const nonDelimited = /^(?:[01]{8}){3,}$/gm;
6+
const byteRegex = /[01]{8}/gm;
7+
8+
string = string.trim();
9+
10+
if (delimited.test(`${string} `) || nonDelimited.test(string)) {
11+
const bytes = string.replace(/ /g, '').match(byteRegex);
12+
return decodeBytes(bytes);
13+
}
14+
15+
return '';
16+
}
17+
18+
export default decode;

0 commit comments

Comments
 (0)