Skip to content

Instantly share code, notes, and snippets.

@kouameYao
Created September 15, 2025 18:15
Show Gist options
  • Save kouameYao/f667873d6410a6c5d1348551b7e72240 to your computer and use it in GitHub Desktop.
Save kouameYao/f667873d6410a6c5d1348551b7e72240 to your computer and use it in GitHub Desktop.
Implement JS array usual mehtods
// MyArray.ts
export class MyArray<T> {
private items: Record<number, T> = {};
public length: number = 0;
constructor(initialItems?: T[]) {
if (initialItems) {
initialItems.forEach((item) => this.push(item));
}
}
push(element: T): number {
this.items[this.length] = element;
this.length++;
return this.length;
}
pop(): T | undefined {
if (this.length === 0) return undefined;
const last = this.items[this.length - 1];
delete this.items[this.length - 1];
this.length--;
return last;
}
shift(): T | undefined {
if (this.length === 0) return undefined;
const first = this.items[0];
for (let i = 0; i < this.length - 1; i++) {
this.items[i] = this.items[i + 1];
}
delete this.items[this.length - 1];
this.length--;
return first;
}
unshift(element: T): number {
for (let i = this.length; i > 0; i--) {
this.items[i] = this.items[i - 1];
}
this.items[0] = element;
this.length++;
return this.length;
}
forEach(callback: (element: T, index: number) => void): void {
for (let i = 0; i < this.length; i++) {
callback(this.items[i], i);
}
}
map<U>(callback: (element: T, index: number) => U): MyArray<U> {
const newArray = new MyArray<U>();
for (let i = 0; i < this.length; i++) {
newArray.push(callback(this.items[i], i));
}
return newArray;
}
filter(callback: (element: T, index: number) => boolean): MyArray<T> {
const newArray = new MyArray<T>();
for (let i = 0; i < this.length; i++) {
if (callback(this.items[i], i)) {
newArray.push(this.items[i]);
}
}
return newArray;
}
find(callback: (element: T, index: number) => boolean): T | undefined {
for (let i = 0; i < this.length; i++) {
if (callback(this.items[i], i)) return this.items[i];
}
return undefined;
}
findIndex(callback: (element: T, index: number) => boolean): number {
for (let i = 0; i < this.length; i++) {
if (callback(this.items[i], i)) return i;
}
return -1;
}
get(index: number): T | undefined {
return this.items[index];
}
}
// MyArray.test.ts
import { MyArray } from "./MyArray";
describe("MyArray<T>", () => {
let arr: MyArray<number>;
beforeEach(() => {
arr = new MyArray<number>();
});
test("push ajoute des éléments", () => {
expect(arr.push(10)).toBe(1);
expect(arr.push(20)).toBe(2);
expect(arr.get(0)).toBe(10);
expect(arr.get(1)).toBe(20);
});
test("pop supprime et retourne le dernier élément", () => {
arr.push(1);
arr.push(2);
expect(arr.pop()).toBe(2);
expect(arr.length).toBe(1);
});
test("shift supprime et retourne le premier élément", () => {
arr.push(1);
arr.push(2);
expect(arr.shift()).toBe(1);
expect(arr.length).toBe(1);
expect(arr.get(0)).toBe(2);
});
test("unshift ajoute au début", () => {
arr.push(2);
arr.unshift(1);
expect(arr.get(0)).toBe(1);
expect(arr.get(1)).toBe(2);
});
test("forEach parcourt tous les éléments", () => {
arr.push(1);
arr.push(2);
let sum = 0;
arr.forEach((x) => (sum += x));
expect(sum).toBe(3);
});
test("map retourne un nouveau tableau transformé", () => {
arr.push(1);
arr.push(2);
const newArr = arr.map((x) => x * 2);
expect(newArr.get(0)).toBe(2);
expect(newArr.get(1)).toBe(4);
});
test("filter retourne les éléments filtrés", () => {
arr.push(1);
arr.push(2);
arr.push(3);
const newArr = arr.filter((x) => x > 1);
expect(newArr.length).toBe(2);
expect(newArr.get(0)).toBe(2);
expect(newArr.get(1)).toBe(3);
});
describe("find & findIndex", () => {
beforeEach(() => {
arr.push(10);
arr.push(20);
arr.push(30);
});
test("find retourne le premier élément correspondant", () => {
expect(arr.find(x => x > 15)).toBe(20);
expect(arr.find(x => x === 100)).toBeUndefined();
});
test("findIndex retourne l'index du premier élément correspondant", () => {
expect(arr.findIndex(x => x > 15)).toBe(1);
expect(arr.findIndex(x => x === 100)).toBe(-1);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment