ksoup icon indicating copy to clipboard operation
ksoup copied to clipboard

Add Ability to Limit the Number of Elements in Selector

Open itboy87 opened this issue 1 year ago • 1 comments

It would be useful to introduce a feature that allows the user to limit the number of elements returned by a selector. For cases where a user is only interested in a subset of elements (e.g., the first 10 matching elements), having an option to specify a limit would enhance performance and flexibility, especially when working with large datasets or documents.

Proposed API/Usage Example

val document = ksoup.parse(html)
val limitedElements = document.select("div.className", limit = 10) // Return only the first 10 matching elements

itboy87 avatar Oct 11 '24 03:10 itboy87

Maybe it could be another function that returns lazy Sequence?

fun Document.selectSequence(query: String): Sequence<Element>

This way performance and flexibility could be achieve with nicer API. I think that document.selectSequence("p")[10] looks better than document.select("p", limit = 11)[10]. Also document.selectSequence("p").take(10) looks good to me

burnoo avatar Oct 24 '24 17:10 burnoo