Skip to content

Commit 474d76c

Browse files
fix: linter errors
1 parent 4bd62a8 commit 474d76c

23 files changed

+896
-877
lines changed

.eslintignore

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
import { describe, expect, test } from 'vitest';
2-
3-
import { myArray } from '../arrayClass';
4-
5-
describe('Array Class Implementation', () => {
6-
test('new empty array is created', () => {
7-
const array = new myArray();
8-
expect(array.isEmpty()).toBe(true);
9-
});
10-
11-
test('create new array with 5 elements', () => {
12-
const array = new myArray(1, 2, 3, 4, 5);
13-
expect(array.size()).toEqual(5);
14-
});
15-
16-
test('get value from a given index in array', () => {
17-
const array = new myArray(
18-
{ id: 1, name: 'Francis' },
19-
{ id: 2, name: 'Carole' },
20-
);
21-
expect(array.get(1)).toEqual({ id: 2, name: 'Carole' });
22-
});
23-
24-
test('push value to end of array', () => {
25-
const array = new myArray();
26-
expect(array.push('Hello')).toBe(1);
27-
expect(array.push('there')).toBe(2);
28-
});
29-
30-
test('remove last item in array', () => {
31-
const array = new myArray(
32-
['name', 'Francis'],
33-
['name', 'Erika'],
34-
['name', 'Peter'],
35-
);
36-
expect(array.pop()).toEqual(['name', 'Peter']);
37-
});
38-
39-
test('delete item at a given index of array', () => {
40-
const array = new myArray(
41-
{ id: 1, name: 'Francis' },
42-
{ id: 2, name: 'Carole' },
43-
{ id: 3, name: 'Peter' },
44-
{ id: 4, name: 'Erika' },
45-
);
46-
expect(array.delete(2)).toEqual({ id: 3, name: 'Peter' });
47-
});
48-
});
1+
import { describe, expect, test } from 'vitest';
2+
3+
import { myArray } from '../arrayClass';
4+
5+
describe('Array Class Implementation', () => {
6+
test('new empty array is created', () => {
7+
const array = new myArray();
8+
expect(array.isEmpty()).toBe(true);
9+
});
10+
11+
test('create new array with 5 elements', () => {
12+
const array = new myArray(1, 2, 3, 4, 5);
13+
expect(array.size()).toEqual(5);
14+
});
15+
16+
test('get value from a given index in array', () => {
17+
const array = new myArray(
18+
{ id: 1, name: 'Francis' },
19+
{ id: 2, name: 'Carole' },
20+
);
21+
expect(array.get(1)).toEqual({ id: 2, name: 'Carole' });
22+
});
23+
24+
test('push value to end of array', () => {
25+
const array = new myArray();
26+
expect(array.push('Hello')).toBe(1);
27+
expect(array.push('there')).toBe(2);
28+
});
29+
30+
test('remove last item in array', () => {
31+
const array = new myArray(
32+
['name', 'Francis'],
33+
['name', 'Erika'],
34+
['name', 'Peter'],
35+
);
36+
expect(array.pop()).toEqual(['name', 'Peter']);
37+
});
38+
39+
test('delete item at a given index of array', () => {
40+
const array = new myArray(
41+
{ id: 1, name: 'Francis' },
42+
{ id: 2, name: 'Carole' },
43+
{ id: 3, name: 'Peter' },
44+
{ id: 4, name: 'Erika' },
45+
);
46+
expect(array.delete(2)).toEqual({ id: 3, name: 'Peter' });
47+
});
48+
});
+64-64
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
1-
export class myArray<T> {
2-
private length: number;
3-
private data: Record<string, T>;
4-
5-
public constructor(...args: T[]) {
6-
this.length = args.length;
7-
this.data = {};
8-
for (const key in args) {
9-
this.data[key] = args[key];
10-
}
11-
}
12-
13-
public size(): number {
14-
return this.length;
15-
}
16-
17-
public isEmpty(): boolean {
18-
return this.length === 0;
19-
}
20-
21-
public get(index: number): T {
22-
return this.data[index];
23-
}
24-
25-
public push(item: T): number {
26-
this.data[this.length] = item;
27-
this.length++;
28-
return this.length;
29-
}
30-
31-
public pop(): T {
32-
const lastItem = this.data[this.length - 1];
33-
delete this.data[this.length - 1];
34-
this.length--;
35-
return lastItem;
36-
}
37-
38-
public insert(index: number, item: T): object {
39-
for (let i = this.length; i >= index; i--) {
40-
this.data[i] = this.data[i - 1];
41-
}
42-
this.data[index] = item;
43-
this.length++;
44-
return this.data;
45-
}
46-
47-
public prepend(item: T): object {
48-
return this.insert(0, item);
49-
}
50-
51-
public delete(index: number): T {
52-
const item = this.data[index];
53-
this.shiftItems(index);
54-
return item;
55-
}
56-
57-
private shiftItems(index: number): void {
58-
for (let i = index; i < this.length - 1; i++) {
59-
this.data[i] = this.data[i + 1];
60-
}
61-
delete this.data[this.length - 1];
62-
this.length--;
63-
}
64-
}
1+
export class myArray<T> {
2+
private length: number;
3+
private data: Record<string, T>;
4+
5+
public constructor(...args: T[]) {
6+
this.length = args.length;
7+
this.data = {};
8+
for (const key in args) {
9+
this.data[key] = args[key];
10+
}
11+
}
12+
13+
public size(): number {
14+
return this.length;
15+
}
16+
17+
public isEmpty(): boolean {
18+
return this.length === 0;
19+
}
20+
21+
public get(index: number): T {
22+
return this.data[index];
23+
}
24+
25+
public push(item: T): number {
26+
this.data[this.length] = item;
27+
this.length++;
28+
return this.length;
29+
}
30+
31+
public pop(): T {
32+
const lastItem = this.data[this.length - 1];
33+
delete this.data[this.length - 1];
34+
this.length--;
35+
return lastItem;
36+
}
37+
38+
public insert(index: number, item: T): object {
39+
for (let i = this.length; i >= index; i--) {
40+
this.data[i] = this.data[i - 1];
41+
}
42+
this.data[index] = item;
43+
this.length++;
44+
return this.data;
45+
}
46+
47+
public prepend(item: T): object {
48+
return this.insert(0, item);
49+
}
50+
51+
public delete(index: number): T {
52+
const item = this.data[index];
53+
this.shiftItems(index);
54+
return item;
55+
}
56+
57+
private shiftItems(index: number): void {
58+
for (let i = index; i < this.length - 1; i++) {
59+
this.data[i] = this.data[i + 1];
60+
}
61+
delete this.data[this.length - 1];
62+
this.length--;
63+
}
64+
}

0 commit comments

Comments
 (0)