cafe-utility 1.36.0 → 1.40.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 +46 -2
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -669,6 +669,10 @@ const scheduleMany = (handlers, dates) => {
669
669
 
670
670
  const interpolate = (a, b, t) => a + (b - a) * t
671
671
 
672
+ const sum = array => array.reduce((a, b) => a + b, 0)
673
+
674
+ const average = array => array.reduce((a, b) => a + b, 0) / array.length
675
+
672
676
  const range = (start, end) => {
673
677
  const array = []
674
678
  for (let i = start; i <= end; i++) {
@@ -697,6 +701,10 @@ const slugify = string =>
697
701
  .replace(/-+/g, '-')
698
702
  .replace(/^-|-$/g, '')
699
703
 
704
+ const camelToTitle = string => capitalize(string.replace(/([A-Z])/g, ' $1'))
705
+
706
+ const slugToTitle = string => string.split('-').map(capitalize).join(' ')
707
+
700
708
  const surroundInOut = (string, filler) => filler + string.split('').join(filler) + filler
701
709
 
702
710
  const enumify = string => slugify(string).replace(/-/g, '_').toUpperCase()
@@ -1090,6 +1098,37 @@ const createSequence = () => {
1090
1098
  return { next: () => value++ }
1091
1099
  }
1092
1100
 
1101
+ const thresholds = [1e3, 1e6, 1e9, 1e12, 1e15, 1e18, 1e21, 1e24, 1e27, 1e30, 1e33]
1102
+ const longNumberUnits = [
1103
+ 'thousand',
1104
+ 'million',
1105
+ 'billion',
1106
+ 'trillion',
1107
+ 'quadrillion',
1108
+ 'quintillion',
1109
+ 'sextillion',
1110
+ 'septillion',
1111
+ 'octillion',
1112
+ 'nonillion',
1113
+ 'decillion'
1114
+ ]
1115
+ const shortNumberUnits = ['K', 'M', 'B', 't', 'q', 'Q', 's', 'S', 'o', 'n', 'd']
1116
+
1117
+ const prettifyNumber = (number, precision = 1, long = false) => {
1118
+ const table = long ? longNumberUnits : shortNumberUnits
1119
+ if (number < thresholds[0]) {
1120
+ return number
1121
+ }
1122
+ for (let i = 0; i < thresholds.length - 1; i++) {
1123
+ if (number < thresholds[i + 1]) {
1124
+ return `${(number / thresholds[i]).toFixed(precision)}${long ? ' ' : ''}${table[i]}`
1125
+ }
1126
+ }
1127
+ return `${(number / thresholds[thresholds.length - 1]).toFixed(precision)}${long ? ' ' : ''}${
1128
+ table[thresholds.length - 1]
1129
+ }`
1130
+ }
1131
+
1093
1132
  const clamp = (value, lower, upper) => (value < lower ? lower : value > upper ? upper : value)
1094
1133
 
1095
1134
  const increment = (value, change, maximum) => {
@@ -1369,12 +1408,15 @@ module.exports = {
1369
1408
  expandError
1370
1409
  },
1371
1410
  Numbers: {
1411
+ sum,
1412
+ average,
1372
1413
  clamp,
1373
1414
  range,
1374
1415
  interpolate,
1375
1416
  createSequence,
1376
1417
  increment,
1377
- decrement
1418
+ decrement,
1419
+ prettify: prettifyNumber
1378
1420
  },
1379
1421
  Promises: {
1380
1422
  raceFulfilled,
@@ -1501,7 +1543,9 @@ module.exports = {
1501
1543
  getExtension,
1502
1544
  getBasename,
1503
1545
  normalizeFilename,
1504
- parseFilename
1546
+ parseFilename,
1547
+ camelToTitle,
1548
+ slugToTitle
1505
1549
  },
1506
1550
  Assertions: {
1507
1551
  asTrue,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "1.36.0",
3
+ "version": "1.40.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {