Skip to content

Instantly share code, notes, and snippets.

View nishanbajracharya's full-sized avatar

Nishan Bajracharya nishanbajracharya

View GitHub Profile
@nishanbajracharya
nishanbajracharya / router-example.ts
Last active January 12, 2024 05:19
Vanilla Router
import './style.css';
import Home from './views/Home';
import Todo from './views/Todo';
import TodoEdit from './views/TodoEdit';
import NotFound from './views/NotFound';
import Router, { RouteErrorProps, RouteProps, Routes } from './lib/router';
function handleProtected(props: RouteProps): boolean {
return false;
// https://gist.github.com/nishanbajracharya/8fe38807b3ad074a7da2072c7b8e701b
function normalize(input = {}) {
var values = Array.isArray(input) ? input : Object.values(input);
return values.reduce((acc, value) => {
if (value.children) {
return {
...acc,
[value.id]: {
@nishanbajracharya
nishanbajracharya / interceptor.js
Last active May 13, 2020 08:42
Axios Interceptor for single request
// DEFINE BASE_URL
// DEFINE refreshAccessToken, this should use `refreshInstance` instead of `instance`
/*
function refreshAccessToken() {
return refreshInstance
.post('token', {
refresToken: localStorage.getItem('refreshToken')
})
...
@nishanbajracharya
nishanbajracharya / css.js
Created November 3, 2019 09:11
Convert JS style object to injectable CSS
function toKebabCase(string) {
return string.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase();
}
function hash(string) {
var h = 0
var i = 0;
if (string.length > 0) {
while (i < string.length) {
@nishanbajracharya
nishanbajracharya / preload.js
Created July 7, 2019 11:19
Preload images in plain JavaScript
function preloadImages(srcs, onComplete = f => f) {
var imgArray = {};
var imgErrorArray = {};
var imageFailCount = 0;
var imageLoadedCount = 0;
srcs.forEach(function(src, index) {
imgArray[index] = null;
imgErrorArray[index] = null;
#!/bin/bash
repoURL=$1
port=${2:-5000}
portInUse=$(lsof -t -i :$port)
if [ -z "$portInUse" ]
then
printf "Port $port available.\n\n"
else
var input = {
'1': {
id: 1,
name: 'John',
children: [{
id: 2,
name: 'Sally'
},
{
id: 3,
@nishanbajracharya
nishanbajracharya / normalization.js
Created June 18, 2019 05:40
Normalize input to get output.
// From this
var input = {
'1': {
id: 1,
name: 'John',
children: [
{ id: 2, name: 'Sally' },
{ id: 3, name: 'Mark', children: [{ id: 4, name: 'Harry' }] }
]
},
const animate = document.getElementById('animate');
const btn = document.getElementById('btn');
let enabled = false;
btn.onclick = function() {
if (enabled) {
enabled = false;
transition(animate, {
@nishanbajracharya
nishanbajracharya / Observer.js
Last active June 11, 2020 08:31
Simple observer pattern with example
class Observer {
constructor(state) {
this.state = state;
this.subscribers = [];
}
get() {
return this.state;
}