From 428490c1f6e7ff3b5d6f5cafc0f5a74fda5ae773 Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Mon, 30 Nov 2015 14:13:55 -0800 Subject: [PATCH] Use custom type guards for legend as well --- src/compiler/legend.ts | 11 +++++++---- src/schema/legend.schema.ts | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/compiler/legend.ts b/src/compiler/legend.ts index 372fa4cbdc..4ec5d28f1c 100644 --- a/src/compiler/legend.ts +++ b/src/compiler/legend.ts @@ -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 = []; @@ -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); @@ -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 @@ -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; } diff --git a/src/schema/legend.schema.ts b/src/schema/legend.schema.ts index 5182b9474c..4b438f609b 100644 --- a/src/schema/legend.schema.ts +++ b/src/schema/legend.schema.ts @@ -1,3 +1,5 @@ +import {isObject} from '../util'; + export interface Legend { orient?: string; title?: string; @@ -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.',