Skip to content

fix(helpers): JSON+LD detects number pproperly #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2021
Merged

fix(helpers): JSON+LD detects number pproperly #409

merged 2 commits into from
May 20, 2021

Conversation

kristianeaw
Copy link
Contributor

@kristianeaw kristianeaw commented May 20, 2021

lodash isEmpty considers numbers (and booleans) an empty value (https://lodash.com/docs/4.17.10#isEmpty), so it is not possible to scape numbers properly.

For example if you try to scrape prices with the following rules, and 'price' has a value (for example 25.5), but 'offers.price' and '0.offers.price' does not, it will loop though the remaining fields and you end up with no value.

price: [
    ({htmlDom: $, url}: type) => $jsonld('price')($, url),
    ({htmlDom: $, url}: type) => $jsonld('offers.price')($, url),
    ({htmlDom: $, url}: type) => $jsonld('0.offers.price')($, url),
]

Wasn't sure if I should file an issue or a PR, or if this behavior is intended for some reason, but would love to see a fix/workaround for this 😊

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
@Kikobeats
Copy link
Member

Kikobeats commented May 20, 2021

Hey, thanks for that.

Can you tell me a JSON+LD structure for creating a unit test there?

Just to be sure the these fails under the current codebase and it passes green after the change 🙂

@Kikobeats Kikobeats changed the title Fix that jsonld does not define numbers as a non empty value fix(helpers): JSON+LD detects number pproperly May 20, 2021
@Kikobeats
Copy link
Member

This could be the test

describe('.$jsonld', function () {
    it('detect numbers', () => {
      const $ = cheerio.load(`
      <script type="application/ld+json">{ "offers": { "price": 119.99 }}</script>
      <script type="application/ld+json">{ "price": { "price": "" }}</script>
      `)
      should($jsonld('offers.price')($)).be.equal('119.99')
    })
  })

Let me add it and merge this PR; thanks for it 🙂

@Kikobeats Kikobeats merged commit 8f4a174 into microlinkhq:master May 20, 2021
@kristianeaw
Copy link
Contributor Author

Thanks so much for getting to this so quickly! 😄

@Kikobeats
Copy link
Member

released at v5.21.9 🎉

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.

None yet

2 participants