Skip to content

Commit

Permalink
Fix TypeScript errors in lib/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
rgieseke committed Aug 29, 2024
1 parent de48d53 commit 39e9c1f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/lib/utils/arraysEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
This uses includes instead of converting to a set because this is only
used internally on a small array size and it's not worth the cost
of making a set
@param {Array} arr1 An array to test
@param {Array} arr2 An array to test against
@param {Array<any>} arr1 An array to test
@param {Array<any>} arr2 An array to test against
@returns {boolean} Whether they contain all and only the same items
*/
export default function arraysEqual(arr1, arr2) {
Expand Down
13 changes: 8 additions & 5 deletions src/lib/utils/debounce.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/**
A simple debounce function taken from here https://www.freecodecamp.org/news/javascript-debounce-example/
@param {function} func The function to debounce.
@param {Function} func The function to debounce.
@param {number} timeout The time in ms to wait.
@returns {function}
@returns {Function}
*/
export default function debounce(func, timeout = 300) {
const debounce = (func, timeout = 300) => {
/** @type {string | number | NodeJS.Timeout | undefined} */
let timer;
return (...args) => {
return (/** @type {any[]} */ ...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, args);
}, timeout);
};
}
};

export default debounce;
2 changes: 1 addition & 1 deletion src/lib/utils/filterObject.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
Remove undefined fields from an object
@param {object} obj The object to filter
@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
@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
@returns {object}
*/
export default function filterObject(obj, comparisonObj = {}) {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/utils/makeAccessor.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import canBeZero from './canBeZero.js';
/**
Make an accessor from a string, number, function or an array of the combination of any
@param {string|number|Function|Array} acc The accessor function, key or list of them.
@returns {Function} An accessor function.
@param {string|number|Function|Array<string|number|Function>} acc The accessor function, key or list of them.
@returns {Function|null} An accessor function.
*/
export default function makeAccessor(acc) {
if (!canBeZero(acc)) return null;
if (Array.isArray(acc)) {
return d =>
return /** @param {any} d */ d =>
acc.map(k => {
return typeof k !== 'function' ? d[k] : k(d);
});
} else if (typeof acc !== 'function') {
return d => d[acc];
return /** @param {any} d */ d => d[acc];
}
return acc;
}
36 changes: 26 additions & 10 deletions src/lib/utils/padScale.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
import isOrdinalDomain from '../helpers/isOrdinalDomain.js';
import getPadFunctions from '../helpers/getPadFunctions.js';
import findScaleName from '../helpers/findScaleName.js';

// These scales have a discrete range so they can't be padded
const unpaddable = ['scaleThreshold', 'scaleQuantile', 'scaleQuantize', 'scaleSequentialQuantile'];

/**
* @typedef {import('d3-scale').ScaleLinear<any, any> |
* import('d3-scale').ScaleTime<any, any> |
* import('d3-scale').ScalePower<any, any> |
* import('d3-scale').ScaleLogarithmic<any, any> |
* import('d3-scale').ScaleSymLog<any, any> |
* import('d3-scale').ScaleOrdinal<any, any> |
* import('d3-scale').ScaleBand<any> |
* import('d3-scale').ScalePoint<any> |
* import('d3-scale').ScaleSequential<any, any> |
* import('d3-scale').ScaleDiverging<any, any> |
* import('d3-scale').ScaleQuantile<any, any> |
* import('d3-scale').ScaleQuantize<any, any> |
* import('d3-scale').ScaleThreshold<any, any>} Scale
*/

/**
Returns a modified scale domain by in/decreasing
the min/max by taking the desired difference
in pixels and converting it to units of data.
Returns an array that you can set as the new domain.
Padding contributed by @veltman.
See here for discussion of transforms: https://github.com/d3/d3-scale/issues/150
@param {Function} scale A D3 scale funcion
@param {number[]} padding A two-value array of numbers specifying padding in pixels
@param {Scale} scale A D3 scale funcion
@param {[number, number]} padding A two-value array of numbers specifying padding in pixels
@returns {number[]} The padded domain
*/
import isOrdinalDomain from '../helpers/isOrdinalDomain.js';
import getPadFunctions from '../helpers/getPadFunctions.js';
import findScaleName from '../helpers/findScaleName.js';

// These scales have a discrete range so they can't be padded
const unpaddable = ['scaleThreshold', 'scaleQuantile', 'scaleQuantize', 'scaleSequentialQuantile'];

export default function padScale(scale, padding) {
if (typeof scale.range !== 'function') {
throw new Error('Scale method `range` must be a function');
Expand All @@ -41,7 +57,7 @@ export default function padScale(scale, padding) {
const [d1, d2] = scale.domain().map(d => {
return isTime ? lift(d.getTime()) : lift(d);
});
const [r1, r2] = scale.range();
const [r1, r2] = /** @type {any[]} */ (scale.range());
const paddingLeft = padding[0] || 0;
const paddingRight = padding[1] || 0;

Expand Down

0 comments on commit 39e9c1f

Please sign in to comment.