-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
18 lines (17 loc) · 752 Bytes
/
index.js
File metadata and controls
18 lines (17 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const any = (items = [], encoding = 'hex', operation) => {
if (items.length <= 1) return items.shift();
const a = Buffer.from(items.shift(), encoding);
const b = Buffer.from(items.shift(), encoding);
let c;
if (operation === 'or') c = a.map((el, idx) => el | b[idx]);
if (operation === 'xor') c = a.map((el, idx) => el ^ b[idx]);
if (operation === 'and') c = a.map((el, idx) => el & b[idx]);
items.unshift(Buffer.from(c).toString(encoding).toUpperCase());
return bits[operation](items, encoding);
};
const bits = {
or: (items, encoding) => any(items, encoding, 'or'),
xor: (items, encoding) => any(items, encoding, 'xor'),
and: (items, encoding) => any(items, encoding, 'and')
};
module.exports = bits;