@sjcrh/proteinpaint-shared 2.87.0 → 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.0",
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",
@@ -1,4 +1,4 @@
1
- import { roundValue } from './roundValue.js'
1
+ import { roundValueAuto } from './roundValue.js'
2
2
 
3
3
  /* This file generates summary statistics on any given array of numbers*/
4
4
 
@@ -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: roundValue(min, 2) },
46
- { id: 'p25', label: '1st quartile', value: roundValue(p25, 2) },
47
- { id: 'median', label: 'Median', value: roundValue(median, 2) },
48
- { id: 'mean', label: 'Mean', value: roundValue(mean(arr), 2) },
49
- { id: 'p75', label: '3rd quartile', value: roundValue(p75, 2) },
50
- { id: 'max', label: 'Maximum', value: roundValue(max, 2) },
51
- { id: 'SD', label: 'Standard deviation', value: roundValue(stdDev, 2) },
52
- { id: 'variance', label: 'Variance', value: roundValue(variance, 2) },
53
- { id: 'IQR', label: 'Inter-quartile range', value: roundValue(IQR, 2) }
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
@@ -7,6 +7,7 @@ round a value to specified digits
7
7
  value: given value
8
8
  digits: number of digits to round to
9
9
  */
10
+
10
11
  export function roundValue(value, digits) {
11
12
  const v = Number(value)
12
13
  if (Number.isInteger(v)) return v
@@ -14,10 +15,18 @@ export function roundValue(value, digits) {
14
15
  return Number(v.toFixed(digits))
15
16
  }
16
17
 
17
- export function roundValueAuto(value) {
18
- const dp = decimalPlacesUntilFirstNonZero(value)
19
- let digits = Math.abs(value) > 1 ? 2 : dp > 0 ? dp + 1 : 2
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
+ */
20
24
 
25
+ export function roundValueAuto(value, format = false) {
26
+ if (!value && value != 0) return value
27
+ const dp = decimalPlacesUntilFirstNonZero(value)
28
+ const digits = Math.abs(value) > 1 ? 2 : dp > 0 ? dp + 1 : 2
29
+ if (format) return formatValue(value, digits)
21
30
  return roundValue(value, digits)
22
31
  }
23
32
 
@@ -46,3 +55,39 @@ export function decimalPlacesUntilFirstNonZero(number) {
46
55
 
47
56
  return decimalPlaces
48
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
+ }