MCPcopy Create free account
hub / github.com/nodejs/undici / FastIterableIterator

Class FastIterableIterator

lib/web/fetch/util.js:736–835  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

734 */
735function createIterator (name, kInternalIterator, keyIndex = 0, valueIndex = 1) {
736 class FastIterableIterator {
737 /** @type {any} */
738 #target
739 /** @type {'key' | 'value' | 'key+value'} */
740 #kind
741 /** @type {number} */
742 #index
743
744 /**
745 * @see https://webidl.spec.whatwg.org/#dfn-default-iterator-object
746 * @param {unknown} target
747 * @param {'key' | 'value' | 'key+value'} kind
748 */
749 constructor (target, kind) {
750 this.#target = target
751 this.#kind = kind
752 this.#index = 0
753 }
754
755 next () {
756 // 1. Let interface be the interface for which the iterator prototype object exists.
757 // 2. Let thisValue be the this value.
758 // 3. Let object be ? ToObject(thisValue).
759 // 4. If object is a platform object, then perform a security
760 // check, passing:
761 // 5. If object is not a default iterator object for interface,
762 // then throw a TypeError.
763 if (typeof this !== 'object' || this === null || !(#target in this)) {
764 throw new TypeError(
765 `'next' called on an object that does not implement interface ${name} Iterator.`
766 )
767 }
768
769 // 6. Let index be object’s index.
770 // 7. Let kind be object’s kind.
771 // 8. Let values be object’s target's value pairs to iterate over.
772 const index = this.#index
773 const values = kInternalIterator(this.#target)
774
775 // 9. Let len be the length of values.
776 const len = values.length
777
778 // 10. If index is greater than or equal to len, then return
779 // CreateIterResultObject(undefined, true).
780 if (index >= len) {
781 return {
782 value: undefined,
783 done: true
784 }
785 }
786
787 // 11. Let pair be the entry in values at index index.
788 const { [keyIndex]: key, [valueIndex]: value } = values[index]
789
790 // 12. Set object’s index to index + 1.
791 this.#index = index + 1
792
793 // 13. Return the iterator result for pair and kind.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected