Skip to content

Instantly share code, notes, and snippets.

View Pro542's full-sized avatar
🦦

Praveen Senadheera Pro542

🦦
  • United Arab Emirates
View GitHub Profile
@Pro542
Pro542 / faultyKeeb.js
Created August 14, 2023 11:13
You have a faulty keyboard. Whenever you type a vowel on it (a,e,i,o,u,y), it reverses the string that you have written, instead of typing the character. Typing other characters works as expected. Given a string, return what will be on the screen after typing with your faulty keyboard.
function faultyKeeb(str) {
for (let i = 0; i < str.length; ++i) {
if (['a', 'e', 'i', 'o', 'u'].includes(str[i])) {
str = Array.from(str.slice(0, i)).reverse().join('') + str.slice(i+1, str.length);
}
}
return str;
};
@Pro542
Pro542 / timer.tsx
Last active February 12, 2020 07:10
React timer hook with example
function useTimer(endTime: string, disableDays = false): [number, number, number, number] {
const [days, setDays] = useState<number>(0);
const [hours, setHours] = useState<number>(0);
const [minutes, setMinutes] = useState<number>(0);
const [seconds, setSeconds] = useState<number>(0);
useEffect(() => {
const timer = (interval: number): void => {
const countDownDate = new Date(endTime).getTime();
const now = new Date().getTime();
@Pro542
Pro542 / useApiCall.tsx
Created November 25, 2019 09:40
Reusable react state hook to handle api calls
/*
* Hook to reuse state logic for api calls
*
*/
function useApiCall(api: AxiosInstance, params: any[], onError: any => any) {
const [data, setData] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
useEffect(async () => {
@Pro542
Pro542 / tsify.js
Last active May 11, 2018 05:41
Change code in directory to typescript
// this script is used to:
// - rename .js files to .tsx and .ts
// - remove "// @flow" from top of files
// script is unsafe
// run from root of project
const fs = require('fs');
const path = require('path');
const dirPath = 'components';