@icgio/icg-utils 1.9.1 → 1.9.2

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,5 +1,4 @@
1
1
  module.exports = {
2
- circular_buffer: require('./lib/circular-buffer.js'),
3
2
  coinmarketcap: require('./lib/crypto-rates.js'),
4
3
  coingecko: require('./lib/crypto-rates.js'),
5
4
  forex_rate: require('./lib/forex-rate.js'),
@@ -1,22 +1,29 @@
1
1
  class RateLimitedLogger {
2
2
  constructor(defaultLimit = 1000) {
3
- this.defaultLimit = defaultLimit // Shared default limit for log and error
4
- this.lastLogTime = 0 // Tracks the last log or error time
3
+ this.defaultLimit = defaultLimit
4
+ this.lastLogTime = 0
5
+ this.lastErrorTime = 0
5
6
  }
6
7
 
7
8
  log(message, limit = this.defaultLimit) {
8
- this._rateLimitedOutput(console.log, message, limit)
9
+ this._rateLimitedOutput(console.log, message, limit, 'log')
9
10
  }
10
11
 
11
12
  error(message, limit = this.defaultLimit) {
12
- this._rateLimitedOutput(console.error, message, limit)
13
+ this._rateLimitedOutput(console.error, message, limit, 'error')
13
14
  }
14
15
 
15
- _rateLimitedOutput(outputFunction, message, limit) {
16
+ _rateLimitedOutput(outputFunction, message, limit, type) {
16
17
  const now = Date.now()
17
- if (now - this.lastLogTime >= limit) {
18
+ const lastTime = type === 'log' ? this.lastLogTime : this.lastErrorTime
19
+
20
+ if (now - lastTime >= limit) {
18
21
  outputFunction(message)
19
- this.lastLogTime = now
22
+ if (type === 'log') {
23
+ this.lastLogTime = now
24
+ } else {
25
+ this.lastErrorTime = now
26
+ }
20
27
  }
21
28
  }
22
29
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.9.1",
3
+ "version": "1.9.2",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -1,42 +0,0 @@
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