@sjcrh/proteinpaint-shared 2.87.1 → 2.88.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sjcrh/proteinpaint-shared",
3
- "version": "2.87.1",
3
+ "version": "2.88.2",
4
4
  "description": "ProteinPaint code that is shared between server and client-side workspaces",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -42,15 +42,15 @@ export default function summaryStats(array) {
42
42
  return {
43
43
  values: [
44
44
  { id: 'total', label: 'Total', value: n },
45
- { id: 'min', label: 'Minimum', value: roundValueAuto(min) },
46
- { id: 'p25', label: '1st quartile', value: roundValueAuto(p25) },
47
- { id: 'median', label: 'Median', value: roundValueAuto(median) },
48
- { id: 'mean', label: 'Mean', value: roundValueAuto(mean(arr)) },
49
- { id: 'p75', label: '3rd quartile', value: roundValueAuto(p75) },
50
- { id: 'max', label: 'Maximum', value: roundValueAuto(max) },
51
- { id: 'SD', label: 'Standard deviation', value: roundValueAuto(stdDev) },
52
- { id: 'variance', label: 'Variance', value: roundValueAuto(variance) },
53
- { id: 'IQR', label: 'Inter-quartile range', value: roundValueAuto(IQR) }
45
+ { id: 'min', label: 'Minimum', value: roundValueAuto(min, true) },
46
+ { id: 'p25', label: '1st quartile', value: roundValueAuto(p25, true) },
47
+ { id: 'median', label: 'Median', value: roundValueAuto(median, true) },
48
+ { id: 'mean', label: 'Mean', value: roundValueAuto(mean(arr), true) },
49
+ { id: 'p75', label: '3rd quartile', value: roundValueAuto(p75, true) },
50
+ { id: 'max', label: 'Maximum', value: roundValueAuto(max, true) },
51
+ { id: 'SD', label: 'Standard deviation', value: roundValueAuto(stdDev, true) },
52
+ { id: 'variance', label: 'Variance', value: roundValueAuto(variance, true) },
53
+ { id: 'IQR', label: 'Inter-quartile range', value: roundValueAuto(IQR, true) }
54
54
  ]
55
55
  }
56
56
  }
package/src/roundValue.js CHANGED
@@ -11,19 +11,22 @@ digits: number of digits to round to
11
11
  export function roundValue(value, digits) {
12
12
  const v = Number(value)
13
13
  if (Number.isInteger(v)) return v
14
- const abs = Math.abs(v)
15
- if (abs < 1 || abs > 9999) {
16
- //Number() reverts positive values less than 10^21 to a whole number
17
- return abs > 9999 ? v.toPrecision(digits) : Number(v.toPrecision(digits))
18
- }
14
+ if (Math.abs(v) < 1) return Number(v.toPrecision(digits))
19
15
  return Number(v.toFixed(digits))
20
16
  }
21
17
 
22
- export function roundValueAuto(value) {
18
+ /** Rounds numbers to the appropriate decimal point
19
+ * if format is true, returns either a number or string in
20
+ * scientific notation.
21
+ *
22
+ * TODO: Review digit logic.
23
+ */
24
+
25
+ export function roundValueAuto(value, format = false) {
23
26
  if (!value && value != 0) return value
24
27
  const dp = decimalPlacesUntilFirstNonZero(value)
25
28
  const digits = Math.abs(value) > 1 ? 2 : dp > 0 ? dp + 1 : 2
26
-
29
+ if (format) return formatValue(value, digits)
27
30
  return roundValue(value, digits)
28
31
  }
29
32
 
@@ -52,3 +55,39 @@ export function decimalPlacesUntilFirstNonZero(number) {
52
55
 
53
56
  return decimalPlaces
54
57
  }
58
+
59
+ /*
60
+ simple logic to return a number close to original while rounding up decimals.
61
+ supplements roundValueAuto which rounds 12345 to 1.2e4 which is only suitable for human quick glance but not subsequent computing
62
+
63
+ TODO:
64
+ 10000 and 10001 to 1e4
65
+ 0.00001 to 1e-5
66
+ 1.00001 to 1
67
+ */
68
+ export function roundValue2(value) {
69
+ if (!Number.isFinite(value)) return value // not a number
70
+ if (Number.isInteger(value)) return value // is integer, do not convert
71
+ const abs = Math.abs(value)
72
+ if (abs > 100) return Math.round(value) // 12345.1234 to 12345 (compared to 1.2e4 from roundValueAuto)
73
+ if (abs > 10) return Number(value.toFixed(1)) // 99.1234 to 99.1
74
+ if (abs > 1) return Number(value.toFixed(2)) // 9.1234 to 9.12
75
+ if (abs > 0.1) return Number(value.toFixed(3)) // 0.12345 to 0.123
76
+ if (abs > 0.01) return Number(value.toFixed(4)) // 0.012345 to 0.0123
77
+ return value // as is
78
+ }
79
+
80
+ /** Use to return displayed values in scientific notation
81
+ * Do not use for values intended for calculation later.
82
+ */
83
+ export function formatValue(value, digits) {
84
+ const v = Number(value)
85
+ if (Number.isInteger(v)) return v
86
+ const abs = Math.abs(v)
87
+ if (abs < 1 || abs > 9999) {
88
+ //Number() reverts positive values less than 10^21 to a whole number
89
+ //To return the value in scientific notation, use toPrecision without Number()
90
+ return abs > 9999 ? v.toPrecision(digits) : Number(v.toPrecision(digits))
91
+ }
92
+ return Number(v.toFixed(digits))
93
+ }