Skip to content

Commit

Permalink
Merge 5700ecd into 9dd61cc
Browse files Browse the repository at this point in the history
  • Loading branch information
marcjansen authored Sep 10, 2018
2 parents 9dd61cc + 5700ecd commit 078e974
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/FeatureUtil/FeatureUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@ class FeatureUtil {
*
* @param {ol.Feature} feature The feature to get the attributes from.
* @param {String} template The template string to resolve.
* @param {String} noValueFoundText The text to apply, if the templated value
* could not be found, default is to 'n.v.'.
* @param {String} [noValueFoundText] The text to apply, if the templated value
* could not be found, default is to 'n.v.'.
* @param {Function} [valueAdjust] A method that will be called with each
* key/value match, we'll use what this function returns for the actual
* replacement. Optional, defaults to a function which will return the raw
* value it received. This can be used for last minute adjustments before
* replacing happens, e.g. to filter out falsy values or to do number
* formatting and such.
* @return {String} The resolved template string.
*/
static resolveAttributeTemplate(feature, template, noValueFoundText = 'n.v.') {
static resolveAttributeTemplate(feature, template, noValueFoundText = 'n.v.', valueAdjust = (key, val) => val) {
let attributeTemplatePrefix = '\\{\\{';
let attributeTemplateSuffix = '\\}\\}';
let resolved = '';
Expand All @@ -63,7 +69,7 @@ class FeatureUtil {
let attributeName = res.slice(2, res.length - 2);

if (attributeName.toLowerCase() === key.toLowerCase()) {
template = template.replace(res, value);
template = template.replace(res, valueAdjust(key, value));
break;
} else {
noMatchCnt++;
Expand Down
20 changes: 19 additions & 1 deletion src/FeatureUtil/FeatureUtil.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ describe('FeatureUtil', () => {
name: 'Shinji Kagawa',
address: 'Borsigplatz 9',
city: 'Dortmund',
homepage: 'https://www.bvb.de/'
homepage: 'https://www.bvb.de/',
'exists-and-is-undefined': undefined,
'exists-and-is-null': null
};
feat = new OlFeature({
geometry: geom
Expand Down Expand Up @@ -80,6 +82,22 @@ describe('FeatureUtil', () => {
expect(got).toBe(template);
});

it('can be configured wrt handling inexistant / falsy values', () => {
let template = '{{exists-and-is-undefined}}|{{exists-and-is-null}}|{{key-does-not-exist}}';
let got = FeatureUtil.resolveAttributeTemplate(feat, template);
expect(got).toBe('undefined|null|n.v.');
got = FeatureUtil.resolveAttributeTemplate(feat, template, '', (key, val) => {return val ? val : '';});
expect(got).toBe('||');
const mockFn = jest.fn(() => {return 'FOO';});
got = FeatureUtil.resolveAttributeTemplate(feat, template, '', mockFn);
expect(mockFn.mock.calls.length).toBe(2);
expect(mockFn.mock.calls[0][0]).toBe('exists-and-is-undefined');
expect(mockFn.mock.calls[0][1]).toBe(undefined);
expect(mockFn.mock.calls[1][0]).toBe('exists-and-is-null');
expect(mockFn.mock.calls[1][1]).toBe(null);
expect(got).toBe('FOO|FOO|');
});

it('wraps an URL occurence with an <a> tag', () => {
let template = '{{homepage}}';
let got = FeatureUtil.resolveAttributeTemplate(feat, template);
Expand Down

0 comments on commit 078e974

Please sign in to comment.