question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Normalizing the public API

See original GitHub issue

The project has two notions of “static methods”:

  1. Methods defined on the Cheerio constructor function. There are six in total:
    • 3 mimic methods defined on the global function defined by the jQuery project: .contains(), .merge(), and .parseHTML()
    • 3 are specific to Cheerio, exposed to address concerns that are not relevant in the browser context .html() and .xml(), .root()
  2. Methods defined on the module’s exported value (e.g. the value returned from require('cheerio')). There are two of these: .load() and .text().

As of today, however, the source does not reflect this distinction. All eight methods are defined in both places.

The exported value is itself the constructor for the Cheerio object. We have never documented the direct use of this constructor. As far as I remember, we have always instructed folks to use it indirectly, after being “bound” by the load method (as trying to use it directly would involve re-implementing the load method).

These details make the API difficult to document, and they may cause confusion for developers who are new to the project. (The methods in the first set don’t make sense in the context of the Cheerio module itself, and the methods in the second set don’t apply to a Cheerio function that has been “bound” to a document.) Because we are preparing for a major release, I would like to discuss taking the opportunity to make a breaking change:

  1. Export an ordinary object value (not a function)
  2. Re-implement exports.html() and exports.xml() to operate in this context (rendering the “outer” markup of a “selection” defined by a provided Cheerio object)
  3. Persist the following exported values: exports.load(), exports.text(), and exports.version
  4. Remove the Cheerio.load() and Cheerio.text() methods from the Cheerio constructor function

#2 directly contradicts the documentation on rendering, but the upgrade path is fairly straightforward:

  • $.html('.pear') becomes cheerio.html($('.pear'))
  • $.html($('.pear')) becomes cheerio.html($('.pear'))
  • $.html() becomes cheerio.html($.root())

I think this is warranted because it makes the API easier to understand/document and because it further reduces disparities between “Cheerio objects” and “jQuery objects”.

@fb55 @matthewmueller does this sound good to you? And are there any consumers out there that can share a use case which would be prevented by the change I’m proposing?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
matthewmuellercommented, Dec 28, 2017

Would it be possible to upgrade this without breaking anything? I agree with these changes and given that we’re still in 0.x, I’m open to this change.

I do think it’d be better if we can avoid breaking anything, alias these functions and update the docs with the improved API.

Lots of users these days – over 5 million downloads per month (!!). For better or for worse, there’s going to be some package.json’s with { "cheerio": "*" } 🙃

0reactions
jugglinmikecommented, Oct 20, 2018

We just completed step 3 via gh-1232, so it’s safe to call this issue resolved 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Normalizing JSON Data from REST APIs - InsertAfter
Normalizing JSON Data from REST APIs ... I always wanted to write down this post on how I design JSON data on the...
Read more >
Normalizing API Responses - DEV Community ‍ ‍
Express.js has been my goto framework for building an API in node. One of the problems I commonly run into is that with...
Read more >
Entity and Data Normalization - Rest Hooks
Extracting entities from a response is known as normalization . Accessing a response reverses the process via denormalization . Global ...
Read more >
Hierarchical REST API vs Normalized REST API
Is there terminology for the Hierarchical REST API and Normalized REST API? No. Which one is more fit to the REST API guideline?...
Read more >
Normalizing Data from Multiple APIs in GraphQL - YouTube
Learn you might normalize data from multiple apis in GraphQL.Code: https://github.com/benawad/graphql-multiple- apis ----If you like cooking, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found