Skip to content
This repository was archived by the owner on Mar 27, 2022. It is now read-only.

Commit b9aeb98

Browse files
committed
Rewrite all
1 parent 42faf96 commit b9aeb98

16 files changed

+137
-254
lines changed

__tests__/binarySearch.spec.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

__tests__/insertion.spec.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
import { SortedArray } from '../src/SortedArray';
2-
import { defaultComparator } from "../src/helpers";
1+
import { insert, numberCmp } from '../src';
32

4-
describe('Insertion in sorted array', () => {
5-
const sortedArray = new SortedArray([1, 3, 4], defaultComparator);
3+
describe('Insertion', () => {
4+
it('should insert new element', () => {
5+
const array = [0, 1, 3, 4];
6+
const EXPECTED = [0, 1, 2, 3, 4];
67

7-
it('', () => {
8-
const EXPECTED_INDEX = 1;
9-
const index = sortedArray.insert(2);
8+
insert(array, 2, numberCmp);
109

11-
expect(index).toBe(EXPECTED_INDEX);
10+
expect(array).toEqual(EXPECTED);
1211
})
13-
})
12+
13+
it('correct insert in first and last position', () => {
14+
const array = [1, 2, 3];
15+
const EXPECTED = [0, 1, 2, 3, 4];
16+
17+
insert(array, 0, numberCmp);
18+
insert(array, 4, numberCmp);
19+
20+
expect(array).toEqual(EXPECTED);
21+
})
22+
})

__tests__/searching.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { search, numberCmp } from '../src';
2+
3+
describe('Searching', () => {
4+
it('should return index of element', () => {
5+
const array = [0, 1, 2, 3, 4, 5];
6+
const EXPECTED = 3;
7+
8+
const index = search(array, 3, numberCmp);
9+
10+
expect(index).toBe(EXPECTED);
11+
})
12+
13+
it('should return -1 if element is not found', () => {
14+
const array = [0, 1, 2, 3, 4, 5];
15+
const EXPECTED = -1;
16+
17+
const index = search(array, 10, numberCmp);
18+
19+
expect(index).toBe(EXPECTED);
20+
})
21+
})

__tests__/sorting.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { sort, numberCmp } from '../src';
2+
3+
describe('Sorting', () => {
4+
it('should sort array of numbers', () => {
5+
const array = [ 2, 5, 1, 0, 4, 3, 6 ];
6+
const EXPECTED = [ 0, 1, 2, 3, 4, 5, 6 ];
7+
8+
const sortedArray = sort(array, numberCmp);
9+
10+
expect(sortedArray).toEqual(EXPECTED);
11+
})
12+
})

src/SortedArray.ts

Lines changed: 0 additions & 152 deletions
This file was deleted.

src/cmp.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Comparator } from './types';
2+
3+
export const numberCmp: Comparator<number> = (a, b) => {
4+
if (a === b) {
5+
return 0;
6+
}
7+
8+
return a < b ? -1 : 1;
9+
}

src/helpers/binarySearch.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/helpers/defaultComparator.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/helpers/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export { sort } from './sort';
2+
export { insert } from './insert';
3+
export { search } from './search';
4+
export { numberCmp } from './cmp';
5+
export type { Comparator } from './types';

0 commit comments

Comments
 (0)