This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathUtils.js
64 lines (55 loc) · 1.37 KB
/
Utils.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
var util = require("util");
var Utils = function() {
this.testCallsign = function(callsign) {
if(typeof callsign == "undefined" || callsign.length > 6)
return false;
callsign = callsign.toUpperCase().replace(/\s*$/g, "");
for(var c = 0; c < callsign.length; c++) {
var a = callsign[c].charCodeAt(0);
if( (a >= 48 && a <= 57)
||
(a >=65 && a <=90)
) {
continue;
}
return false;
}
return true;
}
this.logByte = function(b) {
console.log(
util.format(
"%d%d%d%d%d%d%d%d",
(b & (1<<7)) ? 1 : 0,
(b & (1<<6)) ? 1 : 0,
(b & (1<<5)) ? 1 : 0,
(b & (1<<4)) ? 1 : 0,
(b & (1<<3)) ? 1 : 0,
(b & (1<<2)) ? 1 : 0,
(b & (1<<1)) ? 1 : 0,
(b & (1<<0)) ? 1 : 0
)
);
}
/* distanceBetween(leader, follower, modulus)
Find the difference between 'leader' and 'follower' modulo 'modulus'. */
this.distanceBetween = function(l, f, m) {
return (l < f) ? (l + (m - f)) : (l - f);
}
// Turns a string into an array of character codes
this.stringToByteArray = function(s) {
s = s.split("");
var r = new Array();
for(var i = 0; i < s.length; i++)
r.push(s[i].charCodeAt(0));
return r;
}
// Turns an array of ASCII character codes into a string
this.byteArrayToString = function(s) {
var r = "";
for(var i = 0; i < s.length; i++)
r += String.fromCharCode(s[i]);
return r;
}
}
module.exports = new Utils;