Skip to main content

class based task manager

Jun 1, 2023CodeCatch
Loading...

More TypeScript Posts

D3 use-hook in React

Dec 9, 2024shaiRos

0 likes • 9 views

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) return
setCurrentData(props.data)
// add d3 code
svg.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})`);
// data
const data = props.data
},[props])
return (<svg ref={svgRef} />)
}

typescript hello

Nov 21, 2023AustinLeath

0 likes • 25 views

// Print Hello, World!
console.log("Hello, World!");

langToConfig map

Mar 31, 2023Helper

0 likes • 3 views

const langToConfig = {
'javascript': {
image: '',
cmd: ['']
},
'php': {
image: '',
cmd: ['']
},
}
function removeDuplicates(nums: number[]): number {
nums.splice(0, nums.length, ...Array.from(new Set(nums)));
return nums.length;
};

LeetCode - Two Sum

Oct 22, 2024C S

0 likes • 1 view

function twoSum(nums: number[], target: number): number[] {
const map = new Map();
for(let i = 0; i < nums.length; i++) {
if(map.has(target - nums[i])) {
return [map.get(target - nums[i]), i];
} else {
map.set(nums[i], i);
}
}
return []
}

Depth-First Search in TypeScript

Sep 19, 2024Sam-C137

0 likes • 2 views

class DFS
{
public dfs(G: DFSGraph, startVert: number)
{
let visited: boolean[] = Array<boolean>();
// Pre-populate array:
for(let i = 0; i < G.size; i++)
{
visited.push(false);
}
let s: number[] = new Array();
visited[startVert] = true;
s.push(startVert);
while(s.length > 0)
{
const v = s.pop();
for(let adjV of G.adj[v])
{
if(!visited[adjV])
{
visited[adjV] = true;
s.push(adjV);
}
}
}
}
public dfsRecursive(G: DFSGraph, startVert: number)
{
let visited: boolean[] = Array<boolean>();
// Pre-populate array:
for(let i = 0; i < G.size; i++)
{
visited.push(false);
}
this.dfsAux(G, startVert, visited);
}
private dfsAux(G: DFSGraph, v: number, visited: boolean[])
{
visited[v] = true;
for(let adjV of G.adj[v])
{
if(!visited[adjV])
{
// this.foo(); // Something can happen before the visit.
this.dfsAux(G, adjV, visited);
// this.bar(); // Something can happen after the visit.
}
}
}
}