@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.62 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":[""],"sourcesContent":["export type TGetLastFromIterableArgs = Parameters<typeof getLastFromIterable>;\r\n\r\nexport type TGetLastFromIterableReturn = ReturnType<typeof getLastFromIterable>;\r\n\r\n/**\r\n * Gets the last element of an iterable object such as Array, NodeList, HTMLCollection, etc.\r\n * @template T\r\n * @param {ArrayLike<T> & Iterable<T>} obj\r\n * @returns {T|null}\r\n * @throws {TypeError} getLastFromIterable: obj must be iterable with a numeric length\r\n * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols\r\n * @example\r\n * // How to get the last element from `NodeList` of `div`?\r\n * const lastDiv = getLastFromIterable(document.querySelectorAll(\"div\"));\r\n * console.log(lastDiv) // => Node or null\r\n */\r\nexport const getLastFromIterable = <T>(obj: ArrayLike<T> & Iterable<T>): T | null => {\r\n const hasLen = obj && typeof (obj as any).length === \"number\";\r\n const hasIter = obj && typeof (obj as any)[Symbol.iterator] === \"function\";\r\n if (!hasLen || !hasIter) {\r\n throw new TypeError(\"getLastFromIterable: obj must be iterable with a numeric length\");\r\n }\r\n const { length } = obj;\r\n return length ? (obj as any)[length - 1] : null;\r\n};\r\n"],"names":["getLastFromIterable","obj","hasLen","length","hasIter","Symbol","iterator","TypeError"],"mappings":";;;;;;;;;;;;AAgBO,MAAMA,oBAA0BC,MACrC,MAAMC,OAASD,YAAeA,IAAYE,SAAW,SACrD,MAAMC,QAAUH,YAAeA,IAAYI,OAAOC,YAAc,WAChE,IAAKJ,SAAWE,QACd,MAAM,IAAIG,UAAU,mEAEtB,MAAMJ,OAAEA,QAAWF,IACnB,OAAOE,OAAUF,IAAYE,OAAS,GAAK"}