Skip to content

Commit

Permalink
Use custom type guards for legend as well
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Nov 30, 2015
1 parent ed2a735 commit 428490c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/compiler/legend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {COLOR, SIZE, SHAPE, Channel} from '../channel';
import {Model} from './Model';
import * as time from './time';
import {TEMPORAL} from '../type';
import {isLegend} from '../schema/legend.schema';

export function compileLegends(model: Model) {
var defs = [];
Expand All @@ -29,7 +30,8 @@ export function compileLegends(model: Model) {
}

export function compileLegend(model: Model, channel: Channel, def) {
let legend = model.fieldDef(channel).legend;
// Have to use any because for some reason my custom type guard is not working
const legend:any = model.fieldDef(channel).legend;

// 1.1 Add properties with special rules
def.title = title(model, channel);
Expand All @@ -43,7 +45,7 @@ export function compileLegend(model: Model, channel: Channel, def) {
});

// 2) Add mark property definition groups
const props = typeof legend === 'Legend' && legend.properties || {};
const props = isLegend(legend) && legend.properties || {};
['title', 'labels', 'symbols', 'legend'].forEach(function(group) {
let value = properties[group] ?
properties[group](model, channel, props[group]) : // apply rule
Expand All @@ -58,9 +60,10 @@ export function compileLegend(model: Model, channel: Channel, def) {
}

export function title(model: Model, channel: Channel) {
const legend = model.fieldDef(channel).legend;
// Have to use any because for some reason my custom type guard is not working
const legend: any = model.fieldDef(channel).legend;

if (typeof legend === 'Legend' && legend.title) {
if (isLegend(legend) && legend.title) {
return legend.title;
}

Expand Down
6 changes: 6 additions & 0 deletions src/schema/legend.schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {isObject} from '../util';

export interface Legend {
orient?: string;
title?: string;
Expand All @@ -6,6 +8,10 @@ export interface Legend {
properties?: any; //TODO declare VgLegendProperties
}

export function isLegend(object: any): object is Legend {
return isObject(object);
}

export var legend = {
default: true,
description: 'Properties of a legend or boolean flag for determining whether to show it.',
Expand Down

0 comments on commit 428490c

Please sign in to comment.