Skip to content

Commit

Permalink
added keys method (#692)
Browse files Browse the repository at this point in the history
  • Loading branch information
sh33dafi authored May 29, 2024
1 parent feb34b9 commit 808b529
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
17 changes: 17 additions & 0 deletions devs/run-tests/24arraymap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>): string {
const values:number[] = [];
for (const key of iterator) {
values.push(key)
}
return values.join();
}
}

testArraySome()
testArrayEvery()
testArrayFill()
Expand All @@ -219,3 +235,4 @@ testArrayIncludes()
testArrayAt()
testArrayWith()
testArraySort()
testArrayKeys()
5 changes: 5 additions & 0 deletions packages/core/src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ Array.prototype.sort = function <T>(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)
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/corelib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,11 @@ interface Array<T> {
* ```
*/
sort(compareFn?: (a: T, b: T) => number): this

/**
* Returns an iterable of keys in the array
*/
keys(): IterableIterator<number>;
}

interface ArrayConstructor {
Expand Down

0 comments on commit 808b529

Please sign in to comment.