diff --git a/src/services/accessibility/html_id_generator.ts b/src/services/accessibility/html_id_generator.ts index 483cbde9d09..21a8d80ac3b 100644 --- a/src/services/accessibility/html_id_generator.ts +++ b/src/services/accessibility/html_id_generator.ts @@ -7,7 +7,7 @@ import uuid from 'uuid'; * specify it, it generates a random id prefix. If you specify a custom prefix * it should begin with an letter to be HTML4 compliant. */ -export function htmlIdGenerator(idPrefix?: string): (suffix?: string) => string { +export function htmlIdGenerator(idPrefix?: string) { const prefix = idPrefix || `i${uuid.v1()}`; return (suffix?: string) => `${prefix}_${suffix || uuid.v1()}`; } diff --git a/src/services/color/color_palette.ts b/src/services/color/color_palette.ts index 116c32ae809..b686f9d980f 100644 --- a/src/services/color/color_palette.ts +++ b/src/services/color/color_palette.ts @@ -5,16 +5,10 @@ import { hexToRgb } from './hex_to_rgb'; * Create the color object for manipulation by other functions */ class Color { - r: number; - g: number; - b: number; collection: rgbDef; text: string; - constructor(r: number, g: number, b: number) { - this.r = r; // Red value - this.g = g; // Green value - this.b = b; // Blue value + constructor(public r: number, public g: number, public b: number) { this.collection = [r, g, b]; this.text = createHex(this.collection); } diff --git a/src/services/color/rgb_to_hex.test.ts b/src/services/color/rgb_to_hex.test.ts index fc9f6954718..2875def1167 100644 --- a/src/services/color/rgb_to_hex.test.ts +++ b/src/services/color/rgb_to_hex.test.ts @@ -1,19 +1,50 @@ import { rgbToHex } from './rgb_to_hex'; describe('rgbToHex ', () => { - it('should handle rgb() without whitespace', () => { - expect(rgbToHex('rgb(12,34,56)')).toEqual('#0c2238'); + describe('validation', () => { + it('should return an empty string for malformed input', () => { + expect(rgbToHex('fred')).toEqual(''); + expect(rgbToHex('rgb(fred')).toEqual(''); + expect(rgbToHex('rgb(fred, bob, banana')).toEqual(''); + expect(rgbToHex('rgb(0, 3, 5')).toEqual(''); + expect(rgbToHex('rgba(0, 3, 5')).toEqual(''); + expect(rgbToHex('rgba(0, 3, 5, 99)')).toEqual(''); + }); }); - it('should handle rgb() with whitespace', () => { - expect(rgbToHex('rgb ( 12 , 34 , 56 )')).toEqual('#0c2238'); - }); + describe('rgb()', () => { + it('should handle rgb() without whitespace', () => { + expect(rgbToHex('rgb(12,34,56)')).toEqual('#0c2238'); + }); - it('should handle rgba() without whitespace', () => { - expect(rgbToHex('rgba(12,34,56,0.4)')).toEqual('#0c2238'); + it('should handle rgb() with whitespace', () => { + expect(rgbToHex('rgb ( 12 , 34 , 56 )')).toEqual('#0c2238'); + }); }); - it('should handle rgba() with whitespace', () => { - expect(rgbToHex('rgba ( 12 , 34 , 56 , 0.4 )')).toEqual('#0c2238'); + describe('rgba()', () => { + it('should handle no whitespace', () => { + expect(rgbToHex('rgba(12,34,56,0.4)')).toEqual('#0c2238'); + }); + + it('should handle whitespace', () => { + expect(rgbToHex('rgba ( 12 , 34 , 56 , 0.4 )')).toEqual('#0c2238'); + }); + + it('should handle integer maximum alpha', () => { + expect(rgbToHex('rgba(12,34,56,1)')).toEqual('#0c2238'); + }); + + it('should handle decimal maximum alpha', () => { + expect(rgbToHex('rgba(12,34,56,1.00000)')).toEqual('#0c2238'); + }); + + it('should handle integer zero alpha', () => { + expect(rgbToHex('rgba(12,34,56,0)')).toEqual('#0c2238'); + }); + + it('should handle decimal zero alpha', () => { + expect(rgbToHex('rgba(12,34,56,0.0000)')).toEqual('#0c2238'); + }); }); }); diff --git a/src/services/color/rgb_to_hex.ts b/src/services/color/rgb_to_hex.ts index 03394726a6e..70b606f6eb5 100644 --- a/src/services/color/rgb_to_hex.ts +++ b/src/services/color/rgb_to_hex.ts @@ -5,7 +5,7 @@ function asHex(value: string): string { export function rgbToHex(rgb: string): string { const withoutWhitespace = rgb.replace(/\s+/g, ''); - const rgbMatch = withoutWhitespace.match(/^rgba?\((\d+),(\d+),(\d+)(?:,(?:1|0\.\d+))?\)$/i); + const rgbMatch = withoutWhitespace.match(/^rgba?\((\d+),(\d+),(\d+)(?:,(?:1(?:\.0*)?|0(?:\.\d+)?))?\)$/i); if (!rgbMatch) { return ''; } diff --git a/src/services/random.ts b/src/services/random.ts index 85dd1c78dc0..55372af35d5 100644 --- a/src/services/random.ts +++ b/src/services/random.ts @@ -5,33 +5,32 @@ import { times } from './utils'; const defaultRand = Math.random; export class Random { - // tslint:disable-next-line:variable-name - _rand: () => number; + private readonly rand: () => number; constructor(rand = defaultRand) { - this._rand = rand; + this.rand = rand; } boolean = () => { - return this._rand() > 0.5; + return this.rand() > 0.5; } number = (options: { min?: number, max?: number } = {}) => { const min = isNil(options.min) ? Number.MIN_VALUE : options.min!; const max = isNil(options.max) ? Number.MAX_VALUE : options.max!; - const delta = this._rand() * (max - min); + const delta = this.rand() * (max - min); return min + delta; } integer = (options: { min?: number, max?: number } = {}) => { const min = Math.ceil(isNil(options.min) ? Number.MIN_VALUE : options.min!); const max = Math.floor(isNil(options.max) ? Number.MAX_VALUE : options.max!); - const delta = Math.floor(this._rand() * (max - min + 1)); + const delta = Math.floor(this.rand() * (max - min + 1)); return min + delta; } oneOf = (values: T[]): T => { - return values[Math.floor(this._rand() * values.length)]; + return values[Math.floor(this.rand() * values.length)]; } oneToOne = (values: T[], index: number): T => {