@keeex/utils 7.2.0 → 7.2.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/lib/promise.js +6 -2
- package/lib/units.js +6 -5
- package/package.json +1 -1
- package/web/promise.js +6 -2
- package/web/units.js +6 -5
package/lib/promise.js
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
*
|
|
15
15
|
*/
|
|
16
16
|
/* eslint-env node */
|
|
17
|
+
import * as log from "@keeex/log";
|
|
17
18
|
import { timeConvert } from "./units.js";
|
|
18
19
|
/**
|
|
19
20
|
* Create a promise that resolves after a given delay.
|
|
@@ -67,6 +68,7 @@ export const firstTruthy = async (promiseFuncs) => {
|
|
|
67
68
|
}
|
|
68
69
|
throw new Error("No result");
|
|
69
70
|
};
|
|
71
|
+
const logger = log.createLogger("dropPromise");
|
|
70
72
|
/**
|
|
71
73
|
* Drop a promise and silence any exception.
|
|
72
74
|
*
|
|
@@ -78,7 +80,9 @@ export const dropPromise = (promise) => {
|
|
|
78
80
|
return;
|
|
79
81
|
}
|
|
80
82
|
if (promise instanceof Promise) {
|
|
81
|
-
// eslint-disable-next-line
|
|
82
|
-
promise.catch(() => {
|
|
83
|
+
// eslint-disable-next-line promise/prefer-await-to-then
|
|
84
|
+
promise.catch((e) => {
|
|
85
|
+
logger.error("Dropped promise rejected:", e);
|
|
86
|
+
});
|
|
83
87
|
}
|
|
84
88
|
};
|
package/lib/units.js
CHANGED
|
@@ -92,8 +92,8 @@ const convertUnit = (unitDescription, fromUnit, toUnit) => {
|
|
|
92
92
|
}
|
|
93
93
|
return { from, to };
|
|
94
94
|
};
|
|
95
|
-
const regexBefore = /^(?<unit>[^\d]+)\d
|
|
96
|
-
const regexAfter =
|
|
95
|
+
const regexBefore = /^(?<unit>[^\d,.]+)(?:(?:\d+)|(?:\d*[.,]\d+)|(?:\d+[.,]\d*))$/u;
|
|
96
|
+
const regexAfter = /^(?:(?:\d+)|(?:\d*[.,]\d+)|(?:\d+[.,]\d*))(?<unit>[^\d,.]+)$/u;
|
|
97
97
|
/** Get the unit text from an input value */
|
|
98
98
|
const getUnitFromValue = (value, unitDesc) => {
|
|
99
99
|
if (typeof value !== "string")
|
|
@@ -105,12 +105,13 @@ const getUnitFromValue = (value, unitDesc) => {
|
|
|
105
105
|
const unit = res.groups.unit;
|
|
106
106
|
return unit.trim();
|
|
107
107
|
};
|
|
108
|
-
const
|
|
108
|
+
const regexFloatValue = /^(?:[^\d,.]*)(?<value>(?:\d+)|(?:\d*[.,]\d+)|(?:\d+[.,]\d*))(?:[^\d,.]*)$/u;
|
|
109
|
+
const regexIntOnlyValue = /^(?:[^\d,.]*)(?<value>\d+)(?:[^\d,.]*)$/u;
|
|
109
110
|
/** Get the numeric value, stripping units */
|
|
110
111
|
const getNumberValue = (value) => {
|
|
111
112
|
if (typeof value === "number")
|
|
112
113
|
return value;
|
|
113
|
-
const res =
|
|
114
|
+
const res = regexFloatValue.exec(value);
|
|
114
115
|
if (!res?.groups)
|
|
115
116
|
throw new Error(`Missing value (${value})`);
|
|
116
117
|
return parseFloat(res.groups.value);
|
|
@@ -121,7 +122,7 @@ const getBigintValue = (value) => {
|
|
|
121
122
|
return value;
|
|
122
123
|
if (typeof value === "number")
|
|
123
124
|
return BigInt(value);
|
|
124
|
-
const res =
|
|
125
|
+
const res = regexIntOnlyValue.exec(value);
|
|
125
126
|
if (!res?.groups)
|
|
126
127
|
throw new Error(`Missing value (${value})`);
|
|
127
128
|
return BigInt(res.groups.value);
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@keeex/utils","version":"7.2.
|
|
1
|
+
{"name":"@keeex/utils","version":"7.2.1","description":"Various utility functions for pure JavaScript","scripts":{},"author":"KeeeX SAS","contributors":[{"name":"Gabriel Paul \"Cley Faye\" Risterucci","email":"gabriel@keeex.net"}],"homepage":"https://keeex.me/oss","keywords":["utility"],"type":"module","license":"MIT","dependencies":{"@keeex/bubble_babble":"^3.0.1","@keeex/log":"^1.7.1","base64-arraybuffer":"^1.0.2","cron-parser":"^5.2.0","ms":"^2.1.3","text-encoding-shim":"^1.0.5"},"exports":{"./units.js":{"node":"./lib/units.js","browser":"./web/units.js","default":"./lib/units.js"},"./uint8array.js":{"node":"./lib/uint8array.js","browser":"./web/uint8array.js","default":"./lib/uint8array.js"},"./string.js":{"node":"./lib/string.js","browser":"./web/string.js","default":"./lib/string.js"},"./starttime.js":{"node":"./lib/starttime.js","browser":"./web/starttime.js","default":"./lib/starttime.js"},"./promise.js":{"node":"./lib/promise.js","browser":"./web/promise.js","default":"./lib/promise.js"},"./path.js":{"node":"./lib/path.js","browser":"./web/path.js","default":"./lib/path.js"},"./number.js":{"node":"./lib/number.js","browser":"./web/number.js","default":"./lib/number.js"},"./linebuffer.js":{"node":"./lib/linebuffer.js","browser":"./web/linebuffer.js","default":"./lib/linebuffer.js"},"./json.js":{"node":"./lib/json.js","browser":"./web/json.js","default":"./lib/json.js"},"./idx.js":{"node":"./lib/idx.js","browser":"./web/idx.js","default":"./lib/idx.js"},"./hex.js":{"node":"./lib/hex.js","browser":"./web/hex.js","default":"./lib/hex.js"},"./global.js":{"node":"./lib/global.js","browser":"./web/global.js","default":"./lib/global.js"},"./error.js":{"node":"./lib/error.js","browser":"./web/error.js","default":"./lib/error.js"},"./dict.js":{"node":"./lib/dict.js","browser":"./web/dict.js","default":"./lib/dict.js"},"./cron.js":{"node":"./lib/cron.js","browser":"./web/cron.js","default":"./lib/cron.js"},"./consts.js":{"node":"./lib/consts.js","browser":"./web/consts.js","default":"./lib/consts.js"},"./bytebuffer.js":{"node":"./lib/bytebuffer.js","browser":"./web/bytebuffer.js","default":"./lib/bytebuffer.js"},"./benchmark.js":{"node":"./lib/benchmark.js","browser":"./web/benchmark.js","default":"./lib/benchmark.js"},"./base64.js":{"node":"./lib/base64.js","browser":"./web/base64.js","default":"./lib/base64.js"},"./base58.js":{"node":"./lib/base58.js","browser":"./web/base58.js","default":"./lib/base58.js"},"./arraybuffer.js":{"node":"./lib/arraybuffer.js","browser":"./web/arraybuffer.js","default":"./lib/arraybuffer.js"},"./array.js":{"node":"./lib/array.js","browser":"./web/array.js","default":"./lib/array.js"},"./types/utils.js":{"node":"./lib/types/utils.js","browser":"./web/types/utils.js","default":"./lib/types/utils.js"},"./types/types.js":{"node":"./lib/types/types.js","browser":"./web/types/types.js","default":"./lib/types/types.js"},"./types/record.js":{"node":"./lib/types/record.js","browser":"./web/types/record.js","default":"./lib/types/record.js"},"./types/primitive.js":{"node":"./lib/types/primitive.js","browser":"./web/types/primitive.js","default":"./lib/types/primitive.js"},"./types/predicateerror.js":{"node":"./lib/types/predicateerror.js","browser":"./web/types/predicateerror.js","default":"./lib/types/predicateerror.js"},"./types/enum.js":{"node":"./lib/types/enum.js","browser":"./web/types/enum.js","default":"./lib/types/enum.js"},"./types/array.js":{"node":"./lib/types/array.js","browser":"./web/types/array.js","default":"./lib/types/array.js"},"./marshalling/unmarshaller.js":{"node":"./lib/marshalling/unmarshaller.js","browser":"./web/marshalling/unmarshaller.js","default":"./lib/marshalling/unmarshaller.js"},"./marshalling/marshaller.js":{"node":"./lib/marshalling/marshaller.js","browser":"./web/marshalling/marshaller.js","default":"./lib/marshalling/marshaller.js"},"./async/timecache.js":{"node":"./lib/async/timecache.js","browser":"./web/async/timecache.js","default":"./lib/async/timecache.js"},"./async/queues.js":{"node":"./lib/async/queues.js","browser":"./web/async/queues.js","default":"./lib/async/queues.js"},"./async/keycache.js":{"node":"./lib/async/keycache.js","browser":"./web/async/keycache.js","default":"./lib/async/keycache.js"},"./async/deferredpromise.js":{"node":"./lib/async/deferredpromise.js","browser":"./web/async/deferredpromise.js","default":"./lib/async/deferredpromise.js"},"./async/asynctrigger.js":{"node":"./lib/async/asynctrigger.js","browser":"./web/async/asynctrigger.js","default":"./lib/async/asynctrigger.js"}},"files":["/lib","/web"]}
|
package/web/promise.js
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
*
|
|
15
15
|
*/
|
|
16
16
|
/* eslint-env node */
|
|
17
|
+
import * as log from "@keeex/log";
|
|
17
18
|
import { timeConvert } from "./units.js";
|
|
18
19
|
/**
|
|
19
20
|
* Create a promise that resolves after a given delay.
|
|
@@ -63,6 +64,7 @@ export const firstTruthy = async promiseFuncs => {
|
|
|
63
64
|
}
|
|
64
65
|
throw new Error("No result");
|
|
65
66
|
};
|
|
67
|
+
const logger = log.createLogger("dropPromise");
|
|
66
68
|
/**
|
|
67
69
|
* Drop a promise and silence any exception.
|
|
68
70
|
*
|
|
@@ -74,7 +76,9 @@ export const dropPromise = promise => {
|
|
|
74
76
|
return;
|
|
75
77
|
}
|
|
76
78
|
if (promise instanceof Promise) {
|
|
77
|
-
// eslint-disable-next-line
|
|
78
|
-
promise.catch(
|
|
79
|
+
// eslint-disable-next-line promise/prefer-await-to-then
|
|
80
|
+
promise.catch(e => {
|
|
81
|
+
logger.error("Dropped promise rejected:", e);
|
|
82
|
+
});
|
|
79
83
|
}
|
|
80
84
|
};
|
package/web/units.js
CHANGED
|
@@ -125,8 +125,8 @@ const convertUnit = (unitDescription, fromUnit, toUnit) => {
|
|
|
125
125
|
to
|
|
126
126
|
};
|
|
127
127
|
};
|
|
128
|
-
const regexBefore = /^(?<unit>[^\d]+)\d
|
|
129
|
-
const regexAfter =
|
|
128
|
+
const regexBefore = /^(?<unit>[^\d,.]+)(?:(?:\d+)|(?:\d*[.,]\d+)|(?:\d+[.,]\d*))$/u;
|
|
129
|
+
const regexAfter = /^(?:(?:\d+)|(?:\d*[.,]\d+)|(?:\d+[.,]\d*))(?<unit>[^\d,.]+)$/u;
|
|
130
130
|
/** Get the unit text from an input value */
|
|
131
131
|
const getUnitFromValue = (value, unitDesc) => {
|
|
132
132
|
if (typeof value !== "string") return unitDesc.basename;
|
|
@@ -136,11 +136,12 @@ const getUnitFromValue = (value, unitDesc) => {
|
|
|
136
136
|
const unit = res.groups.unit;
|
|
137
137
|
return unit.trim();
|
|
138
138
|
};
|
|
139
|
-
const
|
|
139
|
+
const regexFloatValue = /^(?:[^\d,.]*)(?<value>(?:\d+)|(?:\d*[.,]\d+)|(?:\d+[.,]\d*))(?:[^\d,.]*)$/u;
|
|
140
|
+
const regexIntOnlyValue = /^(?:[^\d,.]*)(?<value>\d+)(?:[^\d,.]*)$/u;
|
|
140
141
|
/** Get the numeric value, stripping units */
|
|
141
142
|
const getNumberValue = value => {
|
|
142
143
|
if (typeof value === "number") return value;
|
|
143
|
-
const res =
|
|
144
|
+
const res = regexFloatValue.exec(value);
|
|
144
145
|
if (!res?.groups) throw new Error(`Missing value (${value})`);
|
|
145
146
|
return parseFloat(res.groups.value);
|
|
146
147
|
};
|
|
@@ -148,7 +149,7 @@ const getNumberValue = value => {
|
|
|
148
149
|
const getBigintValue = value => {
|
|
149
150
|
if (typeof value === "bigint") return value;
|
|
150
151
|
if (typeof value === "number") return BigInt(value);
|
|
151
|
-
const res =
|
|
152
|
+
const res = regexIntOnlyValue.exec(value);
|
|
152
153
|
if (!res?.groups) throw new Error(`Missing value (${value})`);
|
|
153
154
|
return BigInt(res.groups.value);
|
|
154
155
|
};
|