Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds isSprite typeguard #615

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
};