Skip to content

Commit

Permalink
chore: convert all the utils to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
esmalleydev committed Apr 13, 2024
1 parent e5890dd commit 3f194d6
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 138 deletions.
12 changes: 6 additions & 6 deletions components/generic/CBB/Picks/Calculator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import CancelCircleIcon from '@mui/icons-material/Cancel';

// import CompareStatistic from '../../CompareStatistic';
import HelperCBB from '@/components/helpers/CBB';
import utilsArrayifer from '@/components/utils/Arrayifer.js';
import utilsSorter from '@/components/utils/Sorter.js';
import utilsArrayifer from '@/components/utils/Arrayifer';
import utilsSorter from '@/components/utils/Sorter';


import { useAppSelector } from '@/redux/hooks';
Expand Down Expand Up @@ -565,9 +565,9 @@ const Calculator = ({ cbb_games, date}) => {
);
};

const pickedRowsConatiner = rows_picked.sort(Sorter.getComparator(order, orderBy)).slice().map((row) => getStyledTableRow(row));
const pickedRowsContainer = rows_picked.sort((a: tableRow, b: tableRow) => Sorter.getComparator(order as string, orderBy as string)(a, b)).slice().map((row: tableRow) => getStyledTableRow(row));

const otherRowsConatiner = rows_other.sort(Sorter.getComparator(order, orderBy)).slice().map((row) => getStyledTableRow(row));
const otherRowsConatiner = rows_other.sort((a: tableRow, b: tableRow) => Sorter.getComparator(order as string, orderBy as string)(a, b)).slice().map((row: tableRow) => getStyledTableRow(row));


if (date < now && games_bet > 2 && roundRobinLength) {
Expand All @@ -588,7 +588,7 @@ const Calculator = ({ cbb_games, date}) => {
}
}

const parleyRowsConatiner = rows_parlay.sort(Sorter.getComparator(order, orderBy)).slice().map((row) => getStyledTableRow(row));
const parleyRowsConatiner = rows_parlay.sort((a: tableRow, b: tableRow) => Sorter.getComparator(order as string, orderBy as string)(a, b)).slice().map((row: tableRow) => getStyledTableRow(row));


const getTable = (rowContainers) => {
Expand Down Expand Up @@ -731,7 +731,7 @@ const Calculator = ({ cbb_games, date}) => {
{rows_parlay.length ? <Typography style = {{'margin': '10px 0px'}} variant="h5">Parley games</Typography> : ''}
{rows_parlay.length ? getTable(parleyRowsConatiner) : ''}
{rows_picked.length ? <Typography style = {{'margin': '10px 0px'}} variant="h5">Games bet</Typography> : ''}
{rows_picked.length ? getTable(pickedRowsConatiner) : ''}
{rows_picked.length ? getTable(pickedRowsContainer) : ''}
{rows_other.length ? <Typography style = {{'margin': '10px 0px'}} variant="h5">Other games</Typography> : ''}
{rows_other.length ? getTable(otherRowsConatiner) : ''}
</div>
Expand Down
2 changes: 1 addition & 1 deletion components/generic/CBB/Picks/Stats/Client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useTheme } from '@mui/material/styles';
import moment from 'moment';
import { Typography, Skeleton, Card, CardContent } from '@mui/material';

import utilsColor from '@/components/utils/Color.js';
import utilsColor from '@/components/utils/Color';

const ColorUtil = new utilsColor();

Expand Down
2 changes: 1 addition & 1 deletion components/generic/CBB/Picks/Tile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import CompareStatistic from '@/components/generic/CompareStatistic';
import Indicator from '@/components/generic/CBB/Indicator';
import Pin from '@/components/generic/CBB/Pin';
import { useAppDispatch, useAppSelector } from '@/redux/hooks';
import Color, { getBestColor, getWorstColor } from '@/components/utils/Color.js';
import Color, { getBestColor, getWorstColor } from '@/components/utils/Color';
import { setScrollTop } from '@/redux/features/picks-slice';
import { updateGameSort } from '@/redux/features/favorite-slice';
import { useScrollContext } from '@/contexts/scrollContext';
Expand Down
2 changes: 1 addition & 1 deletion components/generic/CBB/Team/Schedule/TableView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useAppSelector } from '@/redux/hooks';
import { Paper, Table, TableBody, TableContainer, TableHead, TableRow, TableCell, Skeleton } from '@mui/material';
import HelperCBB from '@/components/helpers/CBB';
import Locked from '@/components/generic/Billing/Locked';
import Color, { getBestColor, getWorstColor } from '@/components/utils/Color.js';
import Color, { getBestColor, getWorstColor } from '@/components/utils/Color';
import { useRouter } from 'next/navigation';
import BackdropLoader from '@/components/generic/BackdropLoader';

