Skip to content

Commit 391e4b6

Browse files
authored
flat helpers (chartjs#7806)
* generate a flat helpers package * move helpers built files to dist * upgrade docs for flat helpers
1 parent 10f393a commit 391e4b6

20 files changed

+70
-95
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/custom
44
/dist
55
/gh-pages
6-
/helpers
76

87
# Node.js
98
node_modules/

docs/docs/developers/publishing.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@ But not the helpers.
2020

2121
In UMD, helpers are available through `Chart.helpers`. In ESM, they are imported from `chart.js/helpers`.
2222

23-
There are multiple namespaces under helpers. Some of the namespaces are bundled directly under `Chart.helpers` for backward compatibility, those are: `core`, `color` and `extras`.
24-
25-
For example `import {isNullOrUndef} from 'chart.js/helpers/core'` is available at `Chart.helpers.isNullOrUndef` for UMD.
23+
For example `import {isNullOrUndef} from 'chart.js/helpers'` is available at `Chart.helpers.isNullOrUndef` for UMD.
2624

2725
### Rollup
2826

2927
`output.globals` can be used to convert the helpers.
3028

31-
For convinience, a plugin is available for the configuration: [rollup-plugin-chartjs-globals](https://www.npmjs.com/package/rollup-plugin-chartjs-globals).
29+
```js
30+
{
31+
output: {
32+
globals: {
33+
'chart.js': 'Chart',
34+
'chart.js/helpers': 'Chart.helpers'
35+
}
36+
}
37+
}
38+
```

docs/docs/getting-started/v3-migration.md

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,9 @@ The following properties and methods were removed:
306306
* `helpers.getValueAtIndexOrDefault`. Use `helpers.resolve` instead.
307307
* `helpers.indexOf`
308308
* `helpers.lineTo`
309-
* `helpers.longestText` was moved to the `helpers.canvas` namespace and made private
309+
* `helpers.longestText` was made private
310310
* `helpers.max`
311-
* `helpers.measureText` was moved to the `helpers.canvas` namespace and made private
311+
* `helpers.measureText` was made private
312312
* `helpers.min`
313313
* `helpers.nextItem`
314314
* `helpers.niceNum`
@@ -369,29 +369,12 @@ The following properties were renamed during v3 development:
369369

370370
* `Chart.Animation.animationObject` was renamed to `Chart.Animation`
371371
* `Chart.Animation.chartInstance` was renamed to `Chart.Animation.chart`
372-
* `Chart.canvasHelpers` was renamed to `Chart.helpers.canvas`
372+
* `Chart.canvasHelpers` was merged with `Chart.helpers`
373373
* `Chart.layoutService` was renamed to `Chart.layouts`
374374
* `Chart.pluginService` was renamed to `Chart.plugins`
375-
* `helpers._decimalPlaces` was renamed to `helpers.math._decimalPlaces`
376-
* `helpers.almostEquals` was renamed to `helpers.math.almostEquals`
377-
* `helpers.almostWhole` was renamed to `helpers.math.almostWhole`
378375
* `helpers.callCallback` was renamed to `helpers.callback`
379-
* `helpers.clear` was renamed to `helpers.canvas.clear`
380-
* `helpers.distanceBetweenPoints` was renamed to `helpers.math.distanceBetweenPoints`
381-
* `helpers.drawRoundedRectangle` was renamed to `helpers.canvas.roundedRect`
382-
* `helpers.getAngleFromPoint` was renamed to `helpers.math.getAngleFromPoint`
383-
* `helpers.getRelativePosition` was renamed to `helpers.dom.getRelativePosition`
384-
* `helpers.getStyle` was renamed to `helpers.dom.getStyle`
376+
* `helpers.drawRoundedRectangle` was renamed to `helpers.roundedRect`
385377
* `helpers.getValueOrDefault` was renamed to `helpers.valueOrDefault`
386-
* `helpers.easingEffects` was renamed to `helpers.easing.effects`
387-
* `helpers.log10` was renamed to `helpers.math.log10`
388-
* `helpers.isNumber` was renamed to `helpers.math.isNumber`
389-
* `helpers.sign` was renamed to `helpers.math.sign`
390-
* `helpers.retinaScale` was renamed to `helpers.dom.retinaScale`
391-
* `helpers.splineCurve` was renamed to `helpers.curve.splineCurve`
392-
* `helpers.splineCurveMonotone` was renamed to `helpers.curve.splineCurveMonotone`
393-
* `helpers.toDegrees` was renamed to `helpers.math.toDegrees`
394-
* `helpers.toRadians` was renamed to `helpers.math.toRadians`
395378
* `Scale.calculateTickRotation` was renamed to `Scale.calculateLabelRotation`
396379
* `Tooltip.options.legendColorBackgroupd` was renamed to `Tooltip.options.multiKeyBackground`
397380

@@ -426,8 +409,6 @@ The private APIs listed below were renamed:
426409
* `DatasetController.resyncElements` was renamed to `DatasetController._resyncElements`
427410
* `RadialLinearScale.setReductions` was renamed to `RadialLinearScale._setReductions`
428411
* `Scale.handleMargins` was renamed to `Scale._handleMargins`
429-
* `helpers._alignPixel` was renamed to `helpers.canvas._alignPixel`
430-
* `helpers._decimalPlaces` was renamed to `helpers.math._decimalPlaces`
431412

432413
### Changed
433414

@@ -472,6 +453,8 @@ The APIs listed in this section have changed in signature or behaviour from vers
472453

473454
#### Helpers
474455

456+
All helpers are now exposed in a flat hierarchy, e.g., `Chart.helpers.canvas.clipArea` -> `Chart.helpers.clipArea`
457+
475458
##### Canvas Helper
476459

477460
* The second parameter to `drawPoint` is now the full options object, so `style`, `rotation`, and `radius` are no longer passed explicitly

helpers/helpers.esm.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../dist/helpers.esm';

helpers/helpers.esm.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../dist/helpers.esm';

helpers/helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('..').helpers;

helpers/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "chart.js-helpers",
3+
"private": true,
4+
"description": "helper package",
5+
"main": "helpers.js",
6+
"module": "helpers.esm.js",
7+
"types": "helpers.esm.d.ts"
8+
}

rollup.config.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
const babel = require('rollup-plugin-babel');
55
const cleanup = require('rollup-plugin-cleanup');
66
const dts = require('rollup-plugin-dts').default;
7-
const glob = require('glob');
87
const inject = require('@rollup/plugin-inject');
98
const json = require('@rollup/plugin-json');
109
const resolve = require('@rollup/plugin-node-resolve').default;
@@ -14,16 +13,12 @@ const pkg = require('./package.json');
1413
const input = 'src/index.js';
1514
const inputESM = {
1615
'dist/chart.esm': 'src/index.esm.js',
16+
'dist/helpers.esm': 'src/helpers/index.js'
17+
};
18+
const inputESMTypings = {
19+
'dist/chart.esm': 'types/index.esm.d.ts',
20+
'dist/helpers.esm': 'types/helpers/index.d.ts'
1721
};
18-
const inputESMTypings = {};
19-
glob('src/helpers/helpers.*.js', (_er, files) => {
20-
files.forEach(file => {
21-
inputESM[file.replace(/src\/|helpers\.|\.js/g, '')] = file;
22-
});
23-
Object.keys(inputESM).forEach((key) => {
24-
inputESMTypings[key.replace('src', 'types')] = inputESM[key].replace('src', 'types').replace(/\.js$/, '.d.ts');
25-
});
26-
});
2722

2823
const banner = `/*!
2924
* Chart.js v${pkg.version}
@@ -96,7 +91,7 @@ module.exports = [
9691
],
9792
output: {
9893
dir: './',
99-
chunkFileNames: 'helpers/chunks/[name].js',
94+
chunkFileNames: 'dist/chunks/[name].js',
10095
banner,
10196
format: 'esm',
10297
indent: false,
@@ -112,7 +107,7 @@ module.exports = [
112107
],
113108
output: {
114109
dir: './',
115-
chunkFileNames: 'helpers/chunks/[name].ts',
110+
chunkFileNames: 'dist/chunks/[name].ts',
116111
banner,
117112
format: 'esm',
118113
indent: false,

src/helpers/index.js

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,16 @@
11
/* eslint-disable import/no-namespace */
22

3-
import * as coreHelpers from './helpers.core';
4-
import * as canvas from './helpers.canvas';
5-
import * as collection from './helpers.collection';
6-
import * as curve from './helpers.curve';
7-
import * as dom from './helpers.dom';
8-
import effects from './helpers.easing';
9-
import * as interpolation from './helpers.interpolation';
10-
import * as options from './helpers.options';
11-
import * as math from './helpers.math';
12-
import * as rtl from './helpers.rtl';
13-
import * as segment from './helpers.segment';
3+
export * from './helpers.core';
4+
export * from './helpers.canvas';
5+
export * from './helpers.collection';
6+
export * from './helpers.curve';
7+
export * from './helpers.dom';
8+
export {default as easingEffects} from './helpers.easing';
9+
export * from './helpers.interpolation';
10+
export * from './helpers.options';
11+
export * from './helpers.math';
12+
export * from './helpers.rtl';
13+
export * from './helpers.segment';
1414

15-
import {color, getHoverColor} from './helpers.color';
16-
import {requestAnimFrame, fontString} from './helpers.extras';
17-
18-
export default {
19-
...coreHelpers,
20-
canvas,
21-
collection,
22-
curve,
23-
dom,
24-
easing: {effects},
25-
interpolation,
26-
options,
27-
math,
28-
rtl,
29-
segment,
30-
31-
requestAnimFrame,
32-
// -- Canvas methods
33-
fontString,
34-
color,
35-
getHoverColor
36-
};
15+
export {color, getHoverColor} from './helpers.color';
16+
export {requestAnimFrame, fontString} from './helpers.extras';

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
import Chart from './core/core.controller';
88

9-
import helpers from './helpers/index';
9+
import * as helpers from './helpers/index';
1010
import _adapters from './core/core.adapters';
1111
import Animation from './core/core.animation';
1212
import animator from './core/core.animator';
@@ -27,7 +27,7 @@ import Ticks from './core/core.ticks';
2727
// Register built-ins
2828
Chart.register(controllers, scales, elements, plugins);
2929

30-
Chart.helpers = helpers;
30+
Chart.helpers = {...helpers};
3131
Chart._adapters = _adapters;
3232
Chart.Animation = Animation;
3333
Chart.Animations = Animations;

test/specs/helpers.canvas.tests.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
describe('Chart.helpers.canvas', function() {
4-
describe('auto', jasmine.fixture.specs('helpers.canvas'));
4+
describe('auto', jasmine.fixture.specs('helpers'));
55

66
var helpers = Chart.helpers;
77

@@ -15,7 +15,7 @@ describe('Chart.helpers.canvas', function() {
1515

1616
spyOn(chart.ctx, 'clearRect');
1717

18-
helpers.canvas.clear(chart);
18+
helpers.clear(chart);
1919

2020
expect(chart.ctx.clearRect.calls.count()).toBe(1);
2121
expect(chart.ctx.clearRect.calls.first().object).toBe(chart.ctx);
@@ -25,7 +25,7 @@ describe('Chart.helpers.canvas', function() {
2525

2626
describe('isPointInArea', function() {
2727
it('should determine if a point is in the area', function() {
28-
var isPointInArea = helpers.canvas._isPointInArea;
28+
var isPointInArea = helpers._isPointInArea;
2929
var area = {left: 0, top: 0, right: 512, bottom: 256};
3030

3131
expect(isPointInArea({x: 0, y: 0}, area)).toBe(true);
@@ -45,8 +45,8 @@ describe('Chart.helpers.canvas', function() {
4545

4646

4747
// Regardless 'FooBar' is the longest label it should return (characters * 10)
48-
expect(helpers.canvas._longestText(context, font, arrayOfThings1D, {})).toEqual(60);
49-
expect(helpers.canvas._longestText(context, font, arrayOfThings2D, {})).toEqual(80);
48+
expect(helpers._longestText(context, font, arrayOfThings1D, {})).toEqual(60);
49+
expect(helpers._longestText(context, font, arrayOfThings2D, {})).toEqual(80);
5050
// We check to make sure we made the right calls to the canvas.
5151
expect(context.getCalls()).toEqual([{
5252
name: 'save',
@@ -84,9 +84,9 @@ describe('Chart.helpers.canvas', function() {
8484
var gc = [];
8585
var longest = 70;
8686

87-
expect(helpers.canvas._measureText(context, data, gc, longest, 'foobar')).toEqual(70);
88-
expect(helpers.canvas._measureText(context, data, gc, longest, 'foobar_')).toEqual(70);
89-
expect(helpers.canvas._measureText(context, data, gc, longest, 'foobar_1')).toEqual(80);
87+
expect(helpers._measureText(context, data, gc, longest, 'foobar')).toEqual(70);
88+
expect(helpers._measureText(context, data, gc, longest, 'foobar_')).toEqual(70);
89+
expect(helpers._measureText(context, data, gc, longest, 'foobar_1')).toEqual(80);
9090
// We check to make sure we made the right calls to the canvas.
9191
expect(context.getCalls()).toEqual([{
9292
name: 'measureText',

test/specs/helpers.collection.tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {_filterBetween, _lookup, _lookupByKey, _rlookupByKey} = Chart.helpers.collection;
1+
const {_filterBetween, _lookup, _lookupByKey, _rlookupByKey} = Chart.helpers;
22

33
describe('helpers.collection', function() {
44
it('Should do binary search', function() {

test/specs/helpers.curve.tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ describe('Curve helper tests', function() {
22
let helpers;
33

44
beforeAll(function() {
5-
helpers = window.Chart.helpers.curve;
5+
helpers = window.Chart.helpers;
66
});
77

88
it('should spline curves', function() {

test/specs/helpers.dom.tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ describe('DOM helpers tests', function() {
22
let helpers;
33

44
beforeAll(function() {
5-
helpers = window.Chart.helpers.dom;
5+
helpers = window.Chart.helpers;
66
});
77

88
it ('should get the maximum size for a node', function() {

test/specs/helpers.easing.tests.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
describe('Chart.helpers.easing', function() {
4-
var easing = Chart.helpers.easing;
3+
describe('Chart.helpers.easingEffects', function() {
4+
var helpers = Chart.helpers;
55

66
describe('effects', function() {
77
var expected = {
@@ -39,7 +39,7 @@ describe('Chart.helpers.easing', function() {
3939
};
4040

4141
function generate(method) {
42-
var fn = easing.effects[method];
42+
var fn = helpers.easingEffects[method];
4343
var accuracy = Math.pow(10, 8);
4444
var count = 10;
4545
var values = [];
@@ -52,7 +52,7 @@ describe('Chart.helpers.easing', function() {
5252
return values;
5353
}
5454

55-
Object.keys(easing.effects).forEach(function(method) {
55+
Object.keys(helpers.easingEffects).forEach(function(method) {
5656
it ('"' + method + '" should return expected values', function() {
5757
expect(generate(method)).toEqual(expected[method]);
5858
});

test/specs/helpers.interpolation.tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {_pointInLine, _steppedInterpolation, _bezierInterpolation} = Chart.helpers.interpolation;
1+
const {_pointInLine, _steppedInterpolation, _bezierInterpolation} = Chart.helpers;
22

33
describe('helpers.interpolation', function() {
44
it('Should interpolate a point in line', function() {

test/specs/helpers.math.tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const math = Chart.helpers.math;
1+
const math = Chart.helpers;
22

33
describe('Chart.helpers.math', function() {
44
var factorize = math._factorize;

test/specs/helpers.options.tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {toLineHeight, toPadding, toFont, resolve} = Chart.helpers.options; // from '../../src/helpers/helpers.options';
1+
const {toLineHeight, toPadding, toFont, resolve} = Chart.helpers; // from '../../src/helpers/helpers.options';
22

33
describe('Chart.helpers.options', function() {
44
describe('toLineHeight', function() {

test/specs/helpers.segment.tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {_boundSegment} = Chart.helpers.segment;
1+
const {_boundSegment} = Chart.helpers;
22

33
describe('helpers.segments', function() {
44
describe('_boundSegment', function() {

types/helpers/helpers.easing.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import { EasingFunction } from '../core/interfaces';
22

33
export type EasingFunctionSignature = (t: number) => number;
44

5-
export const easing: Record<EasingFunction, EasingFunctionSignature>;
5+
export const easingEffects: Record<EasingFunction, EasingFunctionSignature>;

0 commit comments

Comments
 (0)