/
clib.js
162 lines (129 loc) · 4.89 KB
/
clib.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
define([], function () {
var audio = null;
return {
/** General stuff **/
isInteger: function (nVal) {
return typeof nVal === "number" && isFinite(nVal) && nVal > -9007199254740992 && nVal < 9007199254740992 && Math.floor(nVal) === nVal;
},
isNumber: function (nVal) {
return typeof nVal === "number" && isFinite(nVal) && nVal > -9007199254740992 && nVal < 9007199254740992;
},
/** Satoshis/Bits handlers **/
satToBit: function (satoshis) {
return satoshis / 100;
},
satToBitFloored: function (satoshis) {
return Math.floor(this.satToBit(satoshis));
},
bitToSat: function (bits) {
return bits * 100;
},
floorHundreds: function (number) {
return Math.floor(number / 100) * 100;
},
//Validate the input text or number of bits
//Should be integer, bigger than 0 and bigger than the balance
validateBitsInput: function(bet, balance) {
var validity = 'valid', message = '';
bet = Number(bet);
if(!this.isInteger(bet)) {
validity = 'wrong';
'Should be an integer';
} else if(bet < 1) {
validity = 'wrong';
message = 'Should be bigger than zero';
} else if(bet > this.satToBit(balance)) {
validity = 'warning';
message = 'Not enough balance :o';
}
return [validity, message];
},
formatSatoshis: function (n, decimals) {
return this.formatDecimals(Math.floor(n) / 100, decimals);
},
formatDecimals: function (n, decimals) {
if (typeof decimals === 'undefined') {
if (n % 1 === 0)
decimals = 0;
else
decimals = 2;
}
return n.toFixed(decimals).toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
},
bitsTextTerm: function (bits) {
return (bits < 1.005 && bits >= 0.995) ? 'bit' : 'bits';
},
browserSupport: function () {
if (typeof Storage === 'undefined')
return false;
return !!XMLHttpRequest;
},
log: function () {
for (var i = 0, length = arguments.length; i < length; i++) {
arguments[i] = JSON.parse(JSON.stringify(arguments[i]));
}
console.log.apply(console, arguments);
},
randomUint32: function () {
if (window && window.crypto && window.crypto.getRandomValues && Uint32Array) {
var o = new Uint32Array(1);
window.crypto.getRandomValues(o);
return o[0];
} else {
console.warn('Falling back to pseudo-random client seed');
return Math.floor(Math.random() * Math.pow(2, 32));
}
},
//Execute an array of functions and return an array with they results in the same order
parallel: function(funcs, complete) {
var completed = 0;
var length = funcs.length;
var results = new Array(length);
var bail = false;
funcs.forEach(function(func, i) {
func(function (err, result) {
if (bail) return;
if (err) {
bail = true;
return complete(err);
}
results[i] = result;
++completed;
console.assert(completed <= length, 'One of the parallel functions called the callback multiple times');
if (completed === length)
return complete(null, results);
})
});
},
getHashParams: function () {
var hashParams = {};
var e,
a = /\+/g, // Regex for replacing addition symbol with a space
r = /([^&;=]+)=?([^&;]*)/g,
d = function (s) {
return decodeURIComponent(s.replace(a, " "));
},
q = window.location.hash.substring(1);
while (e = r.exec(q))
hashParams[d(e[1])] = d(e[2]);
return hashParams;
},
localOrDef: function(name, def) {
if(localStorage[name])
return JSON.parse(localStorage[name]);
else
return def;
},
newMentionRegExp: function(username) {
return new RegExp('@' + username + '(?:$|[^a-z0-9_\-])', 'ig');
},
beep: function() { //Load when is required to avoid unnecessary load
if(!audio) {
audio = new Audio('/sounds/alert.wav');
audio.play();
} else {
audio.play();
}
}
}
});