Here's an example which adds an isLastSibling
method for checking if an element is the last sibling.
var cheerio = require('cheerio'),
$ = cheerio.load('<p><a>1</a><b>2</b><i>3</i></p>'),
$fn = Object.getPrototypeOf($());
$fn.isLastSibling = function() {
return this.parent().children().last()[0] === this[0];
};
console.log(
$('a').isLastSibling(),
$('b').isLastSibling(),
$('i').isLastSibling()
);
The output you should get is false false true
because the <a>
and <b>
elements are not the last siblings but the <i>
element is.