Socket
Book a DemoInstallSign in
Socket

cheerio-eq

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cheerio-eq

Add :eq() selector functionality to cheerio

latest
Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
53
6%
Maintainers
1
Weekly downloads
 
Created
Source

cheerio-eq

Add :eq() selector functionality to cheerio.

build status

If you are looking for a more advanced solution with support for multiple advanved selectors, check out the cheerio-advanced-selectors module.

What's the problem?

Cheerio sacrifices advanced CSS selector support for speed. This means for instance that the :eq() selector isn't supported. The work-around is normally to use the .eq() function instead:

// this will not work:
$('div:eq(1)');

// use this instead:
$('div').eq(1);

This is a good alternative if you write the CSS selectors from scrach, but what if you are working with selectors that already contain :eq()? Don't fear, cheerio-eq is here :)

Solution

The solution to the problem is to automatically parse the selector string at run-time. So if you give cheerio-eq a selector like div:eq(1) it will return the following cheerio cursor: $('div').eq(1).

It also works for complex selectors, so that div:eq(1) h2:eq(0) span will be converted and interpreted as $('div').eq(1).find('h2').eq(0).find('span').

Installation

npm install cheerio-eq

Usage

var cheerio = require('cheerio');
var find = require('cheerio-eq');

var html = '<div>foo</div><div>bar</div>';
var selector = 'div:eq(1)';

var $ = cheerio.load(html);

console.log(find($, selector).text()); // => 'bar'

API

The cheerio-eq module exposes a single function, which takes 2 arguments:

  • The cheerio DOM object (usually just named $)
  • A string containing the selector to parse

License

MIT

Keywords

cheerio

FAQs

Package last updated on 29 Jun 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts