diff --git a/test/typeguards.spec.ts b/test/typeguards.spec.ts index e82ef0305..bfb2913d8 100644 --- a/test/typeguards.spec.ts +++ b/test/typeguards.spec.ts @@ -21,6 +21,7 @@ import { RasterSymbolizer, Rule, ScaleDenominator, + Sprite, TextSymbolizer } from '../style'; import { @@ -47,6 +48,7 @@ import { isRgbChannel, isRule, isScaleDenominator, + isSprite, isSymbolizer, isTextSymbolizer } from '../typeguards'; @@ -240,6 +242,18 @@ const unknownFunction: GeoStylerUnknownFunction = { args: ['city'] }; +const spriteImage: Sprite = { + source: 'http://peter.de/sprite', + position: [{ + name: 'property', + args: ['width'], + }, { + name: 'property', + args: ['height'], + }], + size: [48, 48] +}; + const peter = 'peter'; const twelve = 12; const tru = true; @@ -275,7 +289,8 @@ const thingsToTest = [ numberFunction2, stringFunction, booleanFunction, - unknownFunction + unknownFunction, + spriteImage ]; describe('typeguards', () => { @@ -517,4 +532,13 @@ describe('typeguards', () => { }); }); + it('isSprite', () => { + const expectedMatches: any[] = [ + spriteImage + ]; + thingsToTest.forEach(thing => { + expect(isSprite(thing)).toBe(expectedMatches.includes(thing)); + }); + }); + }); diff --git a/typeguards.ts b/typeguards.ts index 4c41269ad..a879b2e3c 100644 --- a/typeguards.ts +++ b/typeguards.ts @@ -37,7 +37,8 @@ import { GeoStylerFunction, PointSymbolizer, Symbolizer, - FunctionCall + FunctionCall, + Sprite } from './index'; export const isExpression = (got: any): got is Expression => { @@ -265,3 +266,9 @@ export const isGeoStylerFunction = (got: any): got is GeoStylerFunction => { isGeoStylerStringFunction(got) || isGeoStylerUnknownFunction(got); }; + +export const isSprite = (got: any): got is Sprite => { + return typeof got?.source === 'string' || isGeoStylerFunction(got?.source) && + Array.isArray(got.position) && + Array.isArray(got.size); +};