Expand Down
2 changes: 1 addition & 1 deletion components/generic/CBB/Team/Schedule/Tile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useTheme } from '@mui/material/styles';
import BackdropLoader from '@/components/generic/BackdropLoader';
import { Card, CardContent, Typography, Tooltip, Link, CardActionArea, Skeleton, IconButton } from '@mui/material';
import Locked from '@/components/generic/Billing/Locked';
import Color, { getBestColor, getWorstColor } from '@/components/utils/Color.js';
import Color, { getBestColor, getWorstColor } from '@/components/utils/Color';
import { useAppDispatch, useAppSelector } from '@/redux/hooks';
import LegendToggleIcon from '@mui/icons-material/LegendToggle';
import { setScrollTop, updateVisibleScheduleDifferentials } from '@/redux/features/team-slice';
Expand Down
4 changes: 2 additions & 2 deletions components/generic/CBB/Team/Stats/Roster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const Roster = ({ rosterStats }) => {
const [isPending, startTransition] = useTransition();

const [view, setView] = useState<string | null>('overview');
const [order, setOrder] = useState<string | null>('asc');
const [order, setOrder] = useState<string>('asc');
const [orderBy, setOrderBy] = useState<string>('minutes_per_game');
const [spin, setSpin] = useState(false);

Expand All @@ -63,7 +63,7 @@ const Roster = ({ rosterStats }) => {
useEffect(() => {
const sessionOrderby = sessionStorage.getItem('CBB.TEAM.ROSTER.ORDERBY') || null;
setView(sessionStorage.getItem('CBB.TEAM.ROSTER.VIEW') ? sessionStorage.getItem('CBB.TEAM.ROSTER.VIEW') : 'overview');
setOrder(sessionStorage.getItem('CBB.TEAM.ROSTER.ORDER') ? sessionStorage.getItem('CBB.TEAM.ROSTER.ORDER') : 'asc');
setOrder(sessionStorage.getItem('CBB.TEAM.ROSTER.ORDER') ? sessionStorage.getItem('CBB.TEAM.ROSTER.ORDER') as string : 'asc');
setOrderBy(sessionOrderby ? sessionOrderby : 'minutes_per_game');
}, []);

Expand Down
78 changes: 0 additions & 78 deletions components/utils/Arrayifer.js

This file was deleted.

79 changes: 79 additions & 0 deletions components/utils/Arrayifer.ts
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;
20 changes: 10 additions & 10 deletions components/utils/Color.js → components/utils/Color.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import { useTheme } from '@mui/material/styles';

export const getBestColor = () => {
export const getBestColor: () => string = () => {
const theme = useTheme();

return theme.palette.mode === 'light' ? theme.palette.success.main : theme.palette.success.dark;
};

export const getWorstColor = () => {
export const getWorstColor: () => string = () => {
const theme = useTheme();

return theme.palette.mode === 'light' ? theme.palette.error.main : theme.palette.error.dark;
};

export const getLogoColorPrimary = () => {
export const getLogoColorPrimary: () => string = () => {
const theme = useTheme();

// return theme.palette.mode === 'light' ? '#fd35ab' : '#FDD835';
return theme.palette.mode === 'light' ? theme.palette.warning.light : '#FDD835';
};

export const getLogoColorSecondary = () => {
export const getLogoColorSecondary: () => string = () => {
const theme = useTheme();

return theme.palette.mode === 'light' ? '#482ab9' : '#2ab92a';
};


class Color {
constructor(args) {
constructor() {
};


/**
* A linear interpolator for hexadecimal colors
* @param {String} a
* @param {String} b
* @param {Number} amount
* @param {string} a
* @param {string} b
* @param {number} amount
* @example
* // returns #7F7F7F
* lerpColor('#000000', '#ffffff', 0.5)
* @returns {String}
* @returns {string}
*/
lerpColor(a, b, amount) {
lerpColor(a: string, b: string, amount: number): string {
var ah = ah = +a.replace('#', '0x'),
ar = ah >> 16, ag = ah >> 8 & 0xff, ab = ah & 0xff,
bh = +b.replace('#', '0x'),
Expand Down
38 changes: 0 additions & 38 deletions components/utils/Sorter.js

This file was deleted.

38 changes: 38 additions & 0 deletions components/utils/Sorter.ts
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;

0 comments on commit 3f194d6

Please sign in to comment.