diff --git a/packages/endpoint-posts/lib/utils.js b/packages/endpoint-posts/lib/utils.js index d54c8db3a..ada88801f 100644 --- a/packages/endpoint-posts/lib/utils.js +++ b/packages/endpoint-posts/lib/utils.js @@ -45,19 +45,22 @@ export const getGeoValue = (location) => { * @returns {object} JF2 location property */ export const getLocationProperty = (values) => { - const { location, geo } = values; + const { geo, location } = values; + + const hasGeo = geo && geo.length > 0; + const hasLocation = location && Object.entries(sanitise(location)).length > 0; // Determine Microformat type - if (location && location.name) { + if (hasLocation && location.name) { location.type = "card"; - } else if (location) { + } else if (hasLocation && !hasGeo) { location.type = "adr"; } // Add (or use) any provided geo location properties - if (location && geo) { + if (hasLocation && hasGeo) { location.geo = getGeoProperty(geo); - } else if (geo) { + } else if (hasGeo) { return getGeoProperty(geo); } diff --git a/packages/endpoint-posts/test/unit/utils.js b/packages/endpoint-posts/test/unit/utils.js index 5f2658259..b3331c53a 100644 --- a/packages/endpoint-posts/test/unit/utils.js +++ b/packages/endpoint-posts/test/unit/utils.js @@ -173,6 +173,20 @@ describe("endpoint-posts/lib/utils", () => { }); }); + it("Returns empty object if location property is empty", () => { + assert.deepEqual( + getLocationProperty({ + location: { + "street-address": "", + locality: "", + "postal-code": "", + }, + geo: "", + }), + {}, + ); + }); + it("Gets post name", () => { const post = { name: "My favourite sandwich" };