Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
pugnascotia committed Dec 11, 2018
1 parent 52fc8e3 commit e75411f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/services/accessibility/html_id_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`;
}
8 changes: 1 addition & 7 deletions src/services/color/color_palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
49 changes: 40 additions & 9 deletions src/services/color/rgb_to_hex.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
2 changes: 1 addition & 1 deletion src/services/color/rgb_to_hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
Expand Down
13 changes: 6 additions & 7 deletions src/services/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T>(values: T[]): T => {
return values[Math.floor(this._rand() * values.length)];
return values[Math.floor(this.rand() * values.length)];
}

oneToOne = <T>(values: T[], index: number): T => {
Expand Down

0 comments on commit e75411f

Please sign in to comment.