Skip to content

Commit 23a250c

Browse files
committed
Fix TypeScript errors in lib/utils
1 parent de48d53 commit 23a250c

File tree

5 files changed

+40
-22
lines changed

5 files changed

+40
-22
lines changed

src/lib/utils/arraysEqual.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
This uses includes instead of converting to a set because this is only
44
used internally on a small array size and it's not worth the cost
55
of making a set
6-
@param {Array} arr1 An array to test
7-
@param {Array} arr2 An array to test against
6+
@param {Array<any>} arr1 An array to test
7+
@param {Array<any>} arr2 An array to test against
88
@returns {boolean} Whether they contain all and only the same items
99
*/
1010
export default function arraysEqual(arr1, arr2) {

src/lib/utils/debounce.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
/**
22
A simple debounce function taken from here https://www.freecodecamp.org/news/javascript-debounce-example/
3-
@param {function} func The function to debounce.
3+
@param {Function} func The function to debounce.
44
@param {number} timeout The time in ms to wait.
5-
@returns {function}
5+
@returns {Function}
66
*/
7-
export default function debounce(func, timeout = 300) {
7+
const debounce = (func, timeout = 300) => {
8+
/** @type {string | number | NodeJS.Timeout | undefined} */
89
let timer;
9-
return (...args) => {
10+
return (/** @type {any[]} */ ...args) => {
1011
clearTimeout(timer);
1112
timer = setTimeout(() => {
1213
func.apply(this, args);
1314
}, timeout);
1415
};
15-
}
16+
};
17+
18+
export default debounce;

src/lib/utils/filterObject.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
Remove undefined fields from an object
33
@param {object} obj The object to filter
4-
@param {object} [comparisonObj={}] An object that, for any key, if the key is not present on that object, the key will be filtered out. Note, this ignores the value on that object
4+
@param {Object.<string, any>} [comparisonObj={}] An object that, for any key, if the key is not present on that object, the key will be filtered out. Note, this ignores the value on that object
55
@returns {object}
66
*/
77
export default function filterObject(obj, comparisonObj = {}) {

src/lib/utils/makeAccessor.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import canBeZero from './canBeZero.js';
22
/**
33
Make an accessor from a string, number, function or an array of the combination of any
4-
@param {string|number|Function|Array} acc The accessor function, key or list of them.
5-
@returns {Function} An accessor function.
4+
@param {string|number|Function|Array<string|number|Function>} acc The accessor function, key or list of them.
5+
@returns {Function|null} An accessor function.
66
*/
77
export default function makeAccessor(acc) {
88
if (!canBeZero(acc)) return null;
99
if (Array.isArray(acc)) {
10-
return d =>
10+
return /** @param {any} d */ d =>
1111
acc.map(k => {
1212
return typeof k !== 'function' ? d[k] : k(d);
1313
});
1414
} else if (typeof acc !== 'function') {
15-
return d => d[acc];
15+
return /** @param {any} d */ d => d[acc];
1616
}
1717
return acc;
1818
}

src/lib/utils/padScale.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1+
import isOrdinalDomain from '../helpers/isOrdinalDomain.js';
2+
import getPadFunctions from '../helpers/getPadFunctions.js';
3+
import findScaleName from '../helpers/findScaleName.js';
4+
5+
// These scales have a discrete range so they can't be padded
6+
const unpaddable = ['scaleThreshold', 'scaleQuantile', 'scaleQuantize', 'scaleSequentialQuantile'];
7+
8+
/**
9+
* @typedef {import('d3-scale').ScaleLinear<any, any> |
10+
* import('d3-scale').ScaleTime<any, any> |
11+
* import('d3-scale').ScalePower<any, any> |
12+
* import('d3-scale').ScaleLogarithmic<any, any> |
13+
* import('d3-scale').ScaleSymLog<any, any> |
14+
* import('d3-scale').ScaleOrdinal<any, any> |
15+
* import('d3-scale').ScaleBand<any> |
16+
* import('d3-scale').ScalePoint<any> |
17+
* import('d3-scale').ScaleSequential<any, any> |
18+
* import('d3-scale').ScaleDiverging<any, any> |
19+
* import('d3-scale').ScaleQuantize<any, any> |
20+
* import('d3-scale').ScaleThreshold<any, any>} Scale
21+
*/
22+
123
/**
224
Returns a modified scale domain by in/decreasing
325
the min/max by taking the desired difference
426
in pixels and converting it to units of data.
527
Returns an array that you can set as the new domain.
628
Padding contributed by @veltman.
729
See here for discussion of transforms: https://github.com/d3/d3-scale/issues/150
8-
@param {Function} scale A D3 scale funcion
9-
@param {number[]} padding A two-value array of numbers specifying padding in pixels
30+
@param {Scale} scale A D3 scale funcion
31+
@param {[number, number]} padding A two-value array of numbers specifying padding in pixels
1032
@returns {number[]} The padded domain
1133
*/
12-
import isOrdinalDomain from '../helpers/isOrdinalDomain.js';
13-
import getPadFunctions from '../helpers/getPadFunctions.js';
14-
import findScaleName from '../helpers/findScaleName.js';
15-
16-
// These scales have a discrete range so they can't be padded
17-
const unpaddable = ['scaleThreshold', 'scaleQuantile', 'scaleQuantize', 'scaleSequentialQuantile'];
18-
1934
export default function padScale(scale, padding) {
2035
if (typeof scale.range !== 'function') {
2136
throw new Error('Scale method `range` must be a function');
@@ -41,7 +56,7 @@ export default function padScale(scale, padding) {
4156
const [d1, d2] = scale.domain().map(d => {
4257
return isTime ? lift(d.getTime()) : lift(d);
4358
});
44-
const [r1, r2] = scale.range();
59+
const [r1, r2] = /** @type {any[]} */ (scale.range());
4560
const paddingLeft = padding[0] || 0;
4661
const paddingRight = padding[1] || 0;
4762

0 commit comments

Comments
 (0)