From: Jacco van den Berg <39033624+LeeLenaleee@users.noreply.github.com> Date: Tue, 3 May 2022 12:21:43 +0000 (+0200) Subject: Resolve canvasGradient is undefined in node (#10328) X-Git-Tag: v3.8.0~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2c268f09437f88a8be1d0d0ce87e974d17354961;p=thirdparty%2FChart.js.git Resolve canvasGradient is undefined in node (#10328) * Resolve canvasgradient is not defined in node * Remove trailing white space * export isPaternOrGradient helper with typings * fix lint failure, single qoute * Allow for string inputs too to function --- diff --git a/src/helpers/helpers.color.js b/src/helpers/helpers.color.js index 62e667161..9394751de 100644 --- a/src/helpers/helpers.color.js +++ b/src/helpers/helpers.color.js @@ -1,6 +1,13 @@ import colorLib from '@kurkle/color'; -const isPatternOrGradient = (value) => value instanceof CanvasGradient || value instanceof CanvasPattern; +export function isPatternOrGradient(value) { + if (value && typeof value === 'object') { + const type = value.toString(); + return type === '[object CanvasPattern]' || type === '[object CanvasGradient]'; + } + + return false; +} export function color(value) { return isPatternOrGradient(value) ? value : colorLib(value); diff --git a/types/helpers/helpers.color.d.ts b/types/helpers/helpers.color.d.ts index 3cfc20ea7..460b8d1fe 100644 --- a/types/helpers/helpers.color.d.ts +++ b/types/helpers/helpers.color.d.ts @@ -1,3 +1,5 @@ +import { AnyObject } from '../basic'; + export function color(value: CanvasGradient): CanvasGradient; export function color(value: CanvasPattern): CanvasPattern; export function color( @@ -8,6 +10,8 @@ export function color( | [number, number, number, number] ): ColorModel; +export function isPatternOrGradient(value: string | AnyObject): boolean; + export interface ColorModel { rgbString(): string; hexString(): string;