d3-color: strict null checks and instanceof (#25211)

* Strict null checks

strict null check,
uncomment prototypes,
RGBColor#rgb returns this,
remove `displayable` and `toString` when inherits `Color`

* Contributors
This commit is contained in:
denisname
2018-04-25 01:04:48 +02:00
committed by Wesley Wigham
parent 64104e9760
commit af589d355f
3 changed files with 43 additions and 35 deletions

View File

@@ -8,37 +8,21 @@
import * as d3Color from 'd3-color';
// RGB and HSL Typeguards
function isRGB(color: d3Color.RGBColor | d3Color.HSLColor): color is d3Color.RGBColor {
return (color instanceof d3Color.rgb);
}
function isHSL(color: d3Color.RGBColor | d3Color.HSLColor): color is d3Color.HSLColor {
return (color instanceof d3Color.hsl);
}
// Signature tests for 'color', rgb and hsl
let c: d3Color.RGBColor | d3Color.HSLColor;
let c: d3Color.RGBColor | d3Color.HSLColor | null;
let cRGB: d3Color.RGBColor;
let cHSL: d3Color.HSLColor;
let displayable: boolean;
let cString: string;
let nil: null;
// string signature
c = d3Color.color('oops');
c = d3Color.color('steelblue');
if (isRGB(c)) {
cRGB = c;
} else {
cHSL = c;
}
c = d3Color.color('rgba(20, 100, 200, 0.5)');
c = d3Color.color(cRGB);
c = d3Color.color(d3Color.rgb(0, 0, 0));
cRGB = d3Color.color('hsl(60, 100%, 20%, 0.5)').rgb();
cRGB = d3Color.color('hsl(60, 100%, 20%, 0.5)')!.rgb();
cRGB = d3Color.rgb(20, 100, 200);
cRGB = d3Color.rgb(20, 100, 200, 0.5);
@@ -126,3 +110,27 @@ displayable = cCubehelix.displayable();
cString = cCubehelix.toString();
console.log('Channels = (h : %d, s: %d, l: %d)', cCubehelix.h, cCubehelix.s, cCubehelix.l);
console.log('Opacity = %d', cCubehelix.opacity);
// Prototype, instanceof and typeguard
declare let color: d3Color.RGBColor | d3Color.HSLColor | d3Color.LabColor | d3Color.HCLColor | d3Color.CubehelixColor | null;
if (color instanceof d3Color.rgb) {
cRGB = color;
} else if (color instanceof d3Color.hsl) {
cHSL = color;
} else if (color instanceof d3Color.lab) {
cLab = color;
} else if (color instanceof d3Color.hcl) {
cHcl = color;
} else if (color instanceof d3Color.cubehelix) {
cCubehelix = color;
} else if (color === null) {
nil = color;
}
if (color instanceof d3Color.color) {
console.log(color.toString(), color.darker());
} else {
nil = color;
}

View File

@@ -1,9 +1,12 @@
// Type definitions for D3JS d3-color module 1.0
// Project: https://github.com/d3/d3-color/
// Definitions by: Tom Wanzek <https://github.com/tomwanzek>, Alex Ford <https://github.com/gustavderdrache>, Boris Yankov <https://github.com/borisyankov>
// Definitions by: Tom Wanzek <https://github.com/tomwanzek>
// Alex Ford <https://github.com/gustavderdrache>
// Boris Yankov <https://github.com/borisyankov>
// denisname <https://github.com/denisname>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Last module patch version validated against: 1.0.1
// Last module patch version validated against: 1.0.3
// ---------------------------------------------------------------------------
// Shared Type Definitions and Interfaces
@@ -32,9 +35,9 @@ export interface Color {
}
export interface ColorFactory extends Function {
(cssColorSpecifier: string): RGBColor | HSLColor;
(cssColorSpecifier: string): RGBColor | HSLColor | null;
(color: ColorSpaceObject | ColorCommonInstance): RGBColor | HSLColor;
// prototype: Color;
readonly prototype: Color;
}
export interface RGBColor extends Color {
@@ -44,16 +47,14 @@ export interface RGBColor extends Color {
opacity: number;
brighter(k?: number): this;
darker(k?: number): this;
displayable(): boolean;
rgb(): RGBColor;
toString(): string;
rgb(): this;
}
export interface RGBColorFactory extends Function {
(r: number, g: number, b: number, opacity?: number): RGBColor;
(cssColorSpecifier: string): RGBColor;
(color: ColorSpaceObject | ColorCommonInstance): RGBColor;
// prototype: RGBColor;
readonly prototype: RGBColor;
}
export interface HSLColor extends Color {
@@ -63,7 +64,6 @@ export interface HSLColor extends Color {
opacity: number;
brighter(k?: number): this;
darker(k?: number): this;
displayable(): boolean;
rgb(): RGBColor;
}
@@ -71,7 +71,7 @@ export interface HSLColorFactory extends Function {
(h: number, s: number, l: number, opacity?: number): HSLColor;
(cssColorSpecifier: string): HSLColor;
(color: ColorSpaceObject | ColorCommonInstance): HSLColor;
// prototype: HSLColor;
readonly prototype: HSLColor;
}
export interface LabColor extends Color {
@@ -88,7 +88,7 @@ export interface LabColorFactory extends Function {
(l: number, a: number, b: number, opacity?: number): LabColor;
(cssColorSpecifier: string): LabColor;
(color: ColorSpaceObject | ColorCommonInstance): LabColor;
// prototype: LabColor;
readonly prototype: LabColor;
}
export interface HCLColor extends Color {
@@ -105,7 +105,7 @@ export interface HCLColorFactory extends Function {
(h: number, l: number, c: number, opacity?: number): HCLColor;
(cssColorSpecifier: string): HCLColor;
(color: ColorSpaceObject | ColorCommonInstance): HCLColor;
// prototype: HCLColor;
readonly prototype: HCLColor;
}
export interface CubehelixColor extends Color {
@@ -122,7 +122,7 @@ export interface CubehelixColorFactory extends Function {
(h: number, s: number, l: number, opacity?: number): CubehelixColor;
(cssColorSpecifier: string): CubehelixColor;
(color: ColorSpaceObject | ColorCommonInstance): CubehelixColor;
// prototype: CubehelixColor;
readonly prototype: CubehelixColor;
}
// --------------------------------------------------------------------------

View File

@@ -7,7 +7,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [