diff --git a/devs/run-tests/24arraymap.ts b/devs/run-tests/24arraymap.ts index eaca4e203a..38041fcff5 100644 --- a/devs/run-tests/24arraymap.ts +++ b/devs/run-tests/24arraymap.ts @@ -204,6 +204,22 @@ function testArraySort() { assert(mixed.join() === "2,5,9,apple,banana,cherry", "sort5") } +function testArrayKeys() { + const sparseArray = ['a', undefined , 'c'] + const array = ["a", "b", "c"] + + assert(joinIterable(array.keys()) === '0,1,2', "arrayKeys") + assert(joinIterable(sparseArray.keys()) === '0,1,2', "sparseArrayKeys") + + function joinIterable(iterator: IterableIterator): string { + const values:number[] = []; + for (const key of iterator) { + values.push(key) + } + return values.join(); + } +} + testArraySome() testArrayEvery() testArrayFill() @@ -219,3 +235,4 @@ testArrayIncludes() testArrayAt() testArrayWith() testArraySort() +testArrayKeys() diff --git a/packages/core/src/array.ts b/packages/core/src/array.ts index 0af8c3c208..0f6510eef7 100644 --- a/packages/core/src/array.ts +++ b/packages/core/src/array.ts @@ -210,6 +210,11 @@ Array.prototype.sort = function (compareFn?: (a: T, b: T) => number) { return this } +Array.prototype.keys = function () { + console.log ('KEYS', this.length); + return Array(this.length).fill(0).map((_, i) => i); +} + Buffer.prototype.set = function (other: Buffer, trgOff?: number) { if (!trgOff) trgOff = 0 this.blitAt(trgOff, other, 0, other.length) diff --git a/packages/core/src/corelib.d.ts b/packages/core/src/corelib.d.ts index 09a94a2b29..7b8e0881c9 100644 --- a/packages/core/src/corelib.d.ts +++ b/packages/core/src/corelib.d.ts @@ -430,6 +430,11 @@ interface Array { * ``` */ sort(compareFn?: (a: T, b: T) => number): this + + /** + * Returns an iterable of keys in the array + */ + keys(): IterableIterator; } interface ArrayConstructor {