Skip to content

Commit

Permalink
Fallback Syntax and Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ingalls committed Sep 24, 2024
1 parent dd224e5 commit f94d307
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/lib/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import type { Feature } from '@tak-ps/node-cot';
import handlebars from 'handlebars';
import Err from '@openaddresses/batch-error';

handlebars.registerHelper('fallback', (...params: Array<unknown>) => {
return params.find(el => !!el)
})

interface ValidateStyle {
id?: string;
callsign?: string;
Expand Down
42 changes: 42 additions & 0 deletions api/test/style.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,3 +734,45 @@ test('Style: Invalid Templates', async () => {
})
}, /Expecting/);
});

test('Style: {{fallback p1 p2 ...}}', async () => {
const style = new Style({
stale: 123,
enabled_styles: true,
styles: {
callsign: '{{fallback none1 none2 yes1 none3 yes2}}'
}
});

assert.deepEqual(await style.feat({
type: 'Feature',
properties: {
metadata: {
yes1: 'I exist',
yes2: 'I exist but shouldn\'t be picked'
}
},
geometry: {
type: 'Point',
coordinates: [0, 0]
}
}), {
type: 'Feature',
properties: {
callsign: 'I exist',
metadata: {
yes1: 'I exist',
yes2: "I exist but shouldn't be picked"
},
stale: 123000
},
geometry: {
coordinates: [
0,
0
],
type: 'Point'
},
});
});

0 comments on commit f94d307

Please sign in to comment.