@web3r/flowerkit
Version:
Tree-shakable JavaScript and TypeScript utility library for frontend/browser apps: DOM, events, arrays, objects, strings, date, JSON, and network helpers (ESM/CJS, SSR-friendly).
1 lines • 1.43 kB
Source Map (JSON)
{"version":3,"file":"index.cjs","sources":[""],"sourcesContent":["export type TGetIndexOfElArgs = Parameters<typeof getIndexOfEl>;\r\n\r\nexport type TGetIndexOfElReturn = ReturnType<typeof getIndexOfEl>;\r\n\r\n/**\r\n * Gets index of Node from relatively its siblings\r\n * @param el{HTMLElement|Node|Element|Document} DOM element\r\n * @returns {number}\r\n * @throws {TypeError} getIndexOfEl: el must be a Node/Element\r\n * @example\r\n * // <ul>\r\n * // <li id=\"a\">A</li>\r\n * // Text node\r\n * // <li id=\"b\">B</li>\r\n * // <li id=\"c\">C</li>\r\n * // </ul>\r\n * getIndexOfEl(document.querySelector(\"#c\")!) // 2\r\n */\r\nexport const getIndexOfEl = (el: HTMLElement | Node | Element | Document): number => {\r\n if (!el || typeof (el as any).parentNode === \"undefined\") {\r\n throw new TypeError(\"getIndexOfEl: el must be a Node/Element\");\r\n }\r\n const parent = (el as any).parentNode as (ParentNode & { children?: HTMLCollection; }) | null;\r\n if (parent && parent.children) {\r\n return Array.from(parent.children).indexOf(el as Element);\r\n } else {\r\n return 0;\r\n }\r\n};\r\n"],"names":["getIndexOfEl","el","parentNode","TypeError","parent","children","Array","from","indexOf"],"mappings":";;;;;;;;;;;;;;GAkBO,MAAMA,aAAgBC,KAC3B,IAAKA,WAAcA,GAAWC,aAAe,YAC3C,MAAM,IAAIC,UAAU,2CAEtB,MAAMC,OAAUH,GAAWC,WAC3B,GAAIE,QAAUA,OAAOC,SACnB,OAAOC,MAAMC,KAAKH,OAAOC,UAAUG,QAAQP,SAE3C,OAAO"}