cafe-utility 2.1.0 → 3.1.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 +33 -8
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -449,18 +449,22 @@ const mergeDeep = (target, source) => {
449
449
  }
450
450
  }
451
451
 
452
- const zip = (first, second, reducer) => {
453
- const result = { ...first }
454
- for (const entry of Object.entries(second)) {
455
- if (result[entry[0]]) {
456
- result[entry[0]] = reducer(result[entry[0]], entry[1])
457
- } else {
458
- result[entry[0]] = entry[1]
452
+ const zip = (objects, reducer) => {
453
+ const result = {}
454
+ for (const object of objects) {
455
+ for (const key of Object.keys(object)) {
456
+ if (result[key]) {
457
+ result[key] = reducer(result[key], object[key])
458
+ } else {
459
+ result[key] = object[key]
460
+ }
459
461
  }
460
462
  }
461
463
  return result
462
464
  }
463
465
 
466
+ const zipSum = objects => zip(objects, (x, y) => x + y)
467
+
464
468
  /**
465
469
  * @param {*} value
466
470
  * @returns {number}
@@ -652,6 +656,21 @@ const camelToTitle = string => capitalize(string.replace(/([A-Z])/g, ' $1'))
652
656
 
653
657
  const slugToTitle = string => string.split('-').map(capitalize).join(' ')
654
658
 
659
+ const slugToCamel = string => decapitalize(string.split('-').map(capitalize).join(''))
660
+
661
+ const joinHumanly = (parts, separator = ', ', lastSeparator = ' and ') => {
662
+ if (!parts || !parts.length) {
663
+ return null
664
+ }
665
+ if (parts.length === 1) {
666
+ return parts[0]
667
+ }
668
+ if (parts.length === 2) {
669
+ return `${parts[0]}${lastSeparator}${parts[1]}`
670
+ }
671
+ return `${parts.slice(0, parts.length - 1).join(separator)}${lastSeparator}${parts[parts.length - 1]}`
672
+ }
673
+
655
674
  const surroundInOut = (string, filler) => filler + string.split('').join(filler) + filler
656
675
 
657
676
  const enumify = string => slugify(string).replace(/-/g, '_').toUpperCase()
@@ -752,6 +771,8 @@ const shrinkTrim = string => string.replace(/\s+/g, ' ').replace(/\s$|^\s/g, '')
752
771
 
753
772
  const capitalize = string => string.charAt(0).toUpperCase() + string.slice(1)
754
773
 
774
+ const decapitalize = string => string.charAt(0).toLowerCase() + string.slice(1)
775
+
755
776
  const csvEscape = string => (string.match(/"|,/) ? `"${string.replace(/"/g, '""')}"` : string)
756
777
 
757
778
  const parseCsv = (string, delimiter = ',', quote = '"') => {
@@ -1497,6 +1518,7 @@ module.exports = {
1497
1518
  runOn,
1498
1519
  ifPresent,
1499
1520
  zip,
1521
+ zipSum,
1500
1522
  removeEmptyArrays,
1501
1523
  removeEmptyValues,
1502
1524
  flatten,
@@ -1581,6 +1603,7 @@ module.exports = {
1581
1603
  randomize,
1582
1604
  shrinkTrim,
1583
1605
  capitalize,
1606
+ decapitalize,
1584
1607
  csvEscape,
1585
1608
  parseCsv,
1586
1609
  surroundInOut,
@@ -1589,7 +1612,9 @@ module.exports = {
1589
1612
  normalizeFilename,
1590
1613
  parseFilename,
1591
1614
  camelToTitle,
1592
- slugToTitle
1615
+ slugToTitle,
1616
+ slugToCamel,
1617
+ joinHumanly
1593
1618
  },
1594
1619
  Assertions: {
1595
1620
  asEqual,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "2.1.0",
3
+ "version": "3.1.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {