Skip to content

Instantly share code, notes, and snippets.

View bullishgopher's full-sized avatar

bullishgopher

View GitHub Profile
@bullishgopher
bullishgopher / saga.ts
Created October 14, 2020 12:44
Saga for user authentication. Used this code in https://g1.gg/
import { call, all, takeLatest, put, fork } from 'redux-saga/effects';
import { push } from 'react-router-redux';
// api
import * as AuthApi from 'api/auth';
// helper
import { getToken, clearToken } from 'utils/token';
// actions
@bullishgopher
bullishgopher / debounce.ts
Created October 15, 2020 03:14
a debounce utility in TypeScript
const debounce = <F extends (...args: any[]) => any>(
func: F,
waitFor: number,
) => {
let timeout: NodeJS.Timeout;
return (...args: Parameters<F>): Promise<ReturnType<F>> =>
new Promise((resolve) => {
if (timeout) {
clearTimeout(timeout);
@bullishgopher
bullishgopher / TreeViewItem.md
Created October 16, 2020 01:00
React data tree (recursive)
@bullishgopher
bullishgopher / user.js
Created October 18, 2020 12:56
User model
'use strict';
var bcrypt = require('bcrypt-nodejs');
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class User extends Model {
@bullishgopher
bullishgopher / Input.js
Created October 24, 2020 11:50
useDebounce
import React, { useState } from "react";
import { useDebounce } from "use-debounce";
export default function Input() {
const [text, setText] = useState("Hello");
const [value] = useDebounce(text, 1000);
return (
<div>
<input
@bullishgopher
bullishgopher / tictactoe.md
Created October 25, 2020 17:56
TicTacToe game in React
const reverse = (data) => {
if ( !data || !data.length) {
return false
}
const length = data.length
const reversed = data.split("").reverse().join("");
for ( let index = 0; index < length / 2; index ++ )
{
@bullishgopher
bullishgopher / deposit.js
Created July 6, 2021 15:23
Deposit some _amount in ERC20 _token ( USDC, DAI, etc )
// get web3
let web3 = await initAccount()
// get contract instance
let main = new web3.eth.Contract(
JSON_ABI,
CONTRACT_ADDRESS,
)
// usdc contract instance
@bullishgopher
bullishgopher / chaining and nullish coalescing.md
Created April 20, 2023 15:53
80% Cleaner JavaScript Code Using Optional Chaining and Nullish Coalescing
const book = {
   title:"My Favorite JS Functions",
   author:{
     firstName:"John",
     lastName:"Doe"
   }
}

// (1)
@bullishgopher
bullishgopher / digital-root.md
Created April 20, 2023 16:18
Sum of Digits / Digital Root

Digital root is the recursive sum of all the digits in a number.

Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. The input will be a non-negative integer.

Examples

    16  -->  1 + 6 = 7
   942  -->  9 + 4 + 2 = 15  -->  1 + 5 = 6

132189 --> 1 + 3 + 2 + 1 + 8 + 9 = 24 --> 2 + 4 = 6