cafe-utility 1.30.0 → 1.34.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 +48 -5
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -15,6 +15,18 @@ const raceFulfilled = promises => invertPromise(Promise.all(promises.map(invertP
15
15
 
16
16
  const invertPromise = promise => new Promise((resolve, reject) => promise.then(reject, resolve))
17
17
 
18
+ const runInParallelBatches = async (promises, concurrency = 1) => {
19
+ const batches = splitByCount(promises, concurrency)
20
+ const results = []
21
+ const jobs = batches.map(async batch => {
22
+ for (const promise of batch) {
23
+ results.push(await promise())
24
+ }
25
+ })
26
+ await Promise.all(jobs)
27
+ return results
28
+ }
29
+
18
30
  const sleepMillis = millis =>
19
31
  new Promise(resolve =>
20
32
  setTimeout(() => {
@@ -750,8 +762,26 @@ const splitOnce = (string, separator) =>
750
762
 
751
763
  const getExtension = string => {
752
764
  const name = last(string.split(/\\|\//g))
753
- const lastIndexOf = name.lastIndexOf('.')
754
- return lastIndexOf <= 0 ? '' : name.slice(lastIndexOf + 1)
765
+ const lastIndex = name.lastIndexOf('.')
766
+ return lastIndex <= 0 ? '' : name.slice(lastIndex + 1)
767
+ }
768
+
769
+ const getBasename = string => {
770
+ const name = last(string.split(/\\|\//g))
771
+ const index = name.indexOf('.', 1)
772
+ return index <= 0 ? string : name.slice(0, index)
773
+ }
774
+
775
+ const normalizeFilename = string => `${getBasename(string)}.${getExtension(string)}`
776
+
777
+ const parseFilename = string => {
778
+ const basename = getBasename(string)
779
+ const extension = getExtension(string)
780
+ return {
781
+ basename,
782
+ extension,
783
+ filename: `${basename}.${extension}`
784
+ }
755
785
  }
756
786
 
757
787
  const randomize = string => string.replace(/\{(.+?)\}/g, (_, group) => pick(group.split('|')))
@@ -1138,6 +1168,14 @@ const setSomeOnObject = (object, key, value) => {
1138
1168
  }
1139
1169
  }
1140
1170
 
1171
+ const flip = object => {
1172
+ const result = {}
1173
+ for (const [key, value] of Object.entries(object)) {
1174
+ result[value] = key
1175
+ }
1176
+ return result
1177
+ }
1178
+
1141
1179
  const getFlatNotation = (prefix, key, bracket) =>
1142
1180
  prefix + (bracket ? '[' + key + ']' : (prefix.length ? '.' : '') + key)
1143
1181
 
@@ -1305,7 +1343,8 @@ module.exports = {
1305
1343
  },
1306
1344
  Promises: {
1307
1345
  raceFulfilled,
1308
- invert: invertPromise
1346
+ invert: invertPromise,
1347
+ runInParallelBatches
1309
1348
  },
1310
1349
  Dates: {
1311
1350
  getAgo,
@@ -1352,7 +1391,8 @@ module.exports = {
1352
1391
  sort: sortObjectValues,
1353
1392
  map: mapObject,
1354
1393
  rethrow,
1355
- setSomeOnObject
1394
+ setSomeOnObject,
1395
+ flip
1356
1396
  },
1357
1397
  Pagination: {
1358
1398
  asPageNumber,
@@ -1421,7 +1461,10 @@ module.exports = {
1421
1461
  csvEscape,
1422
1462
  parseCsv,
1423
1463
  surroundInOut,
1424
- getExtension
1464
+ getExtension,
1465
+ getBasename,
1466
+ normalizeFilename,
1467
+ parseFilename
1425
1468
  },
1426
1469
  Assertions: {
1427
1470
  asTrue,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "1.30.0",
3
+ "version": "1.34.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {