Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added keys method #692

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading