Skip to content

Commit

Permalink
Array methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderOMara committed Dec 16, 2024
1 parent 82d7bdf commit d84d76f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 19 deletions.
59 changes: 50 additions & 9 deletions arr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,55 @@ Deno.test('array: Symbol.iterator', () => {
const data = new Uint8Array(Foo.BYTE_LENGTH * 3);
const foo4 = new Foo4(data.buffer, Foo.BYTE_LENGTH);

{
const list = [...foo4];
let i = 0;
for (const foo of foo4) {
assertStrictEquals(foo, foo4[i]);
assertStrictEquals(foo, list[i]);
i++;
}
const list = [...foo4];
let i = 0;
for (const foo of foo4) {
assertStrictEquals(foo, foo4[i]);
assertStrictEquals(foo, list[i]);
i++;
}
assertEquals(list.length, foo4.length);
});

Deno.test('array: entries', () => {
const data = new Uint8Array(Foo.BYTE_LENGTH * 3);
const foo4 = new Foo4(data.buffer, Foo.BYTE_LENGTH);

const list = [...foo4.entries()];
let i = 0;
for (const [k, v] of foo4.entries()) {
assertStrictEquals(k, list[i][0]);
assertStrictEquals(v, list[i][1]);
i++;
}
assertEquals(list.length, 4);
});

Deno.test('array: keys', () => {
const data = new Uint8Array(Foo.BYTE_LENGTH * 3);
const foo4 = new Foo4(data.buffer, Foo.BYTE_LENGTH);

const list = [...foo4.keys()];
let i = 0;
for (const foo of foo4.keys()) {
assertStrictEquals(foo, i);
i++;
}
assertEquals(list, [0, 1, 2, 3]);
});

Deno.test('array: values', () => {
const data = new Uint8Array(Foo.BYTE_LENGTH * 3);
const foo4 = new Foo4(data.buffer, Foo.BYTE_LENGTH);

const list = [...foo4.values()];
let i = 0;
for (const foo of foo4.values()) {
assertStrictEquals(foo, foo4[i]);
assertStrictEquals(foo, list[i]);
i++;
}
assertEquals(list.length, foo4.length);
});

Deno.test('array: at', () => {
Expand All @@ -111,7 +151,8 @@ Deno.test('array: at', () => {
[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10],
[Infinity, -Infinity, NaN, Number.EPSILON, -Number.EPSILON],
[1.1, 0.5, -1.1, -0.5, -0],
].flat()
['1', ''],
].flat() as number[]
) {
assertStrictEquals(four.at(index), jsar.at(index), `at(${index})`);
}
Expand Down
57 changes: 47 additions & 10 deletions arr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,24 @@ export interface Arr<T = never> extends Ptr<T>, Type {
at(i: number): T | undefined;

/**
* Array iterator.
* Value iterator.
*/
[Symbol.iterator](): Generator<T, undefined, unknown>;

/**
* Entry key-value pair iterator.
*/
entries(): Generator<[number, T], undefined, unknown>;

/**
* Key iterator.
*/
keys(): Generator<number, undefined, unknown>;

/**
* Value iterator.
*/
values(): Generator<T, undefined, unknown>;
}

/**
Expand Down Expand Up @@ -124,6 +139,22 @@ export function array<T extends Type>(
[name]: class extends Ptr implements Arr<T>, Members {
declare public readonly ['constructor']: ArrClass<Arr<T>>;

public get byteLength(): number {
return this.constructor.BYTE_LENGTH;
}

public get length(): number {
return length;
}

public at(i: number): T | undefined {
i ||= 0;
i -= i % 1;
if ((i < 0 ? i += length : i) >= 0 && i < length) {
return this[i];
}
}

public *[Symbol.iterator](): Generator<
T,
undefined,
Expand All @@ -134,19 +165,25 @@ export function array<T extends Type>(
}
}

public get byteLength(): number {
return this.constructor.BYTE_LENGTH;
public *entries(): Generator<
[number, T],
undefined,
unknown
> {
for (let i = 0; i < length; i++) {
yield [i, this[i]];
}
}

public get length(): number {
return length;
public *keys(): Generator<number, undefined, unknown> {
for (let i = 0; i < length; i++) {
yield i;
}
}

public at(i: number): T | undefined {
i ||= 0;
i -= i % 1;
if ((i < 0 ? i += length : 0) >= 0 && i < length) {
return this[i];
public *values(): Generator<T, undefined, unknown> {
for (let i = 0; i < length; i++) {
yield this[i];
}
}

Expand Down

0 comments on commit d84d76f

Please sign in to comment.