From 7e13f810637e2fdcd0d913563e8d89cc6e93bc52 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Thu, 18 Feb 2021 10:54:33 +0000 Subject: [PATCH] feat: clone options before building --- src/js/charts/BaseChart.js | 2 ++ src/js/utils/helpers.js | 44 ++++++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/js/charts/BaseChart.js b/src/js/charts/BaseChart.js index 91b9d6e..5482067 100644 --- a/src/js/charts/BaseChart.js +++ b/src/js/charts/BaseChart.js @@ -9,9 +9,11 @@ import { import { getColor, isValidColor } from '../utils/colors'; import { runSMILAnimation } from '../utils/animation'; import { downloadFile, prepareForExport } from '../utils/export'; +import { deepClone } from '../utils/helpers'; export default class BaseChart { constructor(parent, options) { + options = deepClone(options) this.parent = typeof parent === 'string' ? document.querySelector(parent) diff --git a/src/js/utils/helpers.js b/src/js/utils/helpers.js index 6a9c84a..d1493f7 100644 --- a/src/js/utils/helpers.js +++ b/src/js/utils/helpers.js @@ -14,10 +14,10 @@ export function floatTwo(d) { * @param {Array} arr2 Second array */ export function arraysEqual(arr1, arr2) { - if (arr1.length !== arr2.length) return false; + if(arr1.length !== arr2.length) return false; let areEqual = true; arr1.map((d, i) => { - if (arr2[i] !== d) areEqual = false; + if(arr2[i] !== d) areEqual = false; }); return areEqual; } @@ -46,8 +46,8 @@ export function shuffle(array) { * @param {Object} element element to fill with * @param {Boolean} start fill at start? */ -export function fillArray(array, count, element, start = false) { - if (!element) { +export function fillArray(array, count, element, start=false) { + if(!element) { element = start ? array[0] : array[array.length - 1]; } let fillerArray = new Array(Math.abs(count)).fill(element); @@ -61,16 +61,16 @@ export function fillArray(array, count, element, start = false) { * @param {Number} charWidth Width of single char in pixels */ export function getStringWidth(string, charWidth) { - return (string + "").length * charWidth; + return (string+"").length * charWidth; } export function bindChange(obj, getFn, setFn) { return new Proxy(obj, { - set: function (target, prop, value) { + set: function(target, prop, value) { setFn(); return Reflect.set(target, prop, value); }, - get: function (target, prop) { + get: function(target, prop) { getFn(); return Reflect.get(target, prop); } @@ -98,7 +98,7 @@ export function getPositionByAngle(angle, radius) { * @param {object} candidate Candidate to test * @param {Boolean} nonNegative flag to treat negative number as invalid */ -export function isValidNumber(candidate, nonNegative = false) { +export function isValidNumber(candidate, nonNegative=false) { if (Number.isNaN(candidate)) return false; else if (candidate === undefined) return false; else if (!Number.isFinite(candidate)) return false; @@ -114,4 +114,30 @@ export function round(d) { // https://floating-point-gui.de/ // https://www.jacklmoore.com/notes/rounding-in-javascript/ return Number(Math.round(d + 'e4') + 'e-4'); -} \ No newline at end of file +} + +/** + * Creates a deep clone of an object + * @param {Object} candidate Any Object + */ + export function deepClone(candidate) { + let cloned, value, key; + + if (candidate instanceof Date) { + return new Date(candidate.getTime()); + } + + if (typeof candidate !== "object" || candidate === null) { + return candidate; + } + + cloned = Array.isArray(candidate) ? [] : {}; + + for (key in candidate) { + value = candidate[key]; + + cloned[key] = deepClone(value); + } + + return cloned; + } \ No newline at end of file