Skip to content

Commit

Permalink
feat: adds isSprite typeguard
Browse files Browse the repository at this point in the history
  • Loading branch information
Kai Volland committed Dec 1, 2023
1 parent 5296d88 commit 2684c38
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
26 changes: 25 additions & 1 deletion test/typeguards.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
RasterSymbolizer,
Rule,
ScaleDenominator,
Sprite,
TextSymbolizer
} from '../style';
import {
Expand All @@ -47,6 +48,7 @@ import {
isRgbChannel,
isRule,
isScaleDenominator,
isSprite,
isSymbolizer,
isTextSymbolizer
} from '../typeguards';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -275,7 +289,8 @@ const thingsToTest = [
numberFunction2,
stringFunction,
booleanFunction,
unknownFunction
unknownFunction,
spriteImage
];

describe('typeguards', () => {
Expand Down Expand Up @@ -517,4 +532,13 @@ describe('typeguards', () => {
});
});

it('isSprite', () => {
const expectedMatches: any[] = [
spriteImage
];
thingsToTest.forEach(thing => {
expect(isSprite(thing)).toBe(expectedMatches.includes(thing));
});
});

});
9 changes: 8 additions & 1 deletion typeguards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import {
GeoStylerFunction,
PointSymbolizer,
Symbolizer,
FunctionCall
FunctionCall,
Sprite
} from './index';

export const isExpression = (got: any): got is Expression<any> => {
Expand Down Expand Up @@ -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);
};

0 comments on commit 2684c38

Please sign in to comment.