Skip to content

Instantly share code, notes, and snippets.

View AliBahaari's full-sized avatar
🙃
KIP! (Knowledge Is Power)

Ali Bahaari AliBahaari

🙃
KIP! (Knowledge Is Power)
View GitHub Profile
@AliBahaari
AliBahaari / localStorageController.ts
Created January 12, 2023 19:04
A function for handling local storage manipulations!
const localStorageController = <T extends string = "", K = "">(
storageName: T,
type: "GET" | "SET" | "DELETE",
storageValue?: K
) => {
if (global?.window !== undefined && window !== undefined) {
if (type === "GET") {
const fetchedValue = window.localStorage.getItem(storageName);
if (fetchedValue) {
return JSON.parse(fetchedValue);
@AliBahaari
AliBahaari / removeEmptyProperties.ts
Created January 12, 2023 19:02
Remove object properties which have `undefined` or `null` values.
interface IRemoveEmptyProperties {
[index: string]: any;
}
const removeEmptyProperties = <T extends IRemoveEmptyProperties = {}>(
param: T
) => {
let newObject: IRemoveEmptyProperties = {};
Object.keys(param).forEach((key: any) => {
if (param[key]) {
@AliBahaari
AliBahaari / searchValueInArrays.ts
Last active January 12, 2023 19:00
A generic function which searches a value in an array of values.
const searchValueInArrays = <T extends any = [], K extends any = "">(
values: T[],
searchedValue: K
): { filteredItems: T[]; searchedValueCounts: number; searchedValue: K } => {
if (values?.length) {
const filteredItems = values?.filter((item) =>
item.includes(searchedValue)
);
return {
filteredItems: filteredItems,
@AliBahaari
AliBahaari / useSpeechRecognition.ts
Created December 27, 2022 19:46
A hook for speech-to-text.
import { isServer } from '@hasty-bazar-commerce/utils'
import { useEffect, useState } from 'react'
export const useSpeechRecognition = (
lang?: string,
continues?: boolean,
interimResults?: boolean,
) => {
const [speechRecognitionState, setSpeechRecognitionState] = useState<boolean>()
@AliBahaari
AliBahaari / useArrMem.js
Last active November 7, 2021 11:14
A hook for checking membership of an element.
export const useArrMem = (i, arr) => {
if (arr.indexOf(i) >= 0) {
return true;
} else {
return false;
}
}
@AliBahaari
AliBahaari / removeDuplicates.py
Last active November 7, 2021 11:15
How to remove duplicates from a list?
duplicatesList = [1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
cleanedList = list(set(duplicatesList))
print(cleanedList)
# Output: [1, 2, 3, 4, 5]