Ver código fonte

feat: clone options before building

pull/347/head
Shivam Mishra 4 anos atrás
committed by GitHub
pai
commit
7e13f81063
2 arquivos alterados com 37 adições e 9 exclusões
  1. +2
    -0
      src/js/charts/BaseChart.js
  2. +35
    -9
      src/js/utils/helpers.js

+ 2
- 0
src/js/charts/BaseChart.js Ver arquivo

@@ -9,9 +9,11 @@ import {
import { getColor, isValidColor } from '../utils/colors'; import { getColor, isValidColor } from '../utils/colors';
import { runSMILAnimation } from '../utils/animation'; import { runSMILAnimation } from '../utils/animation';
import { downloadFile, prepareForExport } from '../utils/export'; import { downloadFile, prepareForExport } from '../utils/export';
import { deepClone } from '../utils/helpers';


export default class BaseChart { export default class BaseChart {
constructor(parent, options) { constructor(parent, options) {
options = deepClone(options)


this.parent = typeof parent === 'string' this.parent = typeof parent === 'string'
? document.querySelector(parent) ? document.querySelector(parent)


+ 35
- 9
src/js/utils/helpers.js Ver arquivo

@@ -14,10 +14,10 @@ export function floatTwo(d) {
* @param {Array} arr2 Second array * @param {Array} arr2 Second array
*/ */
export function arraysEqual(arr1, arr2) { export function arraysEqual(arr1, arr2) {
if (arr1.length !== arr2.length) return false;
if(arr1.length !== arr2.length) return false;
let areEqual = true; let areEqual = true;
arr1.map((d, i) => { arr1.map((d, i) => {
if (arr2[i] !== d) areEqual = false;
if(arr2[i] !== d) areEqual = false;
}); });
return areEqual; return areEqual;
} }
@@ -46,8 +46,8 @@ export function shuffle(array) {
* @param {Object} element element to fill with * @param {Object} element element to fill with
* @param {Boolean} start fill at start? * @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]; element = start ? array[0] : array[array.length - 1];
} }
let fillerArray = new Array(Math.abs(count)).fill(element); 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 * @param {Number} charWidth Width of single char in pixels
*/ */
export function getStringWidth(string, charWidth) { export function getStringWidth(string, charWidth) {
return (string + "").length * charWidth;
return (string+"").length * charWidth;
} }


export function bindChange(obj, getFn, setFn) { export function bindChange(obj, getFn, setFn) {
return new Proxy(obj, { return new Proxy(obj, {
set: function (target, prop, value) {
set: function(target, prop, value) {
setFn(); setFn();
return Reflect.set(target, prop, value); return Reflect.set(target, prop, value);
}, },
get: function (target, prop) {
get: function(target, prop) {
getFn(); getFn();
return Reflect.get(target, prop); return Reflect.get(target, prop);
} }
@@ -98,7 +98,7 @@ export function getPositionByAngle(angle, radius) {
* @param {object} candidate Candidate to test * @param {object} candidate Candidate to test
* @param {Boolean} nonNegative flag to treat negative number as invalid * @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; if (Number.isNaN(candidate)) return false;
else if (candidate === undefined) return false; else if (candidate === undefined) return false;
else if (!Number.isFinite(candidate)) return false; else if (!Number.isFinite(candidate)) return false;
@@ -114,4 +114,30 @@ export function round(d) {
// https://floating-point-gui.de/ // https://floating-point-gui.de/
// https://www.jacklmoore.com/notes/rounding-in-javascript/ // https://www.jacklmoore.com/notes/rounding-in-javascript/
return Number(Math.round(d + 'e4') + 'e-4'); return Number(Math.round(d + 'e4') + 'e-4');
}
}

/**
* 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;
}

Carregando…
Cancelar
Salvar