Skip to content

Instantly share code, notes, and snippets.

View irhamputra's full-sized avatar
🇮🇩
TypeScript, React, GQL, Node and Cat

Irham Putra Prasetyo irhamputra

🇮🇩
TypeScript, React, GQL, Node and Cat
View GitHub Profile
import { FunctionComponent } from 'react'
const combineProviders = (providers: FunctionComponent[]) => providers.reduce((Combined, [Provider, props = {}]) => ({ children }) => (
<Combined>
<Provider {...props}>{children}</Provider>
</Combined>
))
const App = () => {
@irhamputra
irhamputra / index.js
Created August 9, 2020 16:40
Fibonacci O(1)
function fib(n) {
const A = (1 + Math.sqrt(5)) / 2;
const B = (1 - Math.sqrt(5)) / 2;
fib = (Math.pow(A, n) + Math.pow(B, n)) / Math.sqrt(5);
return Math.ceil(fib)
}
export default function debounce<F extends (...args: any[]) => any>(
duration: number,
fn: F,
) {
let timeout: number | null;
return function(this: any, ...args: Parameters<F>) {
if (timeout) {
window.clearTimeout(timeout);
}
timeout = window.setTimeout(() => {
@irhamputra
irhamputra / handleError.js
Last active January 6, 2023 10:35
simple display error message in GraphQL with Styled Component
import styled from 'styled-components';
import React from 'react';
import PropTypes from 'prop-types';
const ErrorStyles = styled.div`
padding: 2rem;
background: white;
margin: 2rem 0;
border: 1px solid rgba(0, 0, 0, 0.05);
@irhamputra
irhamputra / reusable handle form
Created April 10, 2019 21:10
reusable handling for all form in one function
handleChange = (e) => {
const {name, type, value} = e.target;
const val = type === 'number' ? parseFloat(value) : value;
this.setState({[name]: val})
};
@irhamputra
irhamputra / upload.js
Last active January 6, 2023 10:35
upload file function to cloudinary
uploadFile = async e => {
const files = e.target.files;
const data = new FormData();
data.append('file', files[0]);
data.append('upload_preset', 'sick-fits');
const res = await fetch('https://api.cloudinary.com/v1_1/${YOUR_API_URL}/image/upload', {
method: 'post',
body: data
@irhamputra
irhamputra / convertCurrency.js
Last active January 6, 2023 10:34
format number to currency money function
export default function(amount) {
const options = {
style: 'currency',
currency: 'IDR',
minimumFractionDigits: 0,
};
// if its a whole, dollar amount, leave off the .00
if (amount % 100 === 0) options.minimumFractionDigits = 0;
@irhamputra
irhamputra / palindrome function
Last active April 10, 2019 22:47
better palindrome function
// Better performance
function palindrome(str) {
// 1. make all letters lowercase and remove non-alphanumeric characters
str = str.toLowerCase().replace(/[\W_]/g, "");
// 2. check if the length of the string is 0 then it is a palindrome
if (str.length === 0){
return true;
}

1. Clone your fork:

git clone [email protected]:YOUR-USERNAME/YOUR-FORKED-REPO.git

2. Add remote from original repository in your forked repository:

cd into/cloned/fork-repo
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream