@evidence-dev/component-utilities
Advanced tools
Comparing version 0.0.0-4872e0f5 to 0.0.0-48c0d2a8
@@ -1,6 +0,4 @@ | ||
# License | ||
MIT License | ||
Copyright \(c\) 2021 Evidence | ||
Copyright \(c\) 2023 Evidence | ||
@@ -7,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files \(the "Software"\), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
{ | ||
"name": "@evidence-dev/component-utilities", | ||
"version": "0.0.0-4872e0f5", | ||
"version": "0.0.0-48c0d2a8", | ||
"description": "", | ||
@@ -13,2 +13,4 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@faker-js/faker": "^8.0.2", | ||
"@vitest/ui": "^0.34.2", | ||
"vitest": "^0.31.1" | ||
@@ -15,0 +17,0 @@ }, |
@@ -1,132 +0,80 @@ | ||
import { tidy, complete } from '@tidyjs/tidy'; | ||
import { tidy, complete, mutate } from '@tidyjs/tidy'; | ||
import getDistinctValues from './getDistinctValues'; | ||
import { findInterval, vectorSeq } from './helpers/getCompletedData.helpers.js'; | ||
function getDiffs(arr) { | ||
var diffs = []; | ||
for (var i = 1; i < arr.length; i++) diffs.push(arr[i] - arr[i - 1]); | ||
return diffs; | ||
} | ||
/** | ||
* This function fills missing data points in the given data array for a specific series. | ||
* | ||
* @param {Record<string, unknown>[]} data - The data as an array of objects. | ||
* @param {string} x - The property used as x-axis. | ||
* @param {string} y - The property used as y-axis. | ||
* @param {string} series - The specific series in the data to be filled. | ||
* @param {boolean} [nullsZero=false] - A flag indicating whether nulls should be replaced with zero. | ||
* @param {boolean} [fillX=false] - A flag indicating whether the x-axis values should be filled (based on the found interval distance). | ||
* @return {Record<string, unknown>[]} An array containing the filled data objects. | ||
*/ | ||
export default function getCompletedData(data, x, y, series, nullsZero = false, fillX = false) { | ||
const groups = Array.from(data).reduce((a, v) => { | ||
if (series) { | ||
if (!a[v[series]]) a[v[series]] = []; | ||
a[v[series]].push(v); | ||
} else { | ||
if (!a.default) a.default = []; | ||
a.default.push(v); | ||
} | ||
return a; | ||
}, {}); | ||
function gcd(a, b) { | ||
// Get greatest common divisor of the differences between values | ||
if (a < b) return gcd(b, a); | ||
const output = []; | ||
for (const value of Object.values(groups)) { | ||
let xIsDate = value[0]?.[x] instanceof Date; | ||
// base case | ||
if (Math.abs(b) < 0.001) return a; | ||
else return gcd(b, a - Math.floor(a / b) * b); | ||
} | ||
function extent(values, valueof) { | ||
let min; | ||
let max; | ||
if (valueof === undefined) { | ||
for (const value of values) { | ||
if (value != null) { | ||
if (min === undefined) { | ||
if (value >= value) min = max = value; | ||
} else { | ||
if (min > value) min = value; | ||
if (max < value) max = value; | ||
} | ||
} | ||
const nullySpec = { series: null }; | ||
if (nullsZero) { | ||
nullySpec[y] = 0; | ||
} else { | ||
// Ensure null for consistency | ||
nullySpec[y] = null; | ||
} | ||
} else { | ||
let index = -1; | ||
for (let value of values) { | ||
if ((value = valueof(value, ++index, values)) != null) { | ||
if (min === undefined) { | ||
if (value >= value) min = max = value; | ||
} else { | ||
if (min > value) min = value; | ||
if (max < value) max = value; | ||
} | ||
} | ||
} | ||
} | ||
return [min, max]; | ||
} | ||
function vectorSeq(values, period) { | ||
let min = extent(values)[0]; | ||
let max = extent(values)[1]; | ||
const expandKeys = {}; | ||
if (fillX) { | ||
/** @type {Array<number>} */ | ||
let xDistinct; | ||
const sequence = []; | ||
let value = min; | ||
while (value <= max) { | ||
sequence.push(Math.round((value + Number.EPSILON) * 100000000) / 100000000); | ||
value += period; | ||
} | ||
if (xIsDate) | ||
xDistinct = getDistinctValues( | ||
value.map((d) => ({ [x]: d[x].getTime() })), | ||
x | ||
); | ||
else xDistinct = getDistinctValues(value, x); | ||
return sequence; | ||
} | ||
/** @type {number} */ | ||
let interval = findInterval(xDistinct); | ||
function findInterval(arr) { | ||
if (arr.length === 1) { | ||
return; | ||
} | ||
// Sort array ascending | ||
arr.sort(function (a, b) { | ||
return a - b; | ||
}); | ||
// Array of all possible x values | ||
expandKeys[x] = vectorSeq(xDistinct, interval); | ||
} | ||
if (series) { | ||
expandKeys[series] = series; | ||
} | ||
// 1. Multiply array by 100 | ||
arr = arr.map(function (x) { | ||
return x * 100000000; | ||
}); | ||
const tidyFuncs = []; | ||
// 2. Get diffs | ||
arr = getDiffs(arr); | ||
// 3. Calculate greatest common divisor of diffs and divide by 100 | ||
let interval = arr.reduce(gcd) / 100000000; | ||
interval = Math.round((interval + Number.EPSILON) * 100000000) / 100000000; | ||
return interval; | ||
} | ||
export default function getCompletedData(data, x, y, series, nullsZero = false, fillX = false) { | ||
let xDistinct = getDistinctValues(data, x); | ||
let interval; | ||
let filledData; | ||
if (series) { | ||
if (fillX) { | ||
interval = findInterval(xDistinct); | ||
if (nullsZero) { | ||
filledData = tidy( | ||
data, | ||
complete({ [x]: vectorSeq(xDistinct, interval), [series]: series }, { [y]: 0 }) | ||
); | ||
} else { | ||
filledData = tidy( | ||
data, | ||
complete({ [x]: vectorSeq(xDistinct, interval), [series]: series }) | ||
); | ||
} | ||
if (Object.keys(expandKeys).length === 0) { | ||
tidyFuncs.push(complete([x], nullySpec)); | ||
// empty object, no special configuration | ||
} else { | ||
filledData = tidy( | ||
data, | ||
complete( | ||
[x, series], | ||
// Nully values in the x and series columns to be treated as nulls | ||
{ [series]: null, [x]: null } | ||
) | ||
tidyFuncs.push(complete(expandKeys, nullySpec)); | ||
} | ||
if (xIsDate) { | ||
tidyFuncs.push( | ||
mutate({ | ||
[x]: (val) => new Date(val[x]) | ||
}) | ||
); | ||
} | ||
} else { | ||
if (fillX) { | ||
interval = findInterval(xDistinct); | ||
if (nullsZero) { | ||
filledData = tidy(data, complete({ [x]: vectorSeq(xDistinct, interval) }, { [y]: 0 })); | ||
} else { | ||
filledData = tidy(data, complete({ [x]: vectorSeq(xDistinct, interval) })); | ||
} | ||
} else { | ||
filledData = tidy(data, complete([x])); | ||
} | ||
output.push(...tidy(value, ...tidyFuncs)); | ||
} | ||
return filledData; | ||
return output; | ||
} |
export default function getDistinctValues(data, column) { | ||
let distinctValues = []; | ||
const distinctValueSet = new Set(); | ||
data.forEach((d) => { | ||
distinctValueSet.add(d[column]); | ||
}); | ||
distinctValues = [...distinctValueSet]; | ||
return distinctValues; | ||
const set = new Set(data.map((val) => val[column])); | ||
return Array.from(set); | ||
} |
194824
37
6980
3
92