Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/methods/analyzing/summarize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function summarize(
verbose = false,
nbDigits?: number
): SimpleDataItem[] {
let noKeyValue = false
if (keyValue === undefined) {
verbose && log(`No keyValue provided. Will summarize over all keys.`)
keyValue = Object.keys(data[0]).filter((d) => {
Expand All @@ -39,6 +40,7 @@ export default function summarize(
return false
}
})
noKeyValue = true
}

// Let's deal with the keyCategory first
Expand Down Expand Up @@ -116,7 +118,8 @@ export default function summarize(

for (const value of keyValues) {
if (
summaries.includes("sum") ||
(!noKeyValue && // if noKeyValue is true, we already filtered out non numerical values
summaries.includes("sum")) ||
summaries.includes("mean") ||
summaries.includes("median") ||
summaries.includes("deviation") ||
Expand Down
41 changes: 41 additions & 0 deletions test/unit/methods/analyzing/summarize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,47 @@ describe("summarize", function () {
])
})

it("should summarize and filter out keys with non numerical values", function () {
const data = [
{ key1: 1, key2: 2, key3: "a" },
{ key1: 2, key2: 22, key3: "b" },
{ key1: 2, key2: 22, key3: "c" },
{ key1: 2, key2: 22, key3: "d" },
]
const summarizedData = summarize(
data,
undefined,
undefined,
undefined,
undefined,
100,
false,
1
)
assert.deepEqual(summarizedData, [
{
value: "key1",
count: 4,
min: 1,
max: 2,
sum: 7,
mean: 1.8,
median: 2,
deviation: 0.5,
},
{
value: "key2",
count: 4,
min: 2,
max: 22,
sum: 68,
mean: 17,
median: 22,
deviation: 10,
},
])
})

it("should summarize with keyCategory", function () {
const data = [
{ key1: "Rubarbe", key2: 1 },
Expand Down