@keeex/utils 7.0.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/LICENSE +9 -0
- package/README.md +105 -0
- package/lib/array.d.ts +28 -0
- package/lib/array.js +36 -0
- package/lib/arraybuffer.d.ts +138 -0
- package/lib/arraybuffer.js +141 -0
- package/lib/async/asynctrigger.d.ts +50 -0
- package/lib/async/asynctrigger.js +108 -0
- package/lib/async/deferredpromise.d.ts +32 -0
- package/lib/async/deferredpromise.js +66 -0
- package/lib/async/keycache.d.ts +56 -0
- package/lib/async/keycache.js +103 -0
- package/lib/async/queues.d.ts +69 -0
- package/lib/async/queues.js +135 -0
- package/lib/async/timecache.d.ts +58 -0
- package/lib/async/timecache.js +118 -0
- package/lib/base58.d.ts +27 -0
- package/lib/base58.js +83 -0
- package/lib/base64.d.ts +51 -0
- package/lib/base64.js +126 -0
- package/lib/benchmark.d.ts +126 -0
- package/lib/benchmark.js +177 -0
- package/lib/bits/arraybuffer.d.ts +35 -0
- package/lib/bits/arraybuffer.js +64 -0
- package/lib/bits/base64.d.ts +35 -0
- package/lib/bits/base64.js +70 -0
- package/lib/bits/hex.d.ts +17 -0
- package/lib/bits/hex.js +30 -0
- package/lib/bits/uint8array.d.ts +28 -0
- package/lib/bits/uint8array.js +42 -0
- package/lib/bytebuffer.d.ts +27 -0
- package/lib/bytebuffer.js +29 -0
- package/lib/consts.d.ts +33 -0
- package/lib/consts.js +33 -0
- package/lib/cron/logger.d.ts +22 -0
- package/lib/cron/logger.js +31 -0
- package/lib/cron/scheduledtask.d.ts +71 -0
- package/lib/cron/scheduledtask.js +137 -0
- package/lib/cron/types.d.ts +53 -0
- package/lib/cron/types.js +31 -0
- package/lib/cron.d.ts +29 -0
- package/lib/cron.js +47 -0
- package/lib/dict.d.ts +56 -0
- package/lib/dict.js +74 -0
- package/lib/error.d.ts +25 -0
- package/lib/error.js +41 -0
- package/lib/global.d.ts +27 -0
- package/lib/global.js +53 -0
- package/lib/hex.d.ts +32 -0
- package/lib/hex.js +58 -0
- package/lib/idx.d.ts +51 -0
- package/lib/idx.js +81 -0
- package/lib/json.d.ts +57 -0
- package/lib/json.js +116 -0
- package/lib/marshalling/marshaller.d.ts +51 -0
- package/lib/marshalling/marshaller.js +155 -0
- package/lib/marshalling/unmarshaller.d.ts +53 -0
- package/lib/marshalling/unmarshaller.js +124 -0
- package/lib/marshalling/util.d.ts +25 -0
- package/lib/marshalling/util.js +25 -0
- package/lib/number.d.ts +17 -0
- package/lib/number.js +21 -0
- package/lib/path.d.ts +25 -0
- package/lib/path.js +29 -0
- package/lib/promise.d.ts +42 -0
- package/lib/promise.js +78 -0
- package/lib/starttime.d.ts +23 -0
- package/lib/starttime.js +29 -0
- package/lib/string.d.ts +65 -0
- package/lib/string.js +108 -0
- package/lib/types/array.d.ts +34 -0
- package/lib/types/array.js +64 -0
- package/lib/types/enum.d.ts +30 -0
- package/lib/types/enum.js +44 -0
- package/lib/types/predicateerror.d.ts +40 -0
- package/lib/types/predicateerror.js +107 -0
- package/lib/types/primitive.d.ts +23 -0
- package/lib/types/primitive.js +34 -0
- package/lib/types/record.d.ts +67 -0
- package/lib/types/record.js +235 -0
- package/lib/types/types.d.ts +64 -0
- package/lib/types/types.js +115 -0
- package/lib/types/utils.d.ts +18 -0
- package/lib/types/utils.js +67 -0
- package/lib/uint8array.d.ts +176 -0
- package/lib/uint8array.js +438 -0
- package/lib/units.d.ts +159 -0
- package/lib/units.js +290 -0
- package/lib/utils/buffer.d.ts +49 -0
- package/lib/utils/buffer.js +79 -0
- package/lib/utils/fourbytes.d.ts +29 -0
- package/lib/utils/fourbytes.js +45 -0
- package/package.json +1 -0
- package/web/array.d.ts +28 -0
- package/web/array.js +34 -0
- package/web/arraybuffer.d.ts +138 -0
- package/web/arraybuffer.js +141 -0
- package/web/async/asynctrigger.d.ts +50 -0
- package/web/async/asynctrigger.js +106 -0
- package/web/async/deferredpromise.d.ts +32 -0
- package/web/async/deferredpromise.js +65 -0
- package/web/async/keycache.d.ts +56 -0
- package/web/async/keycache.js +97 -0
- package/web/async/queues.d.ts +69 -0
- package/web/async/queues.js +131 -0
- package/web/async/timecache.d.ts +58 -0
- package/web/async/timecache.js +107 -0
- package/web/base58.d.ts +27 -0
- package/web/base58.js +78 -0
- package/web/base64.d.ts +51 -0
- package/web/base64.js +136 -0
- package/web/benchmark.d.ts +126 -0
- package/web/benchmark.js +183 -0
- package/web/bits/arraybuffer.d.ts +35 -0
- package/web/bits/arraybuffer.js +59 -0
- package/web/bits/base64.d.ts +35 -0
- package/web/bits/base64.js +67 -0
- package/web/bits/hex.d.ts +17 -0
- package/web/bits/hex.js +27 -0
- package/web/bits/uint8array.d.ts +28 -0
- package/web/bits/uint8array.js +41 -0
- package/web/bytebuffer.d.ts +27 -0
- package/web/bytebuffer.js +29 -0
- package/web/consts.d.ts +33 -0
- package/web/consts.js +33 -0
- package/web/cron/logger.d.ts +22 -0
- package/web/cron/logger.js +30 -0
- package/web/cron/scheduledtask.d.ts +71 -0
- package/web/cron/scheduledtask.js +136 -0
- package/web/cron/types.d.ts +53 -0
- package/web/cron/types.js +31 -0
- package/web/cron.d.ts +29 -0
- package/web/cron.js +47 -0
- package/web/dict.d.ts +56 -0
- package/web/dict.js +67 -0
- package/web/error.d.ts +25 -0
- package/web/error.js +39 -0
- package/web/global.d.ts +27 -0
- package/web/global.js +49 -0
- package/web/hex.d.ts +32 -0
- package/web/hex.js +52 -0
- package/web/idx.d.ts +51 -0
- package/web/idx.js +76 -0
- package/web/json.d.ts +57 -0
- package/web/json.js +98 -0
- package/web/marshalling/marshaller.d.ts +51 -0
- package/web/marshalling/marshaller.js +150 -0
- package/web/marshalling/unmarshaller.d.ts +53 -0
- package/web/marshalling/unmarshaller.js +115 -0
- package/web/marshalling/util.d.ts +25 -0
- package/web/marshalling/util.js +25 -0
- package/web/number.d.ts +17 -0
- package/web/number.js +21 -0
- package/web/path.d.ts +25 -0
- package/web/path.js +26 -0
- package/web/promise.d.ts +42 -0
- package/web/promise.js +74 -0
- package/web/starttime.d.ts +23 -0
- package/web/starttime.js +29 -0
- package/web/string.d.ts +65 -0
- package/web/string.js +101 -0
- package/web/types/array.d.ts +34 -0
- package/web/types/array.js +63 -0
- package/web/types/enum.d.ts +30 -0
- package/web/types/enum.js +40 -0
- package/web/types/predicateerror.d.ts +40 -0
- package/web/types/predicateerror.js +128 -0
- package/web/types/primitive.d.ts +23 -0
- package/web/types/primitive.js +33 -0
- package/web/types/record.d.ts +67 -0
- package/web/types/record.js +213 -0
- package/web/types/types.d.ts +64 -0
- package/web/types/types.js +123 -0
- package/web/types/utils.d.ts +18 -0
- package/web/types/utils.js +30 -0
- package/web/uint8array.d.ts +176 -0
- package/web/uint8array.js +412 -0
- package/web/units.d.ts +159 -0
- package/web/units.js +312 -0
- package/web/utils/buffer.d.ts +49 -0
- package/web/utils/buffer.js +76 -0
- package/web/utils/fourbytes.d.ts +29 -0
- package/web/utils/fourbytes.js +45 -0
package/web/units.js
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
import ms from "ms";
|
|
17
|
+
import { roundDecimals } from "./number.js";
|
|
18
|
+
/** Coerce a delay value to milliseconds using `ms` if a string is provided */
|
|
19
|
+
export const delayInMs = delayInMsValue => {
|
|
20
|
+
if (typeof delayInMsValue === "number") return delayInMsValue;
|
|
21
|
+
return ms(delayInMsValue);
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Convert description for byte based values
|
|
25
|
+
*
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export const byteUnit = {
|
|
29
|
+
basename: "B",
|
|
30
|
+
levels: [{
|
|
31
|
+
multiplierFromPrevious: 1
|
|
32
|
+
},
|
|
33
|
+
// Base level
|
|
34
|
+
{
|
|
35
|
+
prefix: "k",
|
|
36
|
+
multiplierFromPrevious: 1000
|
|
37
|
+
}, {
|
|
38
|
+
prefix: "M",
|
|
39
|
+
multiplierFromPrevious: 1000
|
|
40
|
+
}, {
|
|
41
|
+
prefix: "G",
|
|
42
|
+
multiplierFromPrevious: 1000
|
|
43
|
+
}, {
|
|
44
|
+
prefix: "T",
|
|
45
|
+
multiplierFromPrevious: 1000
|
|
46
|
+
}, {
|
|
47
|
+
prefix: "P",
|
|
48
|
+
multiplierFromPrevious: 1000
|
|
49
|
+
}, {
|
|
50
|
+
prefix: "E",
|
|
51
|
+
multiplierFromPrevious: 1000
|
|
52
|
+
}, {
|
|
53
|
+
prefix: "Z",
|
|
54
|
+
multiplierFromPrevious: 1000
|
|
55
|
+
}, {
|
|
56
|
+
prefix: "Y",
|
|
57
|
+
multiplierFromPrevious: 1000
|
|
58
|
+
}, {
|
|
59
|
+
prefix: "R",
|
|
60
|
+
multiplierFromPrevious: 1000
|
|
61
|
+
}, {
|
|
62
|
+
prefix: "Q",
|
|
63
|
+
multiplierFromPrevious: 1000
|
|
64
|
+
}]
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Convert description for small time based values
|
|
68
|
+
*
|
|
69
|
+
* Handles `ms`, `s`, `m` (minutes), `h`, `d`, `w` (weeks)
|
|
70
|
+
*
|
|
71
|
+
* @public
|
|
72
|
+
*/
|
|
73
|
+
export const timeUnit = {
|
|
74
|
+
basename: "s",
|
|
75
|
+
levels: [{
|
|
76
|
+
unitName: "ms",
|
|
77
|
+
multiplierFromPrevious: 1
|
|
78
|
+
}, {
|
|
79
|
+
multiplierFromPrevious: 1000
|
|
80
|
+
},
|
|
81
|
+
// Base level
|
|
82
|
+
{
|
|
83
|
+
unitName: "m",
|
|
84
|
+
multiplierFromPrevious: 60
|
|
85
|
+
}, {
|
|
86
|
+
unitName: "h",
|
|
87
|
+
multiplierFromPrevious: 60
|
|
88
|
+
}, {
|
|
89
|
+
unitName: "d",
|
|
90
|
+
multiplierFromPrevious: 24
|
|
91
|
+
}, {
|
|
92
|
+
unitName: "w",
|
|
93
|
+
multiplierFromPrevious: 7
|
|
94
|
+
}]
|
|
95
|
+
};
|
|
96
|
+
/** Return the full name of a level unit */
|
|
97
|
+
const getUnitLevel = (unitDescription, level) => {
|
|
98
|
+
const lvl = unitDescription.levels[level];
|
|
99
|
+
return `${lvl.prefix ?? ""}${lvl.unitName ?? unitDescription.basename}${lvl.suffix ?? ""}`;
|
|
100
|
+
};
|
|
101
|
+
/** Prefix/suffix the numeric value with the requested unit level */
|
|
102
|
+
const addUnitName = (unitDescription, numericValue, levelOrUnit) => {
|
|
103
|
+
const unitStr = typeof levelOrUnit === "string" ? levelOrUnit : getUnitLevel(unitDescription, levelOrUnit);
|
|
104
|
+
if (unitDescription.prefixUnit) return `${unitStr}${numericValue}`;
|
|
105
|
+
return `${numericValue}${unitStr}`;
|
|
106
|
+
};
|
|
107
|
+
/** Convert string unit into level in description */
|
|
108
|
+
const convertUnit = (unitDescription, fromUnit, toUnit) => {
|
|
109
|
+
// eslint-disable-next-line @typescript-eslint/init-declarations
|
|
110
|
+
let from;
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/init-declarations
|
|
112
|
+
let to;
|
|
113
|
+
const fromTrimmed = fromUnit.trim();
|
|
114
|
+
const toTrimmed = toUnit.trim();
|
|
115
|
+
for (let candidateLevel = 0; (from === undefined || to === undefined) && candidateLevel < unitDescription.levels.length; ++candidateLevel) {
|
|
116
|
+
const candidateUnit = getUnitLevel(unitDescription, candidateLevel);
|
|
117
|
+
if (from === undefined && fromTrimmed === candidateUnit) from = candidateLevel;
|
|
118
|
+
if (to === undefined && toTrimmed === candidateUnit) to = candidateLevel;
|
|
119
|
+
}
|
|
120
|
+
if (from === undefined || to === undefined) {
|
|
121
|
+
throw new Error(`Invalid units ("${fromUnit}", "${toUnit}")`);
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
from,
|
|
125
|
+
to
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
const regexBefore = /^(?<unit>[^\d]+)\d+$/u;
|
|
129
|
+
const regexAfter = /^\d+(?<unit>[^\d]+)$/u;
|
|
130
|
+
/** Get the unit text from an input value */
|
|
131
|
+
const getUnitFromValue = (value, unitDesc) => {
|
|
132
|
+
if (typeof value !== "string") return unitDesc.basename;
|
|
133
|
+
const regex = unitDesc.prefixUnit ? regexBefore : regexAfter;
|
|
134
|
+
const res = regex.exec(value);
|
|
135
|
+
if (!res?.groups) return unitDesc.basename;
|
|
136
|
+
const unit = res.groups.unit;
|
|
137
|
+
return unit.trim();
|
|
138
|
+
};
|
|
139
|
+
const regexValue = /^(?:[^\d]*)(?<value>\d+)(?:[^\d]*)$/u;
|
|
140
|
+
/** Get the numeric value, stripping units */
|
|
141
|
+
const getNumberValue = value => {
|
|
142
|
+
if (typeof value === "number") return value;
|
|
143
|
+
const res = regexValue.exec(value);
|
|
144
|
+
if (!res?.groups) throw new Error(`Missing value (${value})`);
|
|
145
|
+
return parseFloat(res.groups.value);
|
|
146
|
+
};
|
|
147
|
+
/** Get the numeric value, stripping units */
|
|
148
|
+
const getBigintValue = value => {
|
|
149
|
+
if (typeof value === "bigint") return value;
|
|
150
|
+
if (typeof value === "number") return BigInt(value);
|
|
151
|
+
const res = regexValue.exec(value);
|
|
152
|
+
if (!res?.groups) throw new Error(`Missing value (${value})`);
|
|
153
|
+
return BigInt(res.groups.value);
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Convert a numerical value from a given unit multiplier to another.
|
|
157
|
+
*
|
|
158
|
+
* @param value - Either the value in the base unit multiplier, or a string with a unit
|
|
159
|
+
*
|
|
160
|
+
* @public
|
|
161
|
+
*/
|
|
162
|
+
export const unitConvert = (unitDescription, value, target = unitDescription.basename) => {
|
|
163
|
+
const fromUnit = getUnitFromValue(value, unitDescription);
|
|
164
|
+
const levels = convertUnit(unitDescription, fromUnit, target);
|
|
165
|
+
let resValue = getNumberValue(value);
|
|
166
|
+
let currentLevel = levels.from;
|
|
167
|
+
if (levels.from < levels.to) {
|
|
168
|
+
while (currentLevel < levels.to) {
|
|
169
|
+
++currentLevel;
|
|
170
|
+
resValue /= unitDescription.levels[currentLevel].multiplierFromPrevious;
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
while (currentLevel > levels.to) {
|
|
174
|
+
resValue *= unitDescription.levels[currentLevel].multiplierFromPrevious;
|
|
175
|
+
--currentLevel;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return resValue;
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* Convert a numerical value from a given unit multiplier to another.
|
|
182
|
+
*
|
|
183
|
+
* This is the bigint variant, and can only handle integer values.
|
|
184
|
+
*
|
|
185
|
+
* @param value - Either the value in the base unit multiplier, or a string with a unit
|
|
186
|
+
*
|
|
187
|
+
* @public
|
|
188
|
+
*/
|
|
189
|
+
export const unitConvertBigint = (unitDescription, value, target = unitDescription.basename) => {
|
|
190
|
+
const fromUnit = getUnitFromValue(value, unitDescription);
|
|
191
|
+
const levels = convertUnit(unitDescription, fromUnit, target);
|
|
192
|
+
let resValue = getBigintValue(value);
|
|
193
|
+
let currentLevel = levels.from;
|
|
194
|
+
if (levels.from < levels.to) {
|
|
195
|
+
while (currentLevel < levels.to) {
|
|
196
|
+
++currentLevel;
|
|
197
|
+
resValue /= BigInt(unitDescription.levels[currentLevel].multiplierFromPrevious);
|
|
198
|
+
}
|
|
199
|
+
} else {
|
|
200
|
+
while (currentLevel > levels.to) {
|
|
201
|
+
resValue *= BigInt(unitDescription.levels[currentLevel].multiplierFromPrevious);
|
|
202
|
+
--currentLevel;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return resValue;
|
|
206
|
+
};
|
|
207
|
+
/**
|
|
208
|
+
* Display a value in the largest relevant unit.
|
|
209
|
+
*
|
|
210
|
+
* For example, a call with "5678B" would return "5.68kB".
|
|
211
|
+
* This will lose precision and is destined to human reading.
|
|
212
|
+
*
|
|
213
|
+
* @public
|
|
214
|
+
*/
|
|
215
|
+
export const unitString = (unitDescription, value, decimalPlaces = 2) => {
|
|
216
|
+
let candidateLevel = 1;
|
|
217
|
+
for (; candidateLevel < unitDescription.levels.length; ++candidateLevel) {
|
|
218
|
+
const unitName = getUnitLevel(unitDescription, candidateLevel);
|
|
219
|
+
const convertedValue = unitConvert(unitDescription, value, unitName);
|
|
220
|
+
if (convertedValue < 1) break;
|
|
221
|
+
}
|
|
222
|
+
--candidateLevel;
|
|
223
|
+
const unitName = getUnitLevel(unitDescription, candidateLevel);
|
|
224
|
+
const convertedValue = roundDecimals(unitConvert(unitDescription, value, unitName), decimalPlaces);
|
|
225
|
+
return addUnitName(unitDescription, convertedValue.toString(), unitName);
|
|
226
|
+
};
|
|
227
|
+
/**
|
|
228
|
+
* Display a value in the largest relevant unit.
|
|
229
|
+
*
|
|
230
|
+
* Bigint variant, will not return decimal values.
|
|
231
|
+
*
|
|
232
|
+
* For example, a call with "5678B" would return "5,7kB".
|
|
233
|
+
* This will lose precision and is destined to human reading.
|
|
234
|
+
*
|
|
235
|
+
* @public
|
|
236
|
+
*/
|
|
237
|
+
export const unitStringBigint = (unitDescription, value) => {
|
|
238
|
+
let candidateLevel = 1;
|
|
239
|
+
for (; candidateLevel < unitDescription.levels.length; ++candidateLevel) {
|
|
240
|
+
const unitName = getUnitLevel(unitDescription, candidateLevel);
|
|
241
|
+
const convertedValue = unitConvertBigint(unitDescription, value, unitName);
|
|
242
|
+
if (convertedValue < 1) break;
|
|
243
|
+
}
|
|
244
|
+
--candidateLevel;
|
|
245
|
+
const unitName = getUnitLevel(unitDescription, candidateLevel);
|
|
246
|
+
const convertedValue = unitConvertBigint(unitDescription, value, unitName);
|
|
247
|
+
return addUnitName(unitDescription, convertedValue.toString(), unitName);
|
|
248
|
+
};
|
|
249
|
+
/**
|
|
250
|
+
* Convert a size in bytes from one unit to another.
|
|
251
|
+
*
|
|
252
|
+
* @param targetUnit - Target unit (`kB`, `MB`, etc.). Defaults to `B`.
|
|
253
|
+
*
|
|
254
|
+
* @public
|
|
255
|
+
*/
|
|
256
|
+
export const byteConvert = (value, targetUnit) => unitConvert(byteUnit, value, targetUnit);
|
|
257
|
+
/**
|
|
258
|
+
* Convert a size in bytes from one unit to another.
|
|
259
|
+
*
|
|
260
|
+
* Bigint based function; can only handle integer values.
|
|
261
|
+
*
|
|
262
|
+
* @param targetUnit - Target unit (`kB`, `MB`, etc.). Defaults to `B`.
|
|
263
|
+
*
|
|
264
|
+
* @public
|
|
265
|
+
*/
|
|
266
|
+
export const byteConvertBigint = (value, targetUnit) => unitConvertBigint(byteUnit, value, targetUnit);
|
|
267
|
+
/**
|
|
268
|
+
* Display a human-readable byte size value.
|
|
269
|
+
*
|
|
270
|
+
* @public
|
|
271
|
+
*/
|
|
272
|
+
export const byteString = (value, decimalPlaces) => unitString(byteUnit, value, decimalPlaces);
|
|
273
|
+
/**
|
|
274
|
+
* Display a human-readable byte size value.
|
|
275
|
+
*
|
|
276
|
+
* Bigint variant, only handle integer values.
|
|
277
|
+
*
|
|
278
|
+
* @public
|
|
279
|
+
*/
|
|
280
|
+
export const byteStringBigint = value => unitStringBigint(byteUnit, value);
|
|
281
|
+
/**
|
|
282
|
+
* Convert a short time from one unit to another.
|
|
283
|
+
*
|
|
284
|
+
* @param targetUnit - Target unit (`ms`, `h`, etc.). Defaults to `s`.
|
|
285
|
+
*
|
|
286
|
+
* @public
|
|
287
|
+
*/
|
|
288
|
+
export const timeConvert = (value, targetUnit) => unitConvert(timeUnit, value, targetUnit);
|
|
289
|
+
/**
|
|
290
|
+
* Convert a short time from one unit to another.
|
|
291
|
+
*
|
|
292
|
+
* Biting based function; can only handle integer values.
|
|
293
|
+
*
|
|
294
|
+
* @param targetUnit - Target unit (`ms`, `h`, etc.). Defaults to `s`.
|
|
295
|
+
*
|
|
296
|
+
* @public
|
|
297
|
+
*/
|
|
298
|
+
export const timeConvertBigint = (value, targetUnit) => unitConvertBigint(timeUnit, value, targetUnit);
|
|
299
|
+
/**
|
|
300
|
+
* Display a human-readable short time value.
|
|
301
|
+
*
|
|
302
|
+
* @public
|
|
303
|
+
*/
|
|
304
|
+
export const timeString = (value, decimalPlaces) => unitString(timeUnit, value, decimalPlaces);
|
|
305
|
+
/**
|
|
306
|
+
* Display a human-readable short time value.
|
|
307
|
+
*
|
|
308
|
+
* Bigint variant, only handle integer values.
|
|
309
|
+
*
|
|
310
|
+
* @public
|
|
311
|
+
*/
|
|
312
|
+
export const timeStringBigint = value => unitStringBigint(timeUnit, value);
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Check that Buffer can convert from/to Base64.
|
|
18
|
+
*
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
export declare const haveBufferBase64: () => boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Check that Buffer can convert from/to UTF8.
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export declare const haveBufferUTF8: () => boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Check that Buffer can convert from/to hex.
|
|
30
|
+
*
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
export declare const haveBufferHex: () => boolean;
|
|
34
|
+
/** @internal */
|
|
35
|
+
export declare const bufferAb2B64: (data: ArrayBuffer) => string;
|
|
36
|
+
/** @internal */
|
|
37
|
+
export declare const bufferB642Ab: (data: string, strict?: boolean) => ArrayBuffer;
|
|
38
|
+
/** @internal */
|
|
39
|
+
export declare const bufferBuf82Hex: (buf8: Uint8Array, addPrefix?: boolean) => string;
|
|
40
|
+
/** @internal */
|
|
41
|
+
export declare const bufferHex2Buf8: (hex: string) => Uint8Array;
|
|
42
|
+
/** @internal */
|
|
43
|
+
export declare const bufferBuf82B64: (buf8: Uint8Array) => string;
|
|
44
|
+
/** @internal */
|
|
45
|
+
export declare const bufferB642Buf8: (b64: string, strict?: boolean) => Uint8Array;
|
|
46
|
+
/** @internal */
|
|
47
|
+
export declare const bufferBuf82Utf8: (buf8: Uint8Array) => string;
|
|
48
|
+
/** @internal */
|
|
49
|
+
export declare const bufferUtf82Buf8: (utf8: string) => Uint8Array;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
import { getArrayBuffer } from "../bits/arraybuffer.js";
|
|
17
|
+
import { b64Len, b64EffectiveData } from "../bits/base64.js";
|
|
18
|
+
import { clearHexInput } from "../bits/hex.js";
|
|
19
|
+
/** Check that Buffer is available somehow */
|
|
20
|
+
const haveBuffer = () => typeof Buffer !== "undefined";
|
|
21
|
+
/** Check that Buffer can convert from/to the given encoding */
|
|
22
|
+
const haveBufferEncoding = (encodingName, testValue) => {
|
|
23
|
+
if (!haveBuffer()) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
return Buffer.from(testValue, encodingName).toString(encodingName) === testValue;
|
|
28
|
+
} catch {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Check that Buffer can convert from/to Base64.
|
|
34
|
+
*
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
export const haveBufferBase64 = () => haveBufferEncoding("base64", "abcd");
|
|
38
|
+
/**
|
|
39
|
+
* Check that Buffer can convert from/to UTF8.
|
|
40
|
+
*
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
export const haveBufferUTF8 = () => haveBufferEncoding("utf8", "Spôŗjṫìck☺");
|
|
44
|
+
/**
|
|
45
|
+
* Check that Buffer can convert from/to hex.
|
|
46
|
+
*
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
export const haveBufferHex = () => haveBufferEncoding("hex", "deadbeef");
|
|
50
|
+
/** @internal */
|
|
51
|
+
export const bufferAb2B64 = data => Buffer.from(data).toString("base64");
|
|
52
|
+
/** @internal */
|
|
53
|
+
export const bufferB642Ab = (data, strict) => {
|
|
54
|
+
const buffer = Buffer.from(strict === undefined ? data : b64EffectiveData(data, strict), "base64");
|
|
55
|
+
if (strict && buffer.byteLength !== b64Len(data, true)) throw new Error("Invalid base64 length");
|
|
56
|
+
return getArrayBuffer(buffer);
|
|
57
|
+
};
|
|
58
|
+
/** @internal */
|
|
59
|
+
export const bufferBuf82Hex = (buf8, addPrefix) => {
|
|
60
|
+
const hexResult = Buffer.from(buf8).toString("hex");
|
|
61
|
+
return addPrefix ? `0x${hexResult}` : hexResult;
|
|
62
|
+
};
|
|
63
|
+
/** @internal */
|
|
64
|
+
export const bufferHex2Buf8 = hex => new Uint8Array(Buffer.from(clearHexInput(hex), "hex"));
|
|
65
|
+
/** @internal */
|
|
66
|
+
export const bufferBuf82B64 = buf8 => Buffer.from(buf8).toString("base64");
|
|
67
|
+
/** @internal */
|
|
68
|
+
export const bufferB642Buf8 = (b64, strict) => {
|
|
69
|
+
const result = new Uint8Array(Buffer.from(strict === undefined ? b64 : b64EffectiveData(b64, strict), "base64"));
|
|
70
|
+
if (strict && result.byteLength !== b64Len(b64, true)) throw new Error("Invalid base64 length");
|
|
71
|
+
return result;
|
|
72
|
+
};
|
|
73
|
+
/** @internal */
|
|
74
|
+
export const bufferBuf82Utf8 = buf8 => Buffer.from(buf8).toString("utf8");
|
|
75
|
+
/** @internal */
|
|
76
|
+
export const bufferUtf82Buf8 = utf8 => new Uint8Array(Buffer.from(utf8, "utf8"));
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Crawl a byte buffer four bytes at a time.
|
|
18
|
+
*
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
export default class FourBytes {
|
|
22
|
+
#private;
|
|
23
|
+
constructor(src: Uint8Array);
|
|
24
|
+
get byte1(): number | undefined;
|
|
25
|
+
get byte2(): number | undefined;
|
|
26
|
+
get byte3(): number | undefined;
|
|
27
|
+
get byte4(): number | undefined;
|
|
28
|
+
advance: (count: number) => void;
|
|
29
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
// The whole purpose of this class is to count to four.
|
|
17
|
+
/* eslint-disable @typescript-eslint/no-magic-numbers */
|
|
18
|
+
/**
|
|
19
|
+
* Crawl a byte buffer four bytes at a time.
|
|
20
|
+
*
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export default class FourBytes {
|
|
24
|
+
#src;
|
|
25
|
+
#cursor = 0;
|
|
26
|
+
constructor(src) {
|
|
27
|
+
this.#src = src;
|
|
28
|
+
}
|
|
29
|
+
get byte1() {
|
|
30
|
+
return this.#src[this.#cursor];
|
|
31
|
+
}
|
|
32
|
+
get byte2() {
|
|
33
|
+
return this.#src[this.#cursor + 1];
|
|
34
|
+
}
|
|
35
|
+
get byte3() {
|
|
36
|
+
return this.#src[this.#cursor + 2];
|
|
37
|
+
}
|
|
38
|
+
get byte4() {
|
|
39
|
+
return this.#src[this.#cursor + 3];
|
|
40
|
+
}
|
|
41
|
+
advance = count => {
|
|
42
|
+
this.#cursor += count;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/* eslint-enable @typescript-eslint/no-magic-numbers */
|