Skip to content

Commit

Permalink
fix: remove changeset trait from state label, test colorize logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Aug 15, 2020
1 parent 50cf48f commit 4aeb324
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 18 deletions.
6 changes: 6 additions & 0 deletions docs/api/cautious-journey.flaglabel.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions docs/api/cautious-journey.flaglabel.requires.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/api/cautious-journey.statelabel.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/api/cautious-journey.statevalue.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/api/cautious-journey.syncoptions.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions docs/api/cautious-journey.syncoptions.random.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 13 additions & 8 deletions src/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,23 @@ export interface BaseLabel {
* Display color.
*/
color?: string;

/**
* Long-form description.
*/
desc?: string;

/**
* Label priority.
*/
priority: number;
requires: Array<LabelRef>;
}

/**
* Individual labels: the equivalent of a checkbox.
*/
export interface FlagLabel extends BaseLabel, ChangeSet {
/* empty */
requires: Array<LabelRef>;
}

/**
Expand All @@ -56,7 +63,7 @@ export interface StateChange extends ChangeSet {
/**
* One of many values for a particular state.
*/
export interface StateValue extends BaseLabel {
export interface StateValue extends BaseLabel, ChangeSet {
/**
* State changes that could occur to this value.
*/
Expand All @@ -66,7 +73,7 @@ export interface StateValue extends BaseLabel {
/**
* Grouped labels: the equivalent of a radio group.
*/
export interface StateLabel extends BaseLabel, ChangeSet {
export interface StateLabel extends BaseLabel {
/**
* Values for this state.
*/
Expand Down Expand Up @@ -121,17 +128,15 @@ export function prioritySort<TLabel extends BaseLabel>(labels: Array<TLabel>): A

/**
* Pick a label color, preferring the label data if set, falling back to a randomly selected color.
*
* TODO: this is a terrible overload
*/
export function getLabelColor(colors: Array<string>, random: prng, flag: FlagLabel): string;
export function getLabelColor(colors: Array<string>, random: prng, state: StateLabel, value: StateValue): string;
export function getLabelColor(colors: Array<string>, random: prng, label: BaseLabel, value?: StateValue): string {
if (doesExist(value) && doesExist(value.color)) {
if (doesExist(value) && doesExist(value.color) && value.color !== '') {
return value.color;
}

if (doesExist(label.color)) {
if (doesExist(label.color) && label.color !== '') {
return label.color;
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export function defaultUntil<T>(...items: Array<Optional<T>>): T {
}

export function randomItem<T>(items: Array<T>, source: prng): T {
const idx = Math.floor(source() * items.length);
const idx = source.int32() % items.length;
return items[idx];
}
64 changes: 59 additions & 5 deletions test/TestLabels.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { alea } from 'seedrandom';

import { getLabelNames, prioritySort } from '../src/labels';
import { getLabelColor, getLabelNames, prioritySort } from '../src/labels';

describe('label helpers', () => {
describe('label name helper', () => {
Expand Down Expand Up @@ -30,23 +31,21 @@ describe('label helpers', () => {

it('should return all states', () => {
const values = [{
adds: [],
becomes: [],
name: 'bin',
priority: 1,
removes: [],
requires: [],
}];
const states = [{
adds: [],
name: 'foo',
priority: 1,
removes: [],
requires: [],
values,
}, {
adds: [],
name: 'bar',
priority: 1,
removes: [],
requires: [],
values,
}];
Expand Down Expand Up @@ -92,4 +91,59 @@ describe('label helpers', () => {
expect(sorted[0]).to.deep.equal(FIRST_LABEL);
});
});

describe('label color helper', () => {
it('should return the value color', () => {
expect(getLabelColor(['test'], alea(), {
color: 'beans',
name: '',
priority: 1,
values: [],
}, {
adds: [],
becomes: [],
color: 'not',
name: '',
priority: 1,
removes: [],
})).to.equal('not');
});

it('should return the state color when value color is unset', () => {
expect(getLabelColor(['test'], alea(), {
color: 'beans',
name: '',
priority: 1,
values: [],
}, {
adds: [],
becomes: [],
color: '',
name: '',
priority: 1,
removes: [],
})).to.equal('beans');
});

it('should return the flag color', () => {
expect(getLabelColor(['test'], alea(), {
adds: [],
color: 'not',
name: '',
priority: 1,
removes: [],
requires: [],
})).to.equal('not');
});

it('should return a random color when the flag color is unset', () => {
expect(getLabelColor(['test'], alea(), {
adds: [],
name: '',
priority: 1,
removes: [],
requires: [],
})).to.equal('test');
});
});
});

0 comments on commit 4aeb324

Please sign in to comment.