Loading...
More TypeScript Posts
function removeElement(nums: number[], val: number): number {let zeroStartIndex = 0;for(let i = 0; i < nums.length; i++) {if(nums[i] !== val) {nums[zeroStartIndex] = nums[i];zeroStartIndex++;}}return zeroStartIndex;};
const getFollowers = async (req: Request, res: Response) => {try {const username = req.params.username || '';const user = await User.findUnique({where: { username },include: { followedBy: true },});if (!user) {res.status(404).json({error: 'User could not be found',});return;}const followers = user.followedBy.map(followedBy => {return {followerId: followedBy.followerId,};});const followersCount = followers.length;res.json({ followersCount: followersCount, followers });} catch (error) {console.error('getFollowers() error: ', error);res.status(500).json({error: `There was an error fetching followers for user with username "${req.params.username}", please try again later`,});}};const getFollowing = async (req: Request, res: Response) => {try {const username = req.params.username || '';const user = await User.findUnique({where: { username },include: { following: true },});if (!user) {res.status(404).json({error: 'User could not be found',});return;}const following = user.following.map(following => {return {followingId: following.followingId,};});const followingCount = following.length;res.json({ followingCount: followingCount, following });} catch (error) {console.error('getFollowing() error: ', error);res.status(500).json({error: `There was an error fetching who is following "${req.params.username}", please try again later`,});}};const followUser = async (req: Request, res: Response) => {try {const username = req.params.username || '';const user = await User.findUnique({where: { username },});if (!user) {res.status(404).json({error: 'User could not be found',});return;}// dont allow a user to follow themselvesif (req.user.id === user.id) {res.status(400).json({ error: 'You cannot follow yourself' });return;}//check if a follow relationship already existsconst followrelationship = await prisma.follows.findUnique({where: {followerId_followingId: {followerId: req.user.id,followingId: user.id,},},});// check if the follow relationship already existsif (followrelationship) {res.status(400).json({ error: 'You are already following this user' });return;}// create the follow relationshipconst follow = await prisma.follows.create({data: {followerId: req.user.id,followingId: user.id,},});res.json({ follow });} catch (error) {console.error('followUser() error: ', error);res.status(500).json({error: `There was an error following "${req.params.username}", please try again later`,});}};const unfollowUser = async (req: Request, res: Response) => {try {const username = req.params.username || '';const user = await User.findUnique({where: { username },});if (!user) {res.status(404).json({error: 'User could not be found',});return;}// dont allow a user to unfollow themselvesif (req.user.id === user.id) {res.status(400).json({ error: 'You cannot unfollow yourself' });return;}//check if a follow relationship already existsconst followrelationship = await prisma.follows.findUnique({where: {followerId_followingId: {followerId: req.user.id,followingId: user.id,},},});// check if the follow relationship already existsif (!followrelationship) {res.status(400).json({ error: 'You are not following this user' });return;}// delete the follow relationshipconst follow = await prisma.follows.delete({where: {followerId_followingId: {followerId: req.user.id,followingId: user.id,},},});res.json({ follow });} catch (error) {console.error('unfollowUser() error: ', error);res.status(500).json({error: `There was an error unfollowing "${req.params.username}", please try again later`,});}};
import React, { useEffect, useRef, useState } from 'react';import * as d3 from 'd3';export default function useD3(renderChartFn : (svg : d3.Selection<SVGElement,{},HTMLElement>) => any, dependencies : any[]) {const ref = useRef<SVGSVGElement>();useEffect(() => {let d3El = d3.select(ref.current)renderChartFn(d3.select(ref.current));return () => {};}, dependencies);return ref;}function MyChart(props : {w: number,h: number,data: Array<any>}) {const [currentData,setCurrentData] = useState(props.data)let svgRef = useD3((svg) => {if (props.data === currentData) returnsetCurrentData(props.data)// add d3 codesvg.selectAll("*").remove();const margin = { top: 0, right: 80, bottom: 20, left: 10 };const width = props.w - margin.left - margin.right;const height = props.h - margin.top - margin.bottom;const canvas = svg.attr('width', width + margin.left + margin.right).attr('height', height + margin.top + margin.bottom).append('g').attr('transform', `translate(${margin.left},${margin.top})`);// dataconst data = props.data},[props])return (<svg ref={svgRef} />)}
function removeDuplicates(nums: number[]): number {nums.splice(0, nums.length, ...Array.from(new Set(nums)));return nums.length;};console.log(removeDuplicates([1, 2, 3, 4, 5, 6]))
// theme.tsimport { createTheme } from '@mui/material/styles';const theme = createTheme({typography: {h1: {fontSize: '2rem',fontWeight: 700,lineHeight: 1.3},h2: {fontSize: '1.625rem',fontWeight: 700,lineHeight: 1.35},h3: {fontSize: '1.375rem',fontWeight: 700,lineHeight: 1.4},h4: {fontSize: '1.125rem',fontWeight: 700,lineHeight: 1.45},h5: {fontSize: '1rem',fontWeight: 700,lineHeight: 1.5},h6: {fontSize: '0.875rem',fontWeight: 700,lineHeight: 1.5},},});
// Print Hello, World!console.log("Hello, World!");