cafe-utility 1.40.0 → 1.44.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 +68 -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) => {
@@ -1364,6 +1398,32 @@ const countUnique = (array, mapper, plain, sort, reverse) => {
1364
1398
 
1365
1399
  const sortObjectValues = (object, compareFn) => Object.fromEntries(Object.entries(object).sort(compareFn))
1366
1400
 
1401
+ const transformToArray = objectOfArrays => {
1402
+ const array = []
1403
+ const keys = Object.keys(objectOfArrays)
1404
+ const length = objectOfArrays[keys[0]].length
1405
+ for (let i = 0; i < length; i++) {
1406
+ const object = {}
1407
+ for (const key of keys) {
1408
+ object[key] = objectOfArrays[key][i]
1409
+ }
1410
+ array.push(object)
1411
+ }
1412
+ return array
1413
+ }
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
+
1367
1427
  module.exports = {
1368
1428
  Random: {
1369
1429
  inclusiveInt: randomIntInclusive,
@@ -1405,7 +1465,8 @@ module.exports = {
1405
1465
  waitFor,
1406
1466
  execAsync,
1407
1467
  getHeapMegabytes,
1408
- expandError
1468
+ expandError,
1469
+ runProcess
1409
1470
  },
1410
1471
  Numbers: {
1411
1472
  sum,
@@ -1416,7 +1477,8 @@ module.exports = {
1416
1477
  createSequence,
1417
1478
  increment,
1418
1479
  decrement,
1419
- prettify: prettifyNumber
1480
+ prettify: prettifyNumber,
1481
+ parseIntOrThrow
1420
1482
  },
1421
1483
  Promises: {
1422
1484
  raceFulfilled,
@@ -1471,7 +1533,10 @@ module.exports = {
1471
1533
  setSomeOnObject,
1472
1534
  flip,
1473
1535
  crossJoin,
1474
- countTruthyValues
1536
+ countTruthyValues,
1537
+ transformToArray,
1538
+ setMulti,
1539
+ incrementMulti
1475
1540
  },
1476
1541
  Pagination: {
1477
1542
  asPageNumber,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "1.40.0",
3
+ "version": "1.44.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {