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.
- package/index.js +33 -8
- 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 = (
|
|
453
|
-
const result = {
|
|
454
|
-
for (const
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
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,
|