-
Notifications
You must be signed in to change notification settings - Fork 24
/
C_catsindent.js
66 lines (55 loc) · 2.04 KB
/
C_catsindent.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const entry1 = {
"id": 185,
"name": "Murzick",
"birthday": 1164210686
}
const entry2 = {
"id": 96,
"name": "Ferdinand",
"birthday": 1429648740
}
const div = document.getElementById('barcode');
function renderBarcode(catInfo, element) {
const separator = '<div style="width: 1px; border: 3px solid black"></div>';
const squereBlack = '<div style="width: 8px; height: 8px; background-color: black"></div>';
const squereWhite = '<div style="width: 8px; height: 8px; background-color: white"></div>';
const content = [];
const summContent = [];
for (let nameLength = catInfo.name.length; nameLength < 11; nameLength ++) {
catInfo.name += ' ';
}
const asciName = catInfo.name.split('').map((symb) => symb.charCodeAt(0).toString(2));
const codeName = toEightSymb(asciName, 8);
const codeId = toEightSymb([catInfo.id.toString(2)], 8);
const codeBirthD = toEightSymb([catInfo.birthday.toString(2)], 32);
const code = codeName.join('') + codeId.join() + codeBirthD;
for (let i = 0, l = code.length; i < l; i++) {
if (+code[i] === 0) {
content.push(squereWhite);
} else if (+code[i] === 1) {
content.push(squereBlack);
}
}
for (let i = 0, l = code.length, sum = 0; i <= l; i++) {
if (i%16 === 0 && i !== 0) {
if (sum%2 === 0) {
summContent.push(squereWhite);
} else {
summContent.push(squereBlack);
}
sum = +code[i];
} else {
sum += +code[i];
}
}
function toEightSymb(string, base) {
return string.map((item) => {
for (let itemLength = item.length; itemLength < base; itemLength++) {
item = '0' + item;
}
return item;
});
}
element.innerHTML = '<div style="display: flex; width: 100%; height: 100%; align-items: stretch">' + separator + '<div style="display: flex; flex-wrap: wrap; width: 128px; height: 100%">' + content.join('') + '</div>' + separator + '<div style="display: flex; flex-direction: column">' + summContent.join('') + '</div>' + separator + '</div>';
}
renderBarcode(entry1, div);