From de696034f63efc39dc9a640f032162f02c726d0d Mon Sep 17 00:00:00 2001 From: Chris Millar Date: Thu, 14 Nov 2024 20:53:41 -0700 Subject: [PATCH] MWPW-161587 - Support typed query indexes * Supports type-safe query indexes Resolves: MWPW-161587 --- libs/blocks/article-feed/article-helpers.js | 18 ++++++++++----- .../article-feed/article-helpers.test.js | 22 +++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 test/blocks/article-feed/article-helpers.test.js diff --git a/libs/blocks/article-feed/article-helpers.js b/libs/blocks/article-feed/article-helpers.js index e9cfe1d59b..fcb2f3f81d 100644 --- a/libs/blocks/article-feed/article-helpers.js +++ b/libs/blocks/article-feed/article-helpers.js @@ -81,12 +81,18 @@ function loadArticleTaxonomy(article) { // for now, we can only compute the category const { tags, path } = clonedArticle; + let topics; + if (tags) { - const topics = tags - .replace(/[["\]]/gm, '') - .split(',') - .map((t) => t.trim()) - .filter((t) => t && t !== ''); + if (Array.isArray(tags)) { + topics = tags.map((t) => t.trim()).filter((t) => t && t !== ''); + } else { + topics = tags + .replace(/[["\]]/gm, '') + .split(',') + .map((t) => t.trim()) + .filter((t) => t && t !== ''); + } const articleTax = computeTaxonomyFromTopics(topics, path); @@ -175,7 +181,7 @@ export async function loadTaxonomy() { */ export function formatCardLocaleDate(date) { if (!date) return ''; - const jsDate = !date.includes('-') ? calculateExcelDate(date) : date.replace(/-/g, '/'); + const jsDate = !date.toString().includes('-') ? calculateExcelDate(date) : date.replace(/-/g, '/'); const dateLocale = getConfig().locale?.ietf; diff --git a/test/blocks/article-feed/article-helpers.test.js b/test/blocks/article-feed/article-helpers.test.js new file mode 100644 index 0000000000..8508d53e68 --- /dev/null +++ b/test/blocks/article-feed/article-helpers.test.js @@ -0,0 +1,22 @@ +import { expect } from '@esm-bundle/chai'; +import { getArticleTaxonomy } from '../../../libs/blocks/article-feed/article-helpers.js'; + +describe('adobetv autoblock', () => { + it('Creates article taxonomy from strings', async () => { + const article = { + tags: 'hello, world', + path: '/no/types/for/you', + }; + const taxonomy = getArticleTaxonomy(article); + expect(taxonomy.topics[0]).to.equal('hello'); + }); + + it('Creates article taxonomy from an array of strings', async () => { + const article = { + tags: ['goodnight', 'moon'], + path: '/no/types/for/you', + }; + const taxonomy = getArticleTaxonomy(article); + expect(taxonomy.topics[0]).to.equal('goodnight'); + }); +});