-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from aliaks-ei/migrate_to_typescript
Migrate repo to Typescript
- Loading branch information
Showing
75 changed files
with
3,721 additions
and
3,795 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/'], | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
16 changes: 7 additions & 9 deletions
16
src/binary-search/binary-search.js → src/binary-search/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.