Skip to content

fix: normalize category elements with attributes to plain strings - #307

Open
pratik-desgn wants to merge 1 commit into
rbren:masterfrom
pratik-desgn:fix-category-attrs-normalization
Open

fix: normalize category elements with attributes to plain strings#307
pratik-desgn wants to merge 1 commit into
rbren:masterfrom
pratik-desgn:fix-category-attrs-normalization

Conversation

@pratik-desgn

Copy link
Copy Markdown

Fixes #289.

index.d.ts declares Item.categories as string[], but xml2js returns a {_, $} object for any <category> element that has attributes (e.g. domain="..."), since item.categories was being set directly from the raw xml2js output with no normalization:

if (xmlItem.category) item.categories = xmlItem.category;

For a feed like:

<category domain="changelog-type">Release</category>
<category domain="changelog-label">copilot</category>

categories came out as:

[
  { _: 'Release', '$': { domain: 'changelog-type' } },
  { _: 'copilot', '$': { domain: 'changelog-label' } }
]

instead of ['Release', 'copilot'] as declared.

Fix

Extract the text value the same way item.guid already does a few lines above:

if (xmlItem.category) {
  item.categories = xmlItem.category.map((category) => (category && category._) || category);
}

This normalizes both shapes (plain string categories and {_, $} categories-with-attributes) to plain strings, matching the declared type.

Testing

  • Added test/input/item-category-with-attrs.rss + golden output, reproducing the exact shape from the issue (mixed attributed/plain <category> tags) — confirmed this test fails on the old code with the exact reported shape, passes with the fix.
  • Regenerated the golden output for 2 pre-existing fixtures (heraldsun, guardian) that happened to already contain <category domain="..."> elements and had the buggy {_, $} shape baked into their snapshots — diffed to confirm the change is scoped to exactly the categories field, nothing else.
  • Full suite passes (37 tests).

index.d.ts declares Item.categories as string[], but xml2js returns
{_, $} objects for any <category> element that has attributes (e.g.
domain="..."), since item.categories was set directly from the raw
xml2js output with no normalization. Extract the text value the same
way item.guid already does, so categories is always string[]
regardless of whether the source XML's <category> tags carry
attributes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Item.categories not matching declared type

1 participant