-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-to-lcd.js
48 lines (42 loc) · 1.5 KB
/
convert-to-lcd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function convertToLcd(number, scale = {width: 1, height: 1}) {
const decimalDigits = String(number).split('').map(Number);
const lcdDigits = decimalDigits.map(lcdDigit(scale));
return lcdNumber(lcdDigits);
}
const lcdDigit = ({width, height}) => (decimalDigit) => {
const EMPTY = ' ';
const [
[TOP_LEFT, TOP, TOP_RIGHT],
[CENTER_LEFT, CENTER, CENTER_RIGHT],
[BOTTOM_LEFT, BOTTOM, BOTTOM_RIGHT],
] = LCD_DIGITS[decimalDigit];
return [
TOP_LEFT + TOP.repeat(width) + TOP_RIGHT,
...copy(height - 1,
CENTER_LEFT + EMPTY.repeat(width) + CENTER_RIGHT,
),
CENTER_LEFT + CENTER.repeat(width) + CENTER_RIGHT,
...copy(height - 1,
BOTTOM_LEFT + EMPTY.repeat(width) + BOTTOM_RIGHT,
),
BOTTOM_LEFT + BOTTOM.repeat(width) + BOTTOM_RIGHT,
];
};
const LCD_DIGITS = transpose([
[' _ ', ' ', ' _ ', ' _ ', ' ', ' _ ', ' _ ', ' _ ', ' _ ', ' _ '],
['| |', ' |', ' _|', ' _|', '|_|', '|_ ', '|_ ', ' |', '|_|', '|_|'],
['|_|', ' |', '|_ ', ' _|', ' |', ' _|', '|_|', ' |', '|_|', ' _|'],
]);
function lcdNumber(lcdDigits) {
const concatenate = (lines, digit) => lines.map((line, index) => line + digit[index]);
return lcdDigits.reduce(concatenate).join('\n') + '\n';
}
module.exports = {
convertToLcd,
}
function copy(count, element) {
return Array(count).fill(element);
}
function transpose(matrix) {
return matrix[0].map((_, column) => matrix.map(row => row[column]));
}