Skip to content

Commit

Permalink
Merge pull request #11 from aliaks-ei/migrate_to_typescript
Browse files Browse the repository at this point in the history
Migrate repo to Typescript
  • Loading branch information
aliaks-ei authored Aug 21, 2023
2 parents 053ba32 + c9f5a31 commit a6a3a62
Show file tree
Hide file tree
Showing 75 changed files with 3,721 additions and 3,795 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/editorconfig-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
node-version: '16'
- run: npm ci
- run: npm run editorconfig:check
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
node-version: '16'
- run: npm ci
- run: npm run test:unit
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts?$': 'ts-jest',
},
transformIgnorePatterns: ['<rootDir>/node_modules/'],
};
6,016 changes: 2,991 additions & 3,025 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "js-algorithms",
"version": "1.0.0",
"version": "1.1.0",
"description": "A collection of the most famous algorithms implemented in Javascript",
"author": "Alexey Mozheyko",
"author": "Aliaksei Mazheika",
"scripts": {
"test:unit": "jest --silent",
"test:unit": "jest",
"editorconfig:check": "editorconfig-checker --exclude '.git|node_modules|.DS_store'"
},
"devDependencies": {
"@types/jest": "^29.5.3",
"editorconfig-checker": "^4.0.2",
"jest": "^27.0.6"
"ts-jest": "^29.1.1",
"typescript": "^5.1.6"
}
}
4 changes: 2 additions & 2 deletions src/binary-search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ Binary search compares the target value to the middle element of the array. If t

## Implementation

**_binarySearch(numArray, key)_** should return **true** if ```key``` is present in ```numArray``` and **false** otherwise.
`binarySearch(numArray, key)` should return `true` if `key` is present in `numArray` and `false` otherwise.

For example:
Example:

```
binarySearch([5, 7, 12, 16, 36, 56, 71], 56) // true
Expand Down
28 changes: 0 additions & 28 deletions src/binary-search/binary-search.spec.js

This file was deleted.

23 changes: 23 additions & 0 deletions src/binary-search/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import binarySearch from ".";

describe("Binary search: ", () => {
test("should return true (array contains key)", () => {
const numArray = [5, 7, 12, 16, 36, 56, 71];
const key = 56;

expect(binarySearch(numArray, key)).toBeTruthy();
});

test("should return false (array does not contains key)", () => {
const numArray = [5, 7, 12, 16, 36, 56, 71];
const key = 99;

expect(binarySearch(numArray, key)).toBeFalsy();
});

test("should return false (empty array passed)", () => {
const key = -33;

expect(binarySearch([], key)).toBeFalsy();
});
});
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
function binarySearch(numArray = [], key) {
let numArrLength = numArray.length;
function binarySearch(numArray: number[] = [], key: number): boolean {
const numArrLength = numArray.length;

if (!numArrLength) {
return false;
}

let middleIndex = Math.floor(numArrLength / 2);
let middleElem = numArray[middleIndex];
const middleIndex = Math.floor(numArrLength / 2);
const middleElem = numArray[middleIndex];

if (key < middleElem && numArrLength > 1) {
let leftHalf = numArray.slice(0, middleIndex);

return binarySearch(leftHalf, key);
}
else if (key > middleElem && numArrLength > 1) {
} else if (key > middleElem && numArrLength > 1) {
let rightHalf = numArray.slice(middleIndex);

return binarySearch(rightHalf, key);
}
else {
} else {
return key === middleElem;
}
}

module.exports = binarySearch;
export default binarySearch;
4 changes: 2 additions & 2 deletions src/bubble-sort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ The pass through the list is repeated until the list is sorted. The algorithm, w

## Implementation

**_bubbleSort(array)_** should return the origin ```array```, sorted with _bubble sort_.
`bubbleSort(array)` should return the origin `array`, sorted with _bubble sort_.

For example:
Example:

```
bubbleSort([5, 3, 8, 2, 1, 4]) // [1, 2, 3, 4, 5, 8]
Expand Down
21 changes: 0 additions & 21 deletions src/bubble-sort/bubble-sort.spec.js

This file was deleted.

17 changes: 17 additions & 0 deletions src/bubble-sort/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import bubbleSort from "./";

