MCPcopy Create free account
hub / github.com/estools/esquery / nthChild

Function nthChild

esquery.js:505–520  ·  view source on GitHub ↗

* Determines if the given node is the `nth` child. * If `nth` is negative then the position is counted * from the end of the list of children. * @param {external:AST} node * @param {external:AST[]} ancestry * @param {Integer} nth * @param {ESQueryOptions|undefined} options * @returns {boolean

(node, ancestry, nth, options)

Source from the content-addressed store, hash-verified

503 * @returns {boolean}
504 */
505function nthChild(node, ancestry, nth, options) {
506 if (nth === 0) { return false; }
507 const [parent] = ancestry;
508 if (!parent) { return false; }
509 const keys = getVisitorKeys(parent, options);
510 for (let i = 0; i < keys.length; ++i) {
511 const listProp = parent[keys[i]];
512 if (Array.isArray(listProp)){
513 const idx = nth < 0 ? listProp.length + nth : nth - 1;
514 if (idx >= 0 && idx < listProp.length && listProp[idx] === node) {
515 return true;
516 }
517 }
518 }
519 return false;
520}
521
522/**
523 * For each selector node marked as a subject, find the portion of the

Callers 1

generateMatcherFunction · 0.85

Calls 1

getVisitorKeysFunction · 0.85

Tested by

no test coverage detected