@icgio/icg-utils 1.9.0 → 1.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  module.exports = {
2
+ circular_buffer: require('./lib/circular-buffer.js'),
2
3
  coinmarketcap: require('./lib/crypto-rates.js'),
3
4
  coingecko: require('./lib/crypto-rates.js'),
4
5
  forex_rate: require('./lib/forex-rate.js'),
@@ -0,0 +1,42 @@
1
+ class CircularBuffer {
2
+ constructor(size) {
3
+ this.size = size
4
+ this.buffer = new Array(size)
5
+ this.index = 0
6
+ this.count = 0
7
+ }
8
+
9
+ push(value) {
10
+ this.buffer[this.index] = value
11
+ this.index = (this.index + 1) % this.size
12
+ if (this.count < this.size) {
13
+ this.count++
14
+ }
15
+ }
16
+
17
+ get_values() {
18
+ return this.buffer.slice(0, this.count)
19
+ }
20
+
21
+ min() {
22
+ return Math.min(...this.getValues())
23
+ }
24
+
25
+ max() {
26
+ return Math.max(...this.getValues())
27
+ }
28
+
29
+ mean() {
30
+ const values = this.getValues()
31
+ return values.reduce((sum, val) => sum + val, 0) / values.length
32
+ }
33
+
34
+ std() {
35
+ const values = this.getValues()
36
+ const mean = this.mean()
37
+ const variance = values.reduce((sum, val) => sum + Math.pow(val - mean, 2), 0) / values.length
38
+ return Math.sqrt(variance)
39
+ }
40
+ }
41
+
42
+ module.exports = CircularBuffer
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.9.0",
3
+ "version": "1.9.1",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {