]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
feat: sideEffects false (#10526)
authorDan Onoshko <danon0404@gmail.com>
Sat, 6 Aug 2022 12:45:41 +0000 (16:45 +0400)
committerGitHub <noreply@github.com>
Sat, 6 Aug 2022 12:45:41 +0000 (08:45 -0400)
* feat: sideEffects false
* refactor: apply defaults by pure way

package.json
src/core/core.animations.defaults.js [new file with mode: 0644]
src/core/core.animations.js
src/core/core.defaults.js
src/core/core.layouts.defaults.js [new file with mode: 0644]
src/core/core.layouts.js
src/core/core.scale.defaults.js
src/core/core.scale.js
src/scales/scale.time.js

index be63cae913c411068fc4acee13c7b496f3bdb010..fe209a27bb41ca8085b95023634c24fba081ed06 100644 (file)
@@ -5,6 +5,10 @@
     "version": "4.0.0-dev",
     "license": "MIT",
     "type": "module",
+    "sideEffects": [
+        "./auto/auto.js",
+        "./dist/chart.umd.js"
+    ],
     "jsdelivr": "dist/chart.umd.js",
     "unpkg": "dist/chart.umd.js",
     "main": "dist/chart.js",
diff --git a/src/core/core.animations.defaults.js b/src/core/core.animations.defaults.js
new file mode 100644 (file)
index 0000000..43501ab
--- /dev/null
@@ -0,0 +1,72 @@
+const numbers = ['x', 'y', 'borderWidth', 'radius', 'tension'];
+const colors = ['color', 'borderColor', 'backgroundColor'];
+
+export function applyAnimationsDefaults(defaults) {
+  defaults.set('animation', {
+    delay: undefined,
+    duration: 1000,
+    easing: 'easeOutQuart',
+    fn: undefined,
+    from: undefined,
+    loop: undefined,
+    to: undefined,
+    type: undefined,
+  });
+
+  defaults.describe('animation', {
+    _fallback: false,
+    _indexable: false,
+    _scriptable: (name) => name !== 'onProgress' && name !== 'onComplete' && name !== 'fn',
+  });
+
+  defaults.set('animations', {
+    colors: {
+      type: 'color',
+      properties: colors
+    },
+    numbers: {
+      type: 'number',
+      properties: numbers
+    },
+  });
+
+  defaults.describe('animations', {
+    _fallback: 'animation',
+  });
+
+  defaults.set('transitions', {
+    active: {
+      animation: {
+        duration: 400
+      }
+    },
+    resize: {
+      animation: {
+        duration: 0
+      }
+    },
+    show: {
+      animations: {
+        colors: {
+          from: 'transparent'
+        },
+        visible: {
+          type: 'boolean',
+          duration: 0 // show immediately
+        },
+      }
+    },
+    hide: {
+      animations: {
+        colors: {
+          to: 'transparent'
+        },
+        visible: {
+          type: 'boolean',
+          easing: 'linear',
+          fn: v => v | 0 // for keeping the dataset visible all the way through the animation
+        },
+      }
+    }
+  });
+}
index fc1cd54e0386d9aef18f9524fd73359d29b7670a..ad35720eefdb9a483b13b8f1183ef7bcbb0b7624 100644 (file)
@@ -3,79 +3,6 @@ import Animation from './core.animation';
 import defaults from './core.defaults';
 import {isArray, isObject} from '../helpers/helpers.core';
 
-const numbers = ['x', 'y', 'borderWidth', 'radius', 'tension'];
-const colors = ['color', 'borderColor', 'backgroundColor'];
-
-defaults.set('animation', {
-  delay: undefined,
-  duration: 1000,
-  easing: 'easeOutQuart',
-  fn: undefined,
-  from: undefined,
-  loop: undefined,
-  to: undefined,
-  type: undefined,
-});
-
-const animationOptions = Object.keys(defaults.animation);
-
-defaults.describe('animation', {
-  _fallback: false,
-  _indexable: false,
-  _scriptable: (name) => name !== 'onProgress' && name !== 'onComplete' && name !== 'fn',
-});
-
-defaults.set('animations', {
-  colors: {
-    type: 'color',
-    properties: colors
-  },
-  numbers: {
-    type: 'number',
-    properties: numbers
-  },
-});
-
-defaults.describe('animations', {
-  _fallback: 'animation',
-});
-
-defaults.set('transitions', {
-  active: {
-    animation: {
-      duration: 400
-    }
-  },
-  resize: {
-    animation: {
-      duration: 0
-    }
-  },
-  show: {
-    animations: {
-      colors: {
-        from: 'transparent'
-      },
-      visible: {
-        type: 'boolean',
-        duration: 0 // show immediately
-      },
-    }
-  },
-  hide: {
-    animations: {
-      colors: {
-        to: 'transparent'
-      },
-      visible: {
-        type: 'boolean',
-        easing: 'linear',
-        fn: v => v | 0 // for keeping the dataset visible all the way through the animation
-      },
-    }
-  }
-});
-
 export default class Animations {
   constructor(chart, config) {
     this._chart = chart;
@@ -88,6 +15,7 @@ export default class Animations {
       return;
     }
 
+    const animationOptions = Object.keys(defaults.animation);
     const animatedProps = this._properties;
 
     Object.getOwnPropertyNames(config).forEach(key => {
index 05de7e9ac0ae4d7b0bcb92321b79b515a74c0390..85d876048a8f1d519c7c56635bca8db77c0e27b1 100644 (file)
@@ -1,5 +1,8 @@
 import {getHoverColor} from '../helpers/helpers.color';
 import {isObject, merge, valueOrDefault} from '../helpers/helpers.core';
+import {applyAnimationsDefaults} from './core.animations.defaults';
+import {applyLayoutsDefaults} from './core.layouts.defaults';
+import {applyScaleDefaults} from './core.scale.defaults';
 
 export const overrides = Object.create(null);
 export const descriptors = Object.create(null);
@@ -33,7 +36,7 @@ function set(root, scope, values) {
  * Note: class is exported for typedoc
  */
 export class Defaults {
-  constructor(_descriptors) {
+  constructor(_descriptors, _appliers) {
     this.animation = undefined;
     this.backgroundColor = 'rgba(0,0,0,0.1)';
     this.borderColor = 'rgba(0,0,0,0.1)';
@@ -77,6 +80,7 @@ export class Defaults {
     this.drawActiveElementsOnTop = true;
 
     this.describe(_descriptors);
+    this.apply(_appliers);
   }
 
   /**
@@ -151,6 +155,10 @@ export class Defaults {
       }
     });
   }
+
+  apply(appliers) {
+    appliers.forEach((apply) => apply(this));
+  }
 }
 
 // singleton instance
@@ -164,4 +172,4 @@ export default /* #__PURE__ */ new Defaults({
     _scriptable: false,
     _indexable: false,
   }
-});
+}, [applyAnimationsDefaults, applyLayoutsDefaults, applyScaleDefaults]);
diff --git a/src/core/core.layouts.defaults.js b/src/core/core.layouts.defaults.js
new file mode 100644 (file)
index 0000000..cac2a7b
--- /dev/null
@@ -0,0 +1,11 @@
+export function applyLayoutsDefaults(defaults) {
+  defaults.set('layout', {
+    autoPadding: true,
+    padding: {
+      top: 0,
+      right: 0,
+      bottom: 0,
+      left: 0
+    }
+  });
+}
index 5154e1285f43875fffdc9daac719ff258e7421ca..d69ea06e86c7b7d876b9494009db074affe2c0a4 100644 (file)
@@ -1,4 +1,3 @@
-import defaults from './core.defaults';
 import {defined, each, isObject} from '../helpers/helpers.core';
 import {toPadding} from '../helpers/helpers.options';
 
@@ -259,16 +258,6 @@ function placeBoxes(boxes, chartArea, params, stacks) {
   chartArea.y = y;
 }
 
-defaults.set('layout', {
-  autoPadding: true,
-  padding: {
-    top: 0,
-    right: 0,
-    bottom: 0,
-    left: 0
-  }
-});
-
 /**
  * @interface LayoutItem
  * @typedef {object} LayoutItem
index c067b3e5cae28111e69242de0d3c40e9a9b04c68..d73ae84ef18ea9d688c9406877c2c9270c6d7faa 100644 (file)
-import defaults from './core.defaults';
 import Ticks from './core.ticks';
 
-defaults.set('scale', {
-  display: true,
-  offset: false,
-  reverse: false,
-  beginAtZero: false,
+export function applyScaleDefaults(defaults) {
+  defaults.set('scale', {
+    display: true,
+    offset: false,
+    reverse: false,
+    beginAtZero: false,
 
-  /**
-        * Scale boundary strategy (bypassed by min/max time options)
-        * - `data`: make sure data are fully visible, ticks outside are removed
-        * - `ticks`: make sure ticks are fully visible, data outside are truncated
-        * @see https://github.com/chartjs/Chart.js/pull/4556
-        * @since 3.0.0
-        */
-  bounds: 'ticks',
+    /**
+     * Scale boundary strategy (bypassed by min/max time options)
+     * - `data`: make sure data are fully visible, ticks outside are removed
+     * - `ticks`: make sure ticks are fully visible, data outside are truncated
+     * @see https://github.com/chartjs/Chart.js/pull/4556
+     * @since 3.0.0
+     */
+    bounds: 'ticks',
 
-  /**
-   * Addition grace added to max and reduced from min data value.
-   * @since 3.0.0
-   */
-  grace: 0,
+    /**
+     * Addition grace added to max and reduced from min data value.
+     * @since 3.0.0
+     */
+    grace: 0,
 
-  // grid line settings
-  grid: {
-    display: true,
-    lineWidth: 1,
-    drawBorder: true,
-    drawOnChartArea: true,
-    drawTicks: true,
-    tickLength: 8,
-    tickWidth: (_ctx, options) => options.lineWidth,
-    tickColor: (_ctx, options) => options.color,
-    offset: false,
-    borderDash: [],
-    borderDashOffset: 0.0,
-    borderWidth: 1
-  },
+    // grid line settings
+    grid: {
+      display: true,
+      lineWidth: 1,
+      drawBorder: true,
+      drawOnChartArea: true,
+      drawTicks: true,
+      tickLength: 8,
+      tickWidth: (_ctx, options) => options.lineWidth,
+      tickColor: (_ctx, options) => options.color,
+      offset: false,
+      borderDash: [],
+      borderDashOffset: 0.0,
+      borderWidth: 1
+    },
 
-  // scale title
-  title: {
-    // display property
-    display: false,
+    // scale title
+    title: {
+      // display property
+      display: false,
 
-    // actual label
-    text: '',
+      // actual label
+      text: '',
 
-    // top/bottom padding
-    padding: {
-      top: 4,
-      bottom: 4
-    }
-  },
+      // top/bottom padding
+      padding: {
+        top: 4,
+        bottom: 4
+      }
+    },
 
-  // label settings
-  ticks: {
-    minRotation: 0,
-    maxRotation: 50,
-    mirror: false,
-    textStrokeWidth: 0,
-    textStrokeColor: '',
-    padding: 3,
-    display: true,
-    autoSkip: true,
-    autoSkipPadding: 3,
-    labelOffset: 0,
-    // We pass through arrays to be rendered as multiline labels, we convert Others to strings here.
-    callback: Ticks.formatters.values,
-    minor: {},
-    major: {},
-    align: 'center',
-    crossAlign: 'near',
+    // label settings
+    ticks: {
+      minRotation: 0,
+      maxRotation: 50,
+      mirror: false,
+      textStrokeWidth: 0,
+      textStrokeColor: '',
+      padding: 3,
+      display: true,
+      autoSkip: true,
+      autoSkipPadding: 3,
+      labelOffset: 0,
+      // We pass through arrays to be rendered as multiline labels, we convert Others to strings here.
+      callback: Ticks.formatters.values,
+      minor: {},
+      major: {},
+      align: 'center',
+      crossAlign: 'near',
 
-    showLabelBackdrop: false,
-    backdropColor: 'rgba(255, 255, 255, 0.75)',
-    backdropPadding: 2,
-  }
-});
+      showLabelBackdrop: false,
+      backdropColor: 'rgba(255, 255, 255, 0.75)',
+      backdropPadding: 2,
+    }
+  });
 
-defaults.route('scale.ticks', 'color', '', 'color');
-defaults.route('scale.grid', 'color', '', 'borderColor');
-defaults.route('scale.grid', 'borderColor', '', 'borderColor');
-defaults.route('scale.title', 'color', '', 'color');
+  defaults.route('scale.ticks', 'color', '', 'color');
+  defaults.route('scale.grid', 'color', '', 'borderColor');
+  defaults.route('scale.grid', 'borderColor', '', 'borderColor');
+  defaults.route('scale.title', 'color', '', 'color');
 
-defaults.describe('scale', {
-  _fallback: false,
-  _scriptable: (name) => !name.startsWith('before') && !name.startsWith('after') && name !== 'callback' && name !== 'parser',
-  _indexable: (name) => name !== 'borderDash' && name !== 'tickBorderDash',
-});
+  defaults.describe('scale', {
+    _fallback: false,
+    _scriptable: (name) => !name.startsWith('before') && !name.startsWith('after') && name !== 'callback' && name !== 'parser',
+    _indexable: (name) => name !== 'borderDash' && name !== 'tickBorderDash',
+  });
 
-defaults.describe('scales', {
-  _fallback: 'scale',
-});
+  defaults.describe('scales', {
+    _fallback: 'scale',
+  });
 
-defaults.describe('scale.ticks', {
-  _scriptable: (name) => name !== 'backdropPadding' && name !== 'callback',
-  _indexable: (name) => name !== 'backdropPadding',
-});
+  defaults.describe('scale.ticks', {
+    _scriptable: (name) => name !== 'backdropPadding' && name !== 'callback',
+    _indexable: (name) => name !== 'backdropPadding',
+  });
+}
index dcc2bc75fcff0c2c63965d20f32685ef785f4b7b..5d20d8216a6f9f5c97386064330197318b2ff8a5 100644 (file)
@@ -4,13 +4,8 @@ import {callback as call, each, finiteOrDefault, isArray, isFinite, isNullOrUnde
 import {toDegrees, toRadians, _int16Range, _limitValue, HALF_PI} from '../helpers/helpers.math';
 import {_alignStartEnd, _toLeftRightCenter} from '../helpers/helpers.extras';
 import {createContext, toFont, toPadding, _addGrace} from '../helpers/helpers.options';
-
-import './core.scale.defaults';
-
-
 import {autoSkip} from './core.scale.autoskip';
 
-
 const reverseAlign = (align) => align === 'left' ? 'right' : align === 'right' ? 'left' : align;
 const offsetFromEdge = (scale, edge, offset) => edge === 'top' || edge === 'left' ? scale[edge] + offset : scale[edge] - offset;
 
index a3940472abdba64924efd4670ce4306d50ae6b90..cbd230d0d081495c4b6f122055ef015dce817eef 100644 (file)
@@ -27,7 +27,7 @@ const INTERVALS = {
 /**
  * @type {Unit[]}
  */
-const UNITS = /** @type Unit[] */(Object.keys(INTERVALS));
+const UNITS = /** @type Unit[] */ /* #__PURE__ */ (Object.keys(INTERVALS));
 
 /**
  * @param {number} a