-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: convert all the utils to typescript
- Loading branch information
1 parent
e5890dd
commit 3f194d6
Showing
11 changed files
with
139 additions
and
138 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
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
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,79 @@ | ||
class Arrayifer { | ||
constructor() { | ||
}; | ||
|
||
|
||
/** | ||
* Shuffle / ranomize elements in an array | ||
* @param {array} array The array to shuffle | ||
* @return array | ||
*/ | ||
shuffle(array: Array<any>): Array<any> { | ||
let currentIndex: number = array.length; | ||
let randomIndex: number; | ||
|
||
// While there remain elements to shuffle. | ||
while (currentIndex != 0) { | ||
// Pick a remaining element. | ||
randomIndex = Math.floor(Math.random() * currentIndex); | ||
currentIndex--; | ||
|
||
// And swap it with the current element. | ||
[array[currentIndex], array[randomIndex]] = [ | ||
array[randomIndex], array[currentIndex]]; | ||
} | ||
|
||
return array; | ||
}; | ||
|
||
/** | ||
* | ||
* @param {Array} arr | ||
* @param {number} n | ||
* @param {number} r | ||
* @param {number} index | ||
* @param {Array} data | ||
* @param {number} i | ||
* @param {Array} results | ||
* @returns Array | ||
*/ | ||
combination (arr: Array<any>, n: number, r: number, index: number, data: number[], i: number, results: number[][]) { | ||
if (index == r) { | ||
let result: number[] = []; | ||
for (let j=0; j<r; j++){ | ||
result.push(data[j]); | ||
} | ||
results.push(result); | ||
return results; | ||
} | ||
|
||
if (i >= n) { | ||
return results; | ||
} | ||
|
||
data[index] = arr[i]; | ||
this.combination(arr, n, r, index+1, data, i+1, results); | ||
this.combination(arr, n, r, index, data, i+1, results); | ||
|
||
return results; | ||
}; | ||
|
||
/** | ||
* Get all combinations of every value in the provided array for a specified number | ||
* @param {Array} arr | ||
* @param {number} n | ||
* @param {number} r | ||
* @returns Array | ||
*/ | ||
getCombinations(arr: Array<any>, n: number, r: number) { | ||
let data: Array<any> = new Array(r); | ||
|
||
let results: number[][] = []; | ||
results = this.combination(arr, n, r, 0, data, 0, results); | ||
return results; | ||
}; | ||
|
||
|
||
}; | ||
|
||
export default Arrayifer; |
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,38 @@ | ||
/** | ||
* Table sorting helper utility | ||
*/ | ||
class Sorter { | ||
constructor() { | ||
}; | ||
|
||
descendingComparator(a, b, orderBy, direction_?: string): number { | ||
if ((orderBy in a) && b[orderBy] === null) { | ||
return 1; | ||
} | ||
if (a[orderBy] === null && (orderBy in b)) { | ||
return -1; | ||
} | ||
|
||
let a_value = a[orderBy]; | ||
let b_value = b[orderBy]; | ||
|
||
const direction = direction_ || 'lower'; | ||
|
||
if (b_value < a_value) { | ||
return direction === 'higher' ? 1 : -1; | ||
} | ||
if (b_value > a_value) { | ||
return direction === 'higher' ? -1 : 1; | ||
} | ||
return 0; | ||
}; | ||
|
||
getComparator(order: string, orderBy: string, direction?: string): Function { | ||
return order === 'desc' | ||
? (a, b) => this.descendingComparator(a, b, orderBy, direction) | ||
: (a, b) => -this.descendingComparator(a, b, orderBy, direction); | ||
}; | ||
|
||
}; | ||
|
||
export default Sorter; |