@beauraines/node-helpers 4.1.0 → 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 +10 -0
- package/package.json +1 -1
- package/src/helpers.js +39 -10
- package/src/helpers.test.js +14 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
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
|
+
|
|
13
|
+
### [4.1.1](https://github.com/beauraines/node-helpers/compare/v4.1.0...v4.1.1) (2024-10-05)
|
|
14
|
+
|
|
5
15
|
## [4.1.0](https://github.com/beauraines/node-helpers/compare/v4.0.39...v4.1.0) (2024-10-05)
|
|
6
16
|
|
|
7
17
|
|
package/package.json
CHANGED
package/src/helpers.js
CHANGED
|
@@ -106,30 +106,59 @@ function getEpochMillis() {
|
|
|
106
106
|
return Date.now()
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
|
|
110
|
-
|
|
109
|
+
|
|
110
|
+
|
|
111
111
|
/**
|
|
112
|
-
* 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
|
+
* }
|
|
113
125
|
*
|
|
114
126
|
* @param {array} data Array of values to plot in the sparkline
|
|
115
|
-
* @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
|
|
116
128
|
* @param {object} options Optional options for display, e.g display min,max,last, range coercion
|
|
129
|
+
*
|
|
117
130
|
* @returns
|
|
118
131
|
*/
|
|
119
132
|
// eslint-disable-next-line no-unused-vars
|
|
120
133
|
function sparkline(data,label,options) {
|
|
121
|
-
// TODO add handling if data is object
|
|
122
|
-
// let open = last30days.map( x=> x.open_count)
|
|
123
134
|
|
|
135
|
+
options = {
|
|
136
|
+
coerceData: true,
|
|
137
|
+
...options
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// TODO add handling if data is object
|
|
124
141
|
// Assuming data is array
|
|
125
142
|
const minValue = Math.min(...data)
|
|
126
143
|
const maxValue = Math.max(...data)
|
|
127
144
|
const lastValue = data.slice(-1)[0]
|
|
128
145
|
|
|
129
|
-
//
|
|
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
|
+
|
|
154
|
+
|
|
155
|
+
let sparkline
|
|
156
|
+
if (label) {
|
|
157
|
+
sparkline = `${label} [${minValue},${maxValue}] ${sparkly(data)} ${lastValue}`
|
|
158
|
+
} else {
|
|
159
|
+
sparkline = sparkly(data)
|
|
160
|
+
}
|
|
161
|
+
return sparkline
|
|
133
162
|
}
|
|
134
163
|
|
|
135
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
|
|