@darkpos/utils 1.0.10 → 1.0.12

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.
@@ -172,7 +172,7 @@ const formatAmountWithCurrency = (amount, currency, locale) =>
172
172
  ? new Intl.NumberFormat(locale, {
173
173
  style: 'currency',
174
174
  currency,
175
- signDisplay: 'negative'
175
+ signDisplay: 'negative',
176
176
  }).format(Number(amount))
177
177
  : '';
178
178
 
package/lib/math.js CHANGED
@@ -90,6 +90,9 @@ const max = (...args) => Decimal.max(...args).toNumber();
90
90
 
91
91
  const min = (...args) => Decimal.min(...args).toNumber();
92
92
 
93
+ const toNearestMultiple = (x, nearestMultiple) =>
94
+ Decimal(x).toNearest(nearestMultiple, Decimal.ROUND_UP).toNumber();
95
+
93
96
  module.exports = {
94
97
  mul,
95
98
  div,
@@ -106,4 +109,5 @@ module.exports = {
106
109
  toDecimalPlaces,
107
110
  max,
108
111
  min,
112
+ toNearestMultiple,
109
113
  };
package/lib/system.js CHANGED
@@ -1,11 +1,9 @@
1
- const os = require('os');
2
-
3
1
  const interval = process.env.DRK_LOGGER_INTERVAL || 60 * 60;
4
- const freeMemory = () => Math.round(os.freemem() / (1024 * 1024));
5
- const totalMemory = () => Math.round(os.totalmem() / (1024 * 1024));
2
+ const freeMemory = os => Math.round(os.freemem() / (1024 * 1024));
3
+ const totalMemory = os => Math.round(os.totalmem() / (1024 * 1024));
6
4
  const cache = {};
7
5
 
8
- const cpuInfo = () => {
6
+ const cpuInfo = os => {
9
7
  const cpus = os.cpus();
10
8
  let user = 0;
11
9
  let nice = 0;
@@ -30,8 +28,8 @@ const cpuInfo = () => {
30
28
  };
31
29
  };
32
30
 
33
- const cpuFree = () => {
34
- const currCPUs = cpuInfo();
31
+ const cpuFree = os => {
32
+ const currCPUs = cpuInfo(os);
35
33
  const idle = (currCPUs.idle - cache.preCPUs.idle) / interval;
36
34
  const total = (currCPUs.total - cache.preCPUs.total) / interval;
37
35
  const free = Math.round((idle / total) * 100);
@@ -41,18 +39,37 @@ const cpuFree = () => {
41
39
 
42
40
  const getSystemInfo = () => cache.systemInfo;
43
41
 
44
- const systemInfo = () => ({
45
- hostname: os.hostname(),
46
- uptime: os.uptime(),
47
- os_arch: os.arch(),
48
- os_platform: os.platform(),
49
- os_release: os.release(),
50
- memory_total: totalMemory(),
51
- memory_free: freeMemory(),
52
- cpu_count: os.cpus().length,
53
- cpu_free: cpuFree(),
54
- node: process.versions && process.versions.node,
55
- });
42
+ const systemInfo = () => {
43
+ try {
44
+ const os = require('os');
45
+ return {
46
+ hostname: os.hostname(),
47
+ uptime: os.uptime(),
48
+ os_arch: os.arch(),
49
+ os_platform: os.platform(),
50
+ os_release: os.release(),
51
+ memory_total: totalMemory(os),
52
+ memory_free: freeMemory(os),
53
+ cpu_count: os.cpus().length,
54
+ cpu_free: cpuFree(os),
55
+ node: process.versions && process.versions.node,
56
+ };
57
+ } catch (error) {
58
+ // Return a fallback object if 'os' module is not available (Expo environment)
59
+ return {
60
+ hostname: 'unknown',
61
+ uptime: 0,
62
+ os_arch: 'unknown',
63
+ os_platform: 'unknown',
64
+ os_release: 'unknown',
65
+ memory_total: 0,
66
+ memory_free: 0,
67
+ cpu_count: 0,
68
+ cpu_free: 0,
69
+ node: process.versions && process.versions.node,
70
+ };
71
+ }
72
+ };
56
73
 
57
74
  const invokeSystemInfo = fn => {
58
75
  if (fn) {
@@ -64,7 +81,15 @@ const invokeSystemInfo = fn => {
64
81
  }, interval * 1000);
65
82
  };
66
83
 
67
- cache.preCPUs = cpuInfo();
68
- cache.systemInfo = systemInfo();
84
+ // Initialize cache safely with try/catch to handle environments without 'os'
85
+ try {
86
+ const os = require('os');
87
+ cache.preCPUs = cpuInfo(os);
88
+ cache.systemInfo = systemInfo();
89
+ } catch (error) {
90
+ // Set default values if 'os' is not available
91
+ cache.preCPUs = { idle: 0, total: 0 };
92
+ cache.systemInfo = systemInfo();
93
+ }
69
94
 
70
95
  module.exports = { getSystemInfo, invokeSystemInfo };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/utils",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "General purpose utilities",
5
5
  "author": "Dark POS",
6
6
  "homepage": "https://gitlab.com/darkpos/packages/dark#readme",
@@ -39,5 +39,5 @@
39
39
  "eslint-plugin-prettier": "^4.0.0",
40
40
  "prettier": "^2.7.0"
41
41
  },
42
- "gitHead": "e34041588c206612b5c2dff2e27aa34ac838137e"
42
+ "gitHead": "4e966b805abc74cc31cb0b3a0754e71f9bb913e5"
43
43
  }