markdown-up 1.8.1 → 1.8.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/lib/data.js +35 -0
- package/lib/scriptLibrary.js +12 -1
- package/package.json +1 -1
package/lib/data.js
CHANGED
|
@@ -447,6 +447,41 @@ export function sortData(data, sorts) {
|
|
|
447
447
|
}
|
|
448
448
|
|
|
449
449
|
|
|
450
|
+
/**
|
|
451
|
+
* Top data rows
|
|
452
|
+
*
|
|
453
|
+
* @param {Object[]} data - The data array
|
|
454
|
+
* @param {number} count - The number of rows to keep
|
|
455
|
+
* @param {?string[]} [categoryFields = null] - The category fields
|
|
456
|
+
* @returns {Object[]} The top data array
|
|
457
|
+
*/
|
|
458
|
+
export function topData(data, count, categoryFields = null) {
|
|
459
|
+
// Bucket rows by category
|
|
460
|
+
const categoryRows = {};
|
|
461
|
+
const categoryOrder = [];
|
|
462
|
+
for (const row of data) {
|
|
463
|
+
const categoryKey = categoryFields === null ? ''
|
|
464
|
+
: JSON.stringify(categoryFields.map((field) => (field in row ? row[field] : null)));
|
|
465
|
+
if (!(categoryKey in categoryRows)) {
|
|
466
|
+
categoryRows[categoryKey] = [];
|
|
467
|
+
categoryOrder.push(categoryKey);
|
|
468
|
+
}
|
|
469
|
+
categoryRows[categoryKey].push(row);
|
|
470
|
+
}
|
|
471
|
+
// Take only the top rows
|
|
472
|
+
const dataTop = [];
|
|
473
|
+
const topCount = count;
|
|
474
|
+
for (const categoryKey of categoryOrder) {
|
|
475
|
+
const categoryKeyRows = categoryRows[categoryKey];
|
|
476
|
+
const categoryKeyLength = categoryKeyRows.length;
|
|
477
|
+
for (let ixRow = 0; ixRow < topCount && ixRow < categoryKeyLength; ixRow++) {
|
|
478
|
+
dataTop.push(categoryKeyRows[ixRow]);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
return dataTop;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
|
|
450
485
|
// Helper function to format labels
|
|
451
486
|
export function formatValue(value, precision = null, datetime = null) {
|
|
452
487
|
if (value instanceof Date) {
|
package/lib/scriptLibrary.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
// Licensed under the MIT License
|
|
2
2
|
// https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
addCalculatedField, aggregateData, filterData, joinData, parseCSV, sortData, topData, validateAggregation, validateData
|
|
6
|
+
} from './data.js';
|
|
5
7
|
import {dataTableElements, validateDataTable} from './dataTable.js';
|
|
6
8
|
import {escapeMarkdownText, markdownHeaderId} from 'markdown-model/lib/elements.js';
|
|
7
9
|
import {getMarkdownTitle, parseMarkdown} from 'markdown-model/lib/parser.js';
|
|
@@ -121,6 +123,15 @@ export const markdownScriptFunctions = {
|
|
|
121
123
|
runtime.addElements(dataTableElements(data, dataTableValidated, options));
|
|
122
124
|
},
|
|
123
125
|
|
|
126
|
+
// $function: dataTop
|
|
127
|
+
// $group: Data
|
|
128
|
+
// $doc: Keep the top rows for each category
|
|
129
|
+
// $arg data: The data array
|
|
130
|
+
// $arg count: The number of rows to keep
|
|
131
|
+
// $arg categoryFields: Optional (default is null). The category fields.
|
|
132
|
+
// $return: The top data array
|
|
133
|
+
'dataTop': ([data, count, categoryFields = null]) => topData(data, count, categoryFields),
|
|
134
|
+
|
|
124
135
|
// $function: dataValidate
|
|
125
136
|
// $group: Data
|
|
126
137
|
// $doc: Validate a data array
|