cafe-utility 1.41.0 → 1.45.0

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.
Files changed (2) hide show
  1. package/index.js +69 -3
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -901,6 +901,20 @@ const execAsync = async (command, resolveWithErrors, inherit, options) =>
901
901
  }
902
902
  })
903
903
 
904
+ const runProcess = async (command, args, onStdout, onStderr) =>
905
+ new Promise((resolve, reject) => {
906
+ const subprocess = ChildProcess.spawn(command, args)
907
+ subprocess.stdout.on('data', onStdout)
908
+ subprocess.stderr.on('data', onStderr)
909
+ subprocess.on('close', code => {
910
+ if (code === 0) {
911
+ resolve(code)
912
+ } else {
913
+ reject(code)
914
+ }
915
+ })
916
+ })
917
+
904
918
  const cloneWithJson = a => JSON.parse(JSON.stringify(a))
905
919
 
906
920
  const unixTimestamp = optionalTimestamp => Math.ceil((optionalTimestamp || Date.now()) / 1000)
@@ -1129,6 +1143,26 @@ const prettifyNumber = (number, precision = 1, long = false) => {
1129
1143
  }`
1130
1144
  }
1131
1145
 
1146
+ const parseIntOrThrow = numberOrString => {
1147
+ if (typeof numberOrString === 'number') {
1148
+ if (isNaN(numberOrString)) {
1149
+ throw Error('parseIntOrThrow got NaN for input')
1150
+ }
1151
+ if (!isFinite(numberOrString)) {
1152
+ throw Error('parseIntOrThrow got infinite for input')
1153
+ }
1154
+ return Math.trunc(numberOrString)
1155
+ }
1156
+ if (typeof numberOrString === 'string') {
1157
+ const parsed = parseInt(numberOrString, 10)
1158
+ if (isNaN(parsed)) {
1159
+ throw Error('parseIntOrThrow parsed NaN for input: ' + numberOrString)
1160
+ }
1161
+ return parsed
1162
+ }
1163
+ throw Error('parseIntOrThrow got unsupported input type: ' + typeof numberOrString)
1164
+ }
1165
+
1132
1166
  const clamp = (value, lower, upper) => (value < lower ? lower : value > upper ? upper : value)
1133
1167
 
1134
1168
  const increment = (value, change, maximum) => {
@@ -1378,6 +1412,32 @@ const transformToArray = objectOfArrays => {
1378
1412
  return array
1379
1413
  }
1380
1414
 
1415
+ const incrementMulti = (objects, key, step = 1) => {
1416
+ for (const object of objects) {
1417
+ object[key] += step
1418
+ }
1419
+ }
1420
+
1421
+ const setMulti = (objects, key, value) => {
1422
+ for (const object of objects) {
1423
+ object[key] = value
1424
+ }
1425
+ }
1426
+
1427
+ const createFastIndex = () => ({ index: {}, keys: [] })
1428
+
1429
+ const pushToFastIndex = (object, key, item, limit = 100) => {
1430
+ if (object.index[key]) {
1431
+ return
1432
+ }
1433
+ object.index[key] = item
1434
+ object.keys.push(key)
1435
+ if (object.keys.length > limit) {
1436
+ const oldKey = object.keys.shift()
1437
+ delete object.index[oldKey]
1438
+ }
1439
+ }
1440
+
1381
1441
  module.exports = {
1382
1442
  Random: {
1383
1443
  inclusiveInt: randomIntInclusive,
@@ -1419,7 +1479,8 @@ module.exports = {
1419
1479
  waitFor,
1420
1480
  execAsync,
1421
1481
  getHeapMegabytes,
1422
- expandError
1482
+ expandError,
1483
+ runProcess
1423
1484
  },
1424
1485
  Numbers: {
1425
1486
  sum,
@@ -1430,7 +1491,8 @@ module.exports = {
1430
1491
  createSequence,
1431
1492
  increment,
1432
1493
  decrement,
1433
- prettify: prettifyNumber
1494
+ prettify: prettifyNumber,
1495
+ parseIntOrThrow
1434
1496
  },
1435
1497
  Promises: {
1436
1498
  raceFulfilled,
@@ -1486,7 +1548,11 @@ module.exports = {
1486
1548
  flip,
1487
1549
  crossJoin,
1488
1550
  countTruthyValues,
1489
- transformToArray
1551
+ transformToArray,
1552
+ setMulti,
1553
+ incrementMulti,
1554
+ createFastIndex,
1555
+ pushToFastIndex
1490
1556
  },
1491
1557
  Pagination: {
1492
1558
  asPageNumber,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "1.41.0",
3
+ "version": "1.45.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {