Skip to content

Instantly share code, notes, and snippets.

View srikiranvelpuri's full-sized avatar
🎯
Focusing

srikiran srikiranvelpuri

🎯
Focusing
View GitHub Profile
@srikiranvelpuri
srikiranvelpuri / React.customhook.useStateObj.js
Created June 29, 2023 04:25
React custom hook to handle state as an object in functional components similar to local state management in class based components.
import React, { useState } from 'react';
import { isEmpty, isNil } from 'ramda';
/**
* Custom hook to handle state as an object in functional components similar to local state management in class based components.
** Returns a stateful value and a function to update it.
* @param {object} initialStateObj
* @returns { [{},React.Dispatch<React.SetStateAction<{}>>]}
*/
const useStateObj = (initialStateObj = {}) => {
@srikiranvelpuri
srikiranvelpuri / refreshOAuthToken.js
Last active May 14, 2024 11:13
Postman PreRequestScript to check if an OAuth token has expired and, if so, refreshes it by making a new authentication request.
const expiryDate = new Date(pm.environment.get('tokenExpiryDate'));
const currDate = new Date(Date.now());
const isTokenExpired = (isNaN(expiryDate) || expiryDate) < currDate;
if(isTokenExpired){
pm.sendRequest({
url: pm.globals.get("oAuthUrl"),
method: 'POST',
header: {
'Authorization': pm.globals.get("basicAuth"),
@srikiranvelpuri
srikiranvelpuri / React.customhook.useDisableInspect.js
Last active May 6, 2024 11:12
React cusotm hook to diable inspect
/**
* React cusotm hook to disable inspect
*/
export const useDisableInspect = () => {
useEffect(() => {
const disableRightClick = (e) => {
e.preventDefault();
};
const handleKeyDown = (e) => {
@srikiranvelpuri
srikiranvelpuri / debounce.js
Last active May 6, 2024 11:24
A pure JavaScript debounce function for optimizing function calls, particularly useful for handling rapid user input events.
function debounce(func, delay) {
let timeoutId;
return function () {
const context = this;
const args = arguments;
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
@srikiranvelpuri
srikiranvelpuri / clarityAnalytics.js
Last active May 6, 2024 11:09
A simple JavaScript module facilitating the seamless integration of Clarity tracking into web applications using clarity-js
import { clarity } from "clarity-js";
import { isNil, isEmpty } from "ramda";
const isNilorEmpty = val => isNil(val) || isEmpty(val);
/**
* Initializes Clarity tracking with the provided Clarity project Id, unique user identifier, and custom tags.
* @param {string} clarityId - The Clarity project Id. This parameter is required.
* @param {string} [uniqueId=""] - The unique identifier for the user.
* @param {Object.<string, string>} [customTags={}] - Custom tags to be set for Clarity. Each key-value pair represents a custom tag and its value.
@srikiranvelpuri
srikiranvelpuri / memoize.js
Created May 6, 2024 11:23
A JavaScript function to cache the results of a function, minimizing redundant computations.
const memoize = (f) =>
function(...args) {
f.memoize = f.memoize || {}
return args in f.memoize
? f.memoize[args]
: (f.memoize[args] = f.apply(this, args))
}
export default memoize