@beauraines/node-helpers 4.1.1 → 4.2.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/CHANGELOG.md +8 -0
- package/package.json +1 -1
- package/src/helpers.js +37 -11
- package/src/helpers.test.js +14 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [4.2.0](https://github.com/beauraines/node-helpers/compare/v4.1.1...v4.2.0) (2024-10-06)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **sparkline:** option support for range coercion ([053b14d](https://github.com/beauraines/node-helpers/commit/053b14d1c7334691ea0ab97b3c24f719d05aeda0))
|
|
11
|
+
* **sparkline:** optionally make a sparkline wiht out the label and descriptive stats ([379330c](https://github.com/beauraines/node-helpers/commit/379330c4f8fb8723b500f65de5a6ed247485d81f))
|
|
12
|
+
|
|
5
13
|
### [4.1.1](https://github.com/beauraines/node-helpers/compare/v4.1.0...v4.1.1) (2024-10-05)
|
|
6
14
|
|
|
7
15
|
## [4.1.0](https://github.com/beauraines/node-helpers/compare/v4.0.39...v4.1.0) (2024-10-05)
|
package/package.json
CHANGED
package/src/helpers.js
CHANGED
|
@@ -107,32 +107,58 @@ function getEpochMillis() {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
|
|
110
|
-
|
|
111
|
-
// Expected output last 30 days [1,5] ▁▂▄▆█ 5 from [1,2,3,4,5]
|
|
110
|
+
|
|
112
111
|
/**
|
|
113
|
-
* Generates a sparkline with labels
|
|
112
|
+
* Generates a sparkline optionally with labels
|
|
113
|
+
*
|
|
114
|
+
* labelled sparkline includes a label, min, max and last value
|
|
115
|
+
*
|
|
116
|
+
* last 30 days [1,5] ▁▂▄▆█ 5
|
|
117
|
+
*
|
|
118
|
+
* unlabled is just that ▁▂▄▆█
|
|
119
|
+
*
|
|
120
|
+
* Options supported are
|
|
121
|
+
*
|
|
122
|
+
* {
|
|
123
|
+
* coerceData: true, // coerces the minimum value to zero
|
|
124
|
+
* }
|
|
114
125
|
*
|
|
115
126
|
* @param {array} data Array of values to plot in the sparkline
|
|
116
|
-
* @param {string} label Text to display before sparkline
|
|
127
|
+
* @param {string} label Text to display before sparkline, if empty or null, will not display any labels
|
|
117
128
|
* @param {object} options Optional options for display, e.g display min,max,last, range coercion
|
|
129
|
+
*
|
|
118
130
|
* @returns
|
|
119
131
|
*/
|
|
120
132
|
// eslint-disable-next-line no-unused-vars
|
|
121
133
|
function sparkline(data,label,options) {
|
|
122
|
-
// TODO add handling if data is object
|
|
123
|
-
// let open = last30days.map( x=> x.open_count)
|
|
124
134
|
|
|
135
|
+
options = {
|
|
136
|
+
coerceData: true,
|
|
137
|
+
...options
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// TODO add handling if data is object
|
|
125
141
|
// Assuming data is array
|
|
126
142
|
const minValue = Math.min(...data)
|
|
127
143
|
const maxValue = Math.max(...data)
|
|
128
144
|
const lastValue = data.slice(-1)[0]
|
|
129
145
|
|
|
130
|
-
//
|
|
131
|
-
|
|
132
|
-
|
|
146
|
+
// This is the default behavior
|
|
147
|
+
if ( options.coerceData ) {
|
|
148
|
+
// coerces the minimum value to zero because the mimimum option is only used for range validation,
|
|
149
|
+
// not display https://github.com/sindresorhus/sparkly/blob/9e33eaff891c41e8fb8c8883f62e9821729a9882/index.js#L15
|
|
150
|
+
// sparkly(open,{minimum:27,maximum:50})
|
|
151
|
+
data = data.map( x=> x- minValue)
|
|
152
|
+
}
|
|
153
|
+
|
|
133
154
|
|
|
134
|
-
|
|
135
|
-
|
|
155
|
+
let sparkline
|
|
156
|
+
if (label) {
|
|
157
|
+
sparkline = `${label} [${minValue},${maxValue}] ${sparkly(data)} ${lastValue}`
|
|
158
|
+
} else {
|
|
159
|
+
sparkline = sparkly(data)
|
|
160
|
+
}
|
|
161
|
+
return sparkline
|
|
136
162
|
}
|
|
137
163
|
|
|
138
164
|
/**
|
package/src/helpers.test.js
CHANGED
|
@@ -8,14 +8,27 @@ describe('helpers',()=> {
|
|
|
8
8
|
expect(helper.toTitleCase(lowerString)).toBe(titleString);
|
|
9
9
|
})
|
|
10
10
|
|
|
11
|
-
it('should return the correctly sparkline',()=>{
|
|
11
|
+
it('should return the correctly formatted sparkline',()=>{
|
|
12
12
|
const expectedOutput = 'last 30 days [1,5] ▁▂▄▆█ 5'
|
|
13
13
|
const input = [1,2,3,4,5]
|
|
14
14
|
const label = 'last 30 days'
|
|
15
15
|
expect(helper.sparkline(input,label)).toBe(expectedOutput)
|
|
16
|
+
})
|
|
16
17
|
|
|
18
|
+
it('should return a sparkline with no additional labeling',()=>{
|
|
19
|
+
const expectedOutput = '▁▂▄▆█'
|
|
20
|
+
const input = [1,2,3,4,5]
|
|
21
|
+
const label = ''
|
|
22
|
+
expect(helper.sparkline(input,label)).toBe(expectedOutput)
|
|
17
23
|
})
|
|
18
24
|
|
|
25
|
+
it('should return a sparkline with no range coercion',()=>{
|
|
26
|
+
const expectedOutput = '▂▄▅▇█'
|
|
27
|
+
const input = [1,2,3,4,5]
|
|
28
|
+
const label = ''
|
|
29
|
+
const options = { coerceData: false }
|
|
30
|
+
expect(helper.sparkline(input,label, options)).toBe(expectedOutput)
|
|
31
|
+
})
|
|
19
32
|
|
|
20
33
|
})
|
|
21
34
|
|