@darkpos/utils 1.0.11 → 1.0.13

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/lib/helpers.js CHANGED
@@ -84,6 +84,8 @@ const checkKey = (json, key) => {
84
84
  return true;
85
85
  };
86
86
 
87
+ const toNumberOrZero = value => (!value ? 0 : Number(value) || 0);
88
+
87
89
  //
88
90
  module.exports = {
89
91
  hasProperty,
@@ -101,4 +103,5 @@ module.exports = {
101
103
  mergeArrays,
102
104
  isArrayEqual,
103
105
  clearEmptyValues,
106
+ toNumberOrZero,
104
107
  };
@@ -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
@@ -93,6 +93,8 @@ const min = (...args) => Decimal.min(...args).toNumber();
93
93
  const toNearestMultiple = (x, nearestMultiple) =>
94
94
  Decimal(x).toNearest(nearestMultiple, Decimal.ROUND_UP).toNumber();
95
95
 
96
+ const floor = x => Decimal.floor(x);
97
+
96
98
  module.exports = {
97
99
  mul,
98
100
  div,
@@ -110,4 +112,5 @@ module.exports = {
110
112
  max,
111
113
  min,
112
114
  toNearestMultiple,
115
+ floor,
113
116
  };
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.11",
3
+ "version": "1.0.13",
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": "fc81201b39f7ea2880fd368a8e541e4fa0750459"
42
+ "gitHead": "94c96f8e898bff82463d49bfb39f27ef0e70909e"
43
43
  }