describe("Bubble sort: ", () => {
test("should return sorted array (only positive numbers)", () => {
const rawArray = [5, 3, 8, 2, 1, 4];
const sortedArray = [1, 2, 3, 4, 5, 8];

expect(bubbleSort(rawArray)).toEqual(sortedArray);
});

test("should return sorted array (positive and negative numbers)", () => {
const rawArray = [-7, 13, 4, 1e8, 0, -15];
const sortedArray = [-15, -7, 0, 4, 13, 1e8];

expect(bubbleSort(rawArray)).toEqual(sortedArray);
});
});
10 changes: 4 additions & 6 deletions src/bubble-sort/bubble-sort.js → src/bubble-sort/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
function bubbleSort(array = []) {
let arrayLength = array.length;

for (let i = arrayLength; i > 0; i--) {
function bubbleSort(array: number[]): number[] {
for (let i = array.length; i > 0; i--) {
for (let j = 0; j < i; j++) {
if (array[j] > array[j + 1]) {
const tempItem = array[j];

array[j] = array[j + 1];
array[j] = array[j + 1];
array[j + 1] = tempItem;
}
}
Expand All @@ -15,4 +13,4 @@ function bubbleSort(array = []) {
return array;
}

module.exports = bubbleSort;
export default bubbleSort;
4 changes: 2 additions & 2 deletions src/caesar-cipher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ _Caesar cipher_ is one of the simplest and most widely known encryption techniqu

## Implementation

**_caesarCipher(str, num)_** should shift every letter in a given ```str``` by a ```num``` (positions down the alphabet).
`caesarCipher(str, num)` should shift every letter in a given `str` by a `num` (positions down the alphabet).

For example:
Example:

```
caesarCipher('Zoo Keeper', 2) // 'Bqq Mggrgt'
Expand Down
35 changes: 0 additions & 35 deletions src/caesar-cipher/caesar-cipher.js

This file was deleted.

37 changes: 0 additions & 37 deletions src/caesar-cipher/caesar-cipher.spec.js

This file was deleted.

27 changes: 27 additions & 0 deletions src/caesar-cipher/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import caesarCipher from "./";

describe("Caesar cipher:", () => {
test("should return transformed string (number 3 given)", () => {
const initialString = "Zoo Keeper";
const expectedString = "Crr Nhhshu";

const shiftNumber = 3;

expect(caesarCipher(initialString, shiftNumber)).toEqual(expectedString);
});

test("should return transformed string (number 84 given)", () => {
const initialString = "I love Javascript";
const expectedString = "O rubk Pgbgyixovz";

const shiftNumber = 84;

expect(caesarCipher(initialString, shiftNumber)).toEqual(expectedString);
});

test("should return empty string (since empty string given)", () => {
const shiftNumber = 99;

expect(caesarCipher("", shiftNumber)).toEqual("");
});
});
34 changes: 34 additions & 0 deletions src/caesar-cipher/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function caesarCipher(str: string, num: number): string {
const alphabet = "abcdefghijklmnopqrstuvwxyz".split("");
const lowerStr = str.toLowerCase();
const position = num % alphabet.length;

let newString = "";

for (let i = 0; i < lowerStr.length; i++) {
const currentLetter = lowerStr[i];

if (currentLetter === " ") {
newString += currentLetter;
} else {
let newIndex = alphabet.indexOf(currentLetter) + position;

if (newIndex > 25) {
newIndex -= alphabet.length;
}

if (newIndex < 0) {
newIndex = alphabet.length + newIndex;
}

newString +=
str[i] === str[i].toUpperCase()
? alphabet[newIndex].toUpperCase()
: alphabet[newIndex];
}
}

return newString;
}

export default caesarCipher;
4 changes: 2 additions & 2 deletions src/fibonacci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ given (i.e. 50).

## Implementation

**_fibonacci(position)_** should return a number on given ```position``` from _Fibonacci_ sequence.
`fibonacci(position)` should return a number on given `position` from _Fibonacci_ sequence.

For example:
Example:

```
fibonacci(9) // 34
Expand Down
11 changes: 0 additions & 11 deletions src/fibonacci/fibonacci.spec.js

This file was deleted.

Loading

0 comments on commit a6a3a62

Please sign in to comment.