@gabrielrufino/cerebrum 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +16 -0
- package/.github/workflows/cd.yml +20 -0
- package/.github/workflows/ci.yml +18 -0
- package/.husky/commit-msg +1 -0
- package/LICENSE +24 -0
- package/README.md +1 -0
- package/commitlint.config.js +1 -0
- package/dist/Sort/BubbleSort.js +17 -0
- package/dist/index.js +17 -0
- package/package.json +35 -0
- package/src/Math/Fibonacci.spec.ts +39 -0
- package/src/Math/Fibonacci.ts +11 -0
- package/src/Math/LinearRegression.spec.ts +52 -0
- package/src/Math/LinearRegression.ts +49 -0
- package/src/Math/TwoSum.spec.ts +40 -0
- package/src/Math/TwoSum.ts +25 -0
- package/src/Sort/BubbleSort.spec.ts +40 -0
- package/src/Sort/BubbleSort.ts +19 -0
- package/src/Sort/SelectionSort.spec.ts +36 -0
- package/src/Sort/SelectionSort.ts +23 -0
- package/src/index.ts +4 -0
- package/tsconfig.json +110 -0
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
name: CD
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_run:
|
|
5
|
+
workflows: ['CI']
|
|
6
|
+
types:
|
|
7
|
+
- completed
|
|
8
|
+
branches:
|
|
9
|
+
- main
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
node-pkg-cd:
|
|
13
|
+
name: Node package CD
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- uses: gabrielrufino/check-ci@main
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
- uses: gabrielrufino/node-pkg-cd@develop
|
|
19
|
+
with:
|
|
20
|
+
node-auth-token: ${{ secrets.NODE_AUTH_TOKEN }}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
- pull_request
|
|
5
|
+
- push
|
|
6
|
+
- workflow_dispatch
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
node-ci:
|
|
10
|
+
name: Node CI
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
- uses: gabrielrufino/node-ci@v3
|
|
15
|
+
|
|
16
|
+
concurrency:
|
|
17
|
+
group: ${{ github.workflow }}-${{ github.sha }}
|
|
18
|
+
cancel-in-progress: true
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npx --no -- commitlint --edit $1
|
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Cerebrum
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default { extends: ['@commitlint/config-conventional'] };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BubbleSort = void 0;
|
|
4
|
+
class BubbleSort {
|
|
5
|
+
constructor() { }
|
|
6
|
+
static execute(numbers) {
|
|
7
|
+
for (let i = 0; i < numbers.length - 1; i++) {
|
|
8
|
+
for (let j = 0; j < numbers.length - i - 1; j++) {
|
|
9
|
+
if (numbers[j] > numbers[j + 1]) {
|
|
10
|
+
[numbers[j], numbers[j + 1]] = [numbers[j + 1], numbers[j]];
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return numbers;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.BubbleSort = BubbleSort;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./Sort/BubbleSort"), exports);
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gabrielrufino/cerebrum",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Algorithms made in TypeScript",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"lint": "eslint ./src/**.ts",
|
|
10
|
+
"lint:fix": "npm run lint -- --fix",
|
|
11
|
+
"test": "vitest run",
|
|
12
|
+
"test:cov": "npm test -- --coverage",
|
|
13
|
+
"test:watch": "vitest",
|
|
14
|
+
"prepare": "husky"
|
|
15
|
+
},
|
|
16
|
+
"author": "Gabriel Rufino <contato@gabrielrufino.com>",
|
|
17
|
+
"license": "UNLICENSED",
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@commitlint/cli": "^19.5.0",
|
|
20
|
+
"@commitlint/config-conventional": "^19.5.0",
|
|
21
|
+
"@faker-js/faker": "^8.4.1",
|
|
22
|
+
"@gabrielrufino/eslint-config": "^1.6.0",
|
|
23
|
+
"@vitest/coverage-v8": "^2.1.1",
|
|
24
|
+
"eslint": "^8.57.1",
|
|
25
|
+
"husky": "^9.1.6",
|
|
26
|
+
"typescript": "^5.6.2",
|
|
27
|
+
"vitest": "^2.1.0"
|
|
28
|
+
},
|
|
29
|
+
"funding": [
|
|
30
|
+
{
|
|
31
|
+
"type": "github",
|
|
32
|
+
"url": "https://github.com/sponsors/gabrielrufino"
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { Fibonacci } from './Fibonacci';
|
|
3
|
+
|
|
4
|
+
describe('Fibonacci', () => {
|
|
5
|
+
it('should throw a RangeError if n is less than 1', () => {
|
|
6
|
+
expect(() => new Fibonacci(0)).toThrow(RangeError);
|
|
7
|
+
expect(() => new Fibonacci(-1)).toThrow(RangeError);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('should return 1 for n = 1', () => {
|
|
11
|
+
const fib = new Fibonacci(1);
|
|
12
|
+
expect(fib.execute()).toBe(1);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should return 1 for n = 2', () => {
|
|
16
|
+
const fib = new Fibonacci(2);
|
|
17
|
+
expect(fib.execute()).toBe(1);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should return 2 for n = 3', () => {
|
|
21
|
+
const fib = new Fibonacci(3);
|
|
22
|
+
expect(fib.execute()).toBe(2);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should return 3 for n = 4', () => {
|
|
26
|
+
const fib = new Fibonacci(4);
|
|
27
|
+
expect(fib.execute()).toBe(3);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should return 5 for n = 5', () => {
|
|
31
|
+
const fib = new Fibonacci(5);
|
|
32
|
+
expect(fib.execute()).toBe(5);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should return 8 for n = 6', () => {
|
|
36
|
+
const fib = new Fibonacci(6);
|
|
37
|
+
expect(fib.execute()).toBe(8);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export class Fibonacci {
|
|
2
|
+
constructor (private readonly n: number) {
|
|
3
|
+
if (n < 1) throw new RangeError('Input should be a positive integer');
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
public execute (): number {
|
|
7
|
+
if ([1, 2].includes(this.n)) return 1
|
|
8
|
+
|
|
9
|
+
return new Fibonacci(this.n - 1).execute() + new Fibonacci(this.n - 2).execute()
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { LinearRegression } from './LinearRegression'
|
|
3
|
+
|
|
4
|
+
describe('LinearRegression', () => {
|
|
5
|
+
it('should calculate correct slope and intercept for basic input', () => {
|
|
6
|
+
const values = [
|
|
7
|
+
{ x: 1, y: 2 },
|
|
8
|
+
{ x: 2, y: 3 },
|
|
9
|
+
{ x: 3, y: 5 },
|
|
10
|
+
{ x: 4, y: 7 },
|
|
11
|
+
{ x: 5, y: 11 },
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
const regression = new LinearRegression(values)
|
|
15
|
+
.calculate()
|
|
16
|
+
|
|
17
|
+
expect(regression.slope).toBeCloseTo(2.2)
|
|
18
|
+
expect(regression.intercept).toBeCloseTo(-1)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('should predict the correct value after regression calculation', () => {
|
|
22
|
+
const values = [
|
|
23
|
+
{ x: 1, y: 2 },
|
|
24
|
+
{ x: 2, y: 3 },
|
|
25
|
+
{ x: 3, y: 5 },
|
|
26
|
+
{ x: 4, y: 7 },
|
|
27
|
+
{ x: 5, y: 11 },
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
const regression = new LinearRegression(values)
|
|
31
|
+
.calculate()
|
|
32
|
+
|
|
33
|
+
const prediction = regression.predict(6)
|
|
34
|
+
expect(prediction).toBeCloseTo(12.2)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('should throw error when trying to predict without calculating regression', () => {
|
|
38
|
+
const values = [
|
|
39
|
+
{ x: 1, y: 2 },
|
|
40
|
+
{ x: 2, y: 3 },
|
|
41
|
+
{ x: 3, y: 5 },
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
const regression = new LinearRegression(values)
|
|
45
|
+
|
|
46
|
+
expect(() => regression.predict(6)).toThrow('You need to calculate the regression before making predictions.')
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('should throw error if values array is empty', () => {
|
|
50
|
+
expect(() => new LinearRegression([])).toThrow('The input data must not be empty.')
|
|
51
|
+
})
|
|
52
|
+
})
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export class LinearRegression {
|
|
2
|
+
private _slope: number | null = null
|
|
3
|
+
private _intercept: number | null = null
|
|
4
|
+
|
|
5
|
+
constructor (private readonly values: Array<{ x: number, y: number }>) {
|
|
6
|
+
if (values.length === 0) {
|
|
7
|
+
throw new Error('The input data must not be empty.');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return this
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public calculate () {
|
|
14
|
+
const length = this.values.length
|
|
15
|
+
|
|
16
|
+
const sumX = this.values.reduce((acc, curr) => acc + curr.x, 0)
|
|
17
|
+
const sumY = this.values.reduce((acc, curr) => acc + curr.y, 0)
|
|
18
|
+
const sumXY = this.values.reduce((acc, curr) => acc + curr.x * curr.y, 0)
|
|
19
|
+
const sumXSquare = this.values.reduce((acc, curr) => acc + Math.pow(curr.x, 2), 0)
|
|
20
|
+
|
|
21
|
+
const denominator = length * sumXSquare - sumX * sumX;
|
|
22
|
+
|
|
23
|
+
if (denominator === 0) {
|
|
24
|
+
throw new Error('The input data points are collinear or insufficient for regression.');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
this._slope = (length * sumXY - sumX * sumY) / denominator
|
|
28
|
+
|
|
29
|
+
this._intercept = (sumY * sumXSquare - sumX * sumXY) / denominator
|
|
30
|
+
|
|
31
|
+
return this
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public predict(x: number): number {
|
|
35
|
+
if (this._slope === null || this._intercept === null) {
|
|
36
|
+
throw new Error('You need to calculate the regression before making predictions.')
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return this._slope * x + this._intercept
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
get slope(): number | null {
|
|
43
|
+
return this._slope
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get intercept(): number | null {
|
|
47
|
+
return this._intercept
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { TwoSum } from './TwoSum';
|
|
3
|
+
|
|
4
|
+
describe('TwoSum', () => {
|
|
5
|
+
it('should return indices of the two numbers that add up to the target', () => {
|
|
6
|
+
const twoSum = new TwoSum([1, 2, 3, 4, 5], 6);
|
|
7
|
+
const result = twoSum.execute();
|
|
8
|
+
expect(result).toEqual([0, 4]);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should return null if no pair of numbers add up to the target', () => {
|
|
12
|
+
const twoSum = new TwoSum([1, 2, 3, 4, 5], 10);
|
|
13
|
+
const result = twoSum.execute();
|
|
14
|
+
expect(result).toBeNull();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('should return indices of the first pair that adds up to the target', () => {
|
|
18
|
+
const twoSum = new TwoSum([1, 2, 3, 3, 4], 6);
|
|
19
|
+
const result = twoSum.execute();
|
|
20
|
+
expect(result).toEqual([1, 4]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should handle negative numbers', () => {
|
|
24
|
+
const twoSum = new TwoSum([-1, 0, 1, 2], 1);
|
|
25
|
+
const result = twoSum.execute();
|
|
26
|
+
expect(result).toEqual([0, 3]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should return null for an empty array', () => {
|
|
30
|
+
const twoSum = new TwoSum([], 1);
|
|
31
|
+
const result = twoSum.execute();
|
|
32
|
+
expect(result).toBeNull();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should return null if there is only one number', () => {
|
|
36
|
+
const twoSum = new TwoSum([1], 1);
|
|
37
|
+
const result = twoSum.execute();
|
|
38
|
+
expect(result).toBeNull();
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export class TwoSum {
|
|
2
|
+
constructor (
|
|
3
|
+
public readonly numbers: number[],
|
|
4
|
+
public readonly target: number
|
|
5
|
+
) {}
|
|
6
|
+
|
|
7
|
+
public execute (): [number, number] | null {
|
|
8
|
+
let left = 0;
|
|
9
|
+
let right = this.numbers.length - 1;
|
|
10
|
+
|
|
11
|
+
while (left < right) {
|
|
12
|
+
const sum = this.numbers[left] + this.numbers[right];
|
|
13
|
+
|
|
14
|
+
if (sum === this.target) {
|
|
15
|
+
return [left, right];
|
|
16
|
+
} else if (sum < this.target) {
|
|
17
|
+
left++;
|
|
18
|
+
} else {
|
|
19
|
+
right--;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { BubbleSort } from './BubbleSort';
|
|
3
|
+
|
|
4
|
+
describe('BubbleSort', () => {
|
|
5
|
+
it('should sort an unsorted array', () => {
|
|
6
|
+
const numbers = [64, 34, 25, 12, 22, 11, 90];
|
|
7
|
+
const sorted = new BubbleSort(numbers).execute();
|
|
8
|
+
expect(sorted).toEqual([11, 12, 22, 25, 34, 64, 90]);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should return the same array if already sorted', () => {
|
|
12
|
+
const numbers = [1, 2, 3, 4, 5];
|
|
13
|
+
const sorted = new BubbleSort(numbers).execute();
|
|
14
|
+
expect(sorted).toEqual([1, 2, 3, 4, 5]);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('should sort an array with duplicate numbers', () => {
|
|
18
|
+
const numbers = [5, 1, 4, 2, 1];
|
|
19
|
+
const sorted = new BubbleSort(numbers).execute();
|
|
20
|
+
expect(sorted).toEqual([1, 1, 2, 4, 5]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should handle an empty array', () => {
|
|
24
|
+
const numbers: number[] = [];
|
|
25
|
+
const sorted = new BubbleSort(numbers).execute();
|
|
26
|
+
expect(sorted).toEqual([]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should handle an array with a single element', () => {
|
|
30
|
+
const numbers = [42];
|
|
31
|
+
const sorted = new BubbleSort(numbers).execute();
|
|
32
|
+
expect(sorted).toEqual([42]);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should sort an array with negative numbers', () => {
|
|
36
|
+
const numbers = [-3, -1, -4, 2, 0];
|
|
37
|
+
const sorted = new BubbleSort(numbers).execute();
|
|
38
|
+
expect(sorted).toEqual([-4, -3, -1, 0, 2]);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export class BubbleSort {
|
|
2
|
+
constructor (
|
|
3
|
+
private readonly numbers: Array<number>
|
|
4
|
+
) {
|
|
5
|
+
return this
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
public execute(): Array<number> {
|
|
9
|
+
for (let i = 0; i < this.numbers.length - 1; i++) {
|
|
10
|
+
for (let j = 0; j < this.numbers.length - i - 1; j++) {
|
|
11
|
+
if (this.numbers[j] > this.numbers[j + 1]) {
|
|
12
|
+
[this.numbers[j], this.numbers[j + 1]] = [this.numbers[j + 1], this.numbers[j]];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return [...this.numbers];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { SelectionSort } from './SelectionSort';
|
|
3
|
+
|
|
4
|
+
describe('SelectionSort', () => {
|
|
5
|
+
it('should sort an array of numbers in ascending order', () => {
|
|
6
|
+
const unsorted = [64, 25, 12, 22, 11];
|
|
7
|
+
const sorted = [11, 12, 22, 25, 64];
|
|
8
|
+
expect(new SelectionSort(unsorted).execute()).toEqual(sorted);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should handle an empty array', () => {
|
|
12
|
+
const unsorted: number[] = [];
|
|
13
|
+
expect(new SelectionSort(unsorted).execute()).toEqual([]);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('should handle an array with one element', () => {
|
|
17
|
+
const unsorted = [42];
|
|
18
|
+
expect(new SelectionSort(unsorted).execute()).toEqual([42]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should handle an array with already sorted elements', () => {
|
|
22
|
+
const unsorted = [1, 2, 3, 4, 5];
|
|
23
|
+
expect(new SelectionSort(unsorted).execute()).toEqual([1, 2, 3, 4, 5]);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('should handle an array with identical elements', () => {
|
|
27
|
+
const unsorted = [5, 5, 5, 5, 5];
|
|
28
|
+
expect(new SelectionSort(unsorted).execute()).toEqual([5, 5, 5, 5, 5]);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should sort an array with negative numbers', () => {
|
|
32
|
+
const unsorted = [3, -1, -5, 2, 0];
|
|
33
|
+
const sorted = [-5, -1, 0, 2, 3];
|
|
34
|
+
expect(new SelectionSort(unsorted).execute()).toEqual(sorted);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export class SelectionSort {
|
|
2
|
+
constructor(
|
|
3
|
+
private readonly numbers: Array<number>
|
|
4
|
+
) {
|
|
5
|
+
return this
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
public execute(): Array<number> {
|
|
9
|
+
for (let i = 0; i < this.numbers.length - 1; i++) {
|
|
10
|
+
let minIndex = i;
|
|
11
|
+
|
|
12
|
+
for (let j = i + 1; j < this.numbers.length; j++) {
|
|
13
|
+
if (this.numbers[j] < this.numbers[minIndex]) {
|
|
14
|
+
minIndex = j;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
[this.numbers[i], this.numbers[minIndex]] = [this.numbers[minIndex], this.numbers[i]];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return [...this.numbers];
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/index.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
4
|
+
|
|
5
|
+
/* Projects */
|
|
6
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
7
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
8
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
9
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
10
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
+
|
|
13
|
+
/* Language and Environment */
|
|
14
|
+
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
15
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
|
+
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
|
18
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
20
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
22
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
23
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
26
|
+
|
|
27
|
+
/* Modules */
|
|
28
|
+
"module": "commonjs", /* Specify what module code is generated. */
|
|
29
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
30
|
+
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
31
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
32
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
33
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
34
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
35
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
36
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
37
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
38
|
+
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
|
|
39
|
+
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
|
|
40
|
+
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
|
|
41
|
+
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
|
|
42
|
+
// "noUncheckedSideEffectImports": true, /* Check side effect imports. */
|
|
43
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
44
|
+
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
|
|
45
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
46
|
+
|
|
47
|
+
/* JavaScript Support */
|
|
48
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
49
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
50
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
51
|
+
|
|
52
|
+
/* Emit */
|
|
53
|
+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
54
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
55
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
56
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
57
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
58
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
59
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
60
|
+
"outDir": "./dist", /* Specify an output folder for all emitted files. */
|
|
61
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
62
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
63
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
64
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
65
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
66
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
67
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
68
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
69
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
70
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
71
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
72
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
73
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
74
|
+
|
|
75
|
+
/* Interop Constraints */
|
|
76
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
77
|
+
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
|
78
|
+
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
|
|
79
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
80
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
|
81
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
82
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
83
|
+
|
|
84
|
+
/* Type Checking */
|
|
85
|
+
"strict": true, /* Enable all strict type-checking options. */
|
|
86
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
87
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
88
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
89
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
90
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
91
|
+
// "strictBuiltinIteratorReturn": true, /* Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'. */
|
|
92
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
93
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
94
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
95
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
96
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
97
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
98
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
99
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
100
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
101
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
102
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
103
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
104
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
105
|
+
|
|
106
|
+
/* Completeness */
|
|
107
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
108
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
109
|
+
},"exclude": ["**/*.spec.ts"]
|
|
110
|
+
}
|