Skip to content

Commit b6057ea

Browse files
committed
Internal lib - Update lib with sanitized processes
1 parent 063accc commit b6057ea

File tree

2 files changed

+83
-25
lines changed

2 files changed

+83
-25
lines changed

src/lib.js

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,20 @@ export function makeDonut(item, cx, cy, rx, ry, piProportion = 1.99999, piMult =
4242
degrees,
4343
piMult,
4444
true
45-
)
45+
);
4646

4747
ratios.push({
4848
arcSlice: `${path} L ${inner.startX} ${inner.startY} ${inner.path} L ${startX} ${startY}`,
49-
cx,
50-
cy,
49+
cx: checkNaN(cx),
50+
cy: checkNaN(cy),
5151
...series[i],
52-
proportion,
53-
ratio: ratio,
54-
path,
55-
startX,
56-
startY,
57-
endX,
58-
endY,
52+
proportion: checkNaN(proportion),
53+
ratio: checkNaN(ratio),
54+
path: path.replaceAll('NaN', '0'),
55+
startX: checkNaN(startX),
56+
startY: checkNaN(startY),
57+
endX: checkNaN(endX),
58+
endY: checkNaN(endY),
5959
center: createArc(
6060
[cx, cy],
6161
[rx * arcAmpl, ry * arcAmpl],
@@ -105,18 +105,18 @@ export function createArc([cx, cy], [rx, ry], [position, ratio], phi, degrees =
105105
const fA = ratio > Math.PI ? 1 : 0;
106106
const fS = ratio > 0 ? reverse ? 0 : 1 : reverse ? 1 : 0;
107107
return {
108-
startX: reverse ? eX : sX,
109-
startY: reverse ? eY : sY,
110-
endX: reverse ? sX : eX,
111-
endY: reverse ? sY : eY,
112-
path: `M${reverse ? eX : sX} ${reverse ? eY : sY} A ${[
113-
rx,
114-
ry,
115-
(phi / (piMult * Math.PI)) * degrees,
116-
fA,
117-
fS,
118-
reverse ? sX : eX,
119-
reverse ? sY : eY,
108+
startX: reverse ? checkNaN(eX) : checkNaN(sX),
109+
startY: reverse ? checkNaN(eY) : checkNaN(sY),
110+
endX: reverse ? checkNaN(sX) : checkNaN(eX),
111+
endY: reverse ? checkNaN(sY) : checkNaN(eY),
112+
path: `M${reverse ? checkNaN(eX) : checkNaN(sX)} ${reverse ? checkNaN(eY) : checkNaN(sY)} A ${[
113+
checkNaN(rx),
114+
checkNaN(ry),
115+
checkNaN((phi / (piMult * Math.PI)) * degrees),
116+
checkNaN(fA),
117+
checkNaN(fS),
118+
reverse ? checkNaN(sX) : checkNaN(eX),
119+
reverse ? checkNaN(sY) : checkNaN(eY),
120120
].join(" ")}`,
121121
};
122122
}
@@ -859,9 +859,11 @@ export function sumByAttribute(arr, attr) {
859859
}
860860

861861
export function makePath(plots, closed = true, bare = false) {
862+
if (!plots.length) return "M0,0";
862863
let path = "";
863864
plots.forEach(plot => {
864-
path += `${plot.x},${plot.y} `
865+
if (!plot) return '';
866+
path += `${plot.x},${plot.y} `;
865867
})
866868
if (bare) {
867869
return path.trim();
@@ -1726,7 +1728,7 @@ export function sumSeries(source) {
17261728
export function checkFormatter(func, { value, config }) {
17271729
let isValid = false;
17281730
let formattedValue = value;
1729-
1731+
17301732
if (typeof func === 'function') {
17311733
try {
17321734
// Ensure that the function is called with an object containing `value` and `config`
@@ -1764,6 +1766,37 @@ export function hasDeepProperty(obj, path) {
17641766
});
17651767
}
17661768

1769+
export function sanitizeArray(arr, keys = []) {
1770+
1771+
function sanitizeValue(value) {
1772+
if([NaN, undefined, Infinity, -Infinity, null].includes(value)) {
1773+
console.warn(`A non processable value was detected : ${value}`)
1774+
}
1775+
return (typeof value === 'number' && isFinite(value)) ? value : 0;
1776+
}
1777+
1778+
function sanitize(data) {
1779+
if (Array.isArray(data)) {
1780+
return data.map(item => sanitize(item));
1781+
} else if (typeof data === 'object' && data !== null) {
1782+
1783+
let sanitizedObject = { ...data };
1784+
keys.forEach(key => {
1785+
if (sanitizedObject.hasOwnProperty(key) && Array.isArray(sanitizedObject[key])) {
1786+
sanitizedObject[key] = sanitize(sanitizedObject[key]);
1787+
}
1788+
});
1789+
return Object.fromEntries(
1790+
Object.entries(sanitizedObject).map(([k, v]) => [k, sanitize(v)])
1791+
);
1792+
} else {
1793+
return sanitizeValue(data);
1794+
}
1795+
}
1796+
1797+
return sanitize(arr);
1798+
}
1799+
17671800
const lib = {
17681801
XMLNS,
17691802
abbreviate,
@@ -1821,6 +1854,7 @@ const lib = {
18211854
opacity,
18221855
palette,
18231856
rotateMatrix,
1857+
sanitizeArray,
18241858
shiftHue,
18251859
sumByAttribute,
18261860
sumSeries,

tests/lib.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
niceNum,
4747
objectIsEmpty,
4848
rotateMatrix,
49+
sanitizeArray,
4950
shiftHue,
5051
sumByAttribute,
5152
translateSize,
@@ -691,6 +692,9 @@ describe('makePath', () => {
691692
test('creates svg points for a polygon', () => {
692693
expect(makePath(plots, false, true)).toBe('1,2 2,3 3,4 4,5 5,6')
693694
})
695+
test('returns an empty path when plots are empty', () => {
696+
expect(makePath([], false)).toBe('M0,0')
697+
})
694698
})
695699

696700
describe('calculateNiceScale', () => {
@@ -1865,4 +1869,24 @@ describe('hasDeepProperty', () => {
18651869
expect(hasDeepProperty(obj, 'attr0.attr1.attr2.attr3')).toBe(true)
18661870
expect(hasDeepProperty(obj, 'attr0.attr1.attr2.attr3.attr4')).toBe(false)
18671871
})
1868-
})
1872+
});
1873+
1874+
describe('sanitizeArray', () => {
1875+
const source0 = [1, 2, 3, NaN, undefined, Infinity, -Infinity];
1876+
test('sanitizes an array of numbers', () => {
1877+
expect(sanitizeArray(source0)).toStrictEqual([1, 2, 3, 0, 0, 0, 0])
1878+
});
1879+
1880+
const source1 = [{
1881+
values: [1, NaN, undefined, Infinity, -Infinity],
1882+
value: [2, NaN, undefined, Infinity, -Infinity]
1883+
}];
1884+
test('sanitizes an array of objects where some attributes are arrays of numbers', () => {
1885+
expect(sanitizeArray(source1, ['values, value'])).toStrictEqual([
1886+
{
1887+
value: [2, 0, 0, 0, 0],
1888+
values: [1, 0, 0, 0, 0]
1889+
}
1890+
]);
1891+
});
1892+
});

0 commit comments

Comments
 (0)