Skip to content

Instantly share code, notes, and snippets.

View manavm1990's full-sized avatar
🏠
Working from home

Manav Misra manavm1990

🏠
Working from home
View GitHub Profile
@manavm1990
manavm1990 / index.ts
Created August 22, 2023 15:26
Sometimes, merging objects with spread results in TS losing its way with inferences. It starts to complain about potential `undefined`. You COULD use type assertions......
// Merges additional properties into an object, ensuring type consistency.
export const mergeProperties = <T, U>(
originalObject: T,
additionalProps: U,
): T & U => {
return { ...originalObject, ...additionalProps };
};
import { type ZodError } from 'zod';
export const stringifyZodError = (error: ZodError): string => {
return error.issues
.map((issue) => {
const path = issue.path.join('.');
const message = issue.message;
return `Currently facing issue with the field "${path}". ${message}`;
})
.join('\n');
@manavm1990
manavm1990 / index.js
Created April 29, 2023 13:34
Create some contacts with faker
import { faker } from "@faker-js/faker";
import { promises as fs } from "fs";
const users = Array.from({ length: 1000 }, (_, i) => ({
name: faker.name.fullName(),
tel: faker.phone.number("###-###-####"),
img: faker.image.avatar(),
})).map((user) => {
const { name } = user;
const [firstName, lastName] = name.split(" ");
function formatMinutesSeconds(time) {
return `${Math.floor(time / 60)}:${(time % 60).toString().padStart(2, "0")}`;
}
@manavm1990
manavm1990 / winner.js
Created October 18, 2022 13:12
Get winner from TTT game
const WINNING_INDICES = [
[0, 1, 2],
[0, 3, 6],
[0, 4, 8],
[1, 4, 7],
[2, 5, 8],
[2, 4, 6],
[3, 4, 5],
[6, 7, 8],
];
@manavm1990
manavm1990 / index.test.ts
Last active October 9, 2022 04:46
Some expert level JS challenges (according to Hackernoon post)
export const getLenOfLongestSubstringWithoutRepeatingChars = (
str: string,
): number => {
// If the string is empty, return 0
if (str.length === 0) {
return 0;
}
// Start building a substring from the first character until we duplicate a character
return str
@manavm1990
manavm1990 / spinner.html
Created October 8, 2022 19:03
Tailwind Spinner
<main className="flex h-screen items-center justify-center">
<div className="border-gray mr-3 h-48 w-48 animate-spin rounded-full border-4 border-t-4 border-t-blue-300" />
</main>
const WINNING_INDICES = [
[0, 1, 2],
[0, 3, 6],
[0, 4, 8],
[1, 4, 7],
[2, 5, 8],
[2, 4, 6],
[3, 4, 5],
[6, 7, 8],
];
import { render, screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import App from "./App";
import renderer from "react-test-renderer";
const setup = () => {
render(<App />);
return screen.getByRole("table");
};
@manavm1990
manavm1990 / UserContext.jsx
Last active May 9, 2022 13:38
Memoized UserContext
import PropTypes from "prop-types";
import React from "react";
export const UserContext = React.createContext();
export default function UserContextProvider({ children }) {
const [currentUser, setCurrentUser] = React.useState(
JSON.parse(localStorage.getItem("user"))
);