Skip to content

Instantly share code, notes, and snippets.

View Tribhuwan-Joshi's full-sized avatar
🌊
water

Tjsm Tribhuwan-Joshi

🌊
water
View GitHub Profile
@Tribhuwan-Joshi
Tribhuwan-Joshi / copyFolder.js
Created February 22, 2025 18:03
Copy All folders with specific names that are present in n-level nested directories
const fs = require('node:fs/promises');
const path = require('path');
let c = 1;
async function fn(p, FolderName) {
const items = await fs.readdir(p, { withFileTypes: true });
for (i of items) {
if (i.isDirectory()) {
if (i.name == FolderName) {
console.log(i);
await fs.cp(path.join(i.parentPath, i.name), `./all/finished-${c++}`, {
@Tribhuwan-Joshi
Tribhuwan-Joshi / client.js
Created January 13, 2025 05:12
Native handling of file upload in NodeJs - no library
const inputButton = document.getElementById("inputtag");
const buttonn = document.querySelector(".btn");
inputButton.addEventListener("change", async (e) => await updateFile(e));
buttonn.addEventListener("click", () => uploadFile());
let data = null;
async function updateFile(e) {
const file = e.target.files[0];
const formData = new FormData();
@Tribhuwan-Joshi
Tribhuwan-Joshi / auth.ts
Created October 25, 2023 11:48
Add Auth in Next13
import NextAuth from "next-auth";
import GithubProvider from "next-auth/providers/github";
export const authOptions = {
providers: [
GithubProvider({
clientId: process.env.GITHUB_ID ?? "",
clientSecret: process.env.GITHUB_SECRET ?? "",
}),
],
@Tribhuwan-Joshi
Tribhuwan-Joshi / app.js
Created July 14, 2023 08:15
JWT overview
const express = require("express");
const jwt = require("jsonwebtoken");
const app = express();
app.get("/api", (req, res) => {
res.json({
message: "Welcome to the API",
});
});
@Tribhuwan-Joshi
Tribhuwan-Joshi / app.js
Created July 11, 2023 07:25
Authentication passport
const express = require("express");
const path = require("path");
const session = require("express-session");
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const mongoose = require("mongoose");
const User = require("./models/user");
const {
getUser,
@Tribhuwan-Joshi
Tribhuwan-Joshi / throttle.js
Created May 20, 2023 15:23
Given a function fn and a time in milliseconds t, return a throttled version of that function.
/*
A throttled function is first called without delay and then, for a time interval of t milliseconds, can't be executed but should store the latest function arguments provided to call fn with them after the end of the delay.
*/
const throttle = function (fn , t){
let intervalInProgress = null;
let argsToProcess = null;
const intervalFunction = () =>{
if(argsToProcess == null){
@Tribhuwan-Joshi
Tribhuwan-Joshi / promisePool.js
Created May 18, 2023 11:33
Promise pool to limit concurrency
const promisePool = async function(functions, n) {
async function evaluateNext(){
if(functions.length == 0 ) return ;
const fn = functions.shift();
await fn();
await evaluateNext(); // call the function recursively until the function ends
}
const nPromises = Array(n).fill().map(evaluateNext); // create array of size n to call the evalute next fn.
await Promise.all(nPromises); // wait for all promise to resolve
};
@Tribhuwan-Joshi
Tribhuwan-Joshi / timeLimit.js
Created May 16, 2023 09:31
Promise Time Limit JS
/**
* @param {Function} fn
* @param {number} t
* @return {Function}
*/
var timeLimit = function(fn, t) {
return async function(...args) {
return Promise.race([new Promise((res,rej)=>setTimeout(()=>rej("Time Limit Exceeded"),t)) , fn(...args) ])
}
async function sleep(millis){
return new Promise(res => setTimeout(res,millis) ) // return a promise that will resolve after millis milliseconds
}
const t = Date.now();
sleep(100).then(()=>console.log(Date.now() - t ));
@Tribhuwan-Joshi
Tribhuwan-Joshi / curry.js
Created May 14, 2023 06:13
Smooth currying in JS
const curry = function (fn){
return function currying(...args){
if(args.length >= fn.length ){
return fn(...args);
}
else {
return function(...args2){
return currying.apply(...args.concat(args2))
}
}