Skip to content

Commit 51da1a7

Browse files
committed
feat(colors): add type guards to sequential color scales
1 parent 1a2f9d8 commit 51da1a7

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

packages/colors/src/scales/continuousColorScale.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useMemo } from 'react'
2+
import isPlainObject from 'lodash/isPlainObject.js'
23
import { ScaleDiverging, ScaleQuantize, ScaleSequential, scaleLinear } from 'd3-scale'
34
import {
45
SequentialColorScaleConfig,
@@ -26,18 +27,25 @@ export type ContinuousColorScaleValues =
2627
| DivergingColorScaleValues
2728
| QuantizeColorScaleValues
2829

29-
const isSequentialColorScaleConfig = (
30+
export const isSequentialColorScaleConfig = (
3031
config: ContinuousColorScaleConfig
3132
): config is SequentialColorScaleConfig => config.type === 'sequential'
3233

33-
const isDivergingColorScaleConfig = (
34+
export const isDivergingColorScaleConfig = (
3435
config: ContinuousColorScaleConfig
3536
): config is DivergingColorScaleConfig => config.type === 'diverging'
3637

37-
const isQuantizeColorScaleConfig = (
38+
export const isQuantizeColorScaleConfig = (
3839
config: ContinuousColorScaleConfig
3940
): config is QuantizeColorScaleConfig => config.type === 'quantize'
4041

42+
export const isContinuousColorScale = (config: unknown): config is ContinuousColorScaleConfig => {
43+
if (!isPlainObject(config)) return false
44+
45+
const { type } = config as { type: string }
46+
return type === 'sequential' || type === 'diverging' || type === 'quantize'
47+
}
48+
4149
export const getContinuousColorScale = <Config extends ContinuousColorScaleConfig>(
4250
config: Config,
4351
values: ContinuousColorScaleValues

0 commit comments

Comments
 (0)