Skip to content

Instantly share code, notes, and snippets.

Avatar
🐨

Hung Doan hungdoansy

🐨
  • Got It, Inc
  • Hanoi, Vietnam
View GitHub Profile
@hungdoansy
hungdoansy / convert.js
Created May 12, 2021
Convert remote image into base64 with axios
View convert.js
const getBase64 = async url => {
const response = await axios.get(url, {
responseType: 'arraybuffer',
});
const imageType = response.headers['content-type'];
const base64 = Buffer.from(response.data, 'binary').toString('base64');
const dataURI = `data:${imageType};base64,${base64}`;
return dataURI;
@hungdoansy
hungdoansy / compare.js
Created Apr 28, 2021
Compare Math.floor and ~~
View compare.js
const getNumbers = () => {
const numbers = [];
for(let i = 0; i < 50000; i++) {
const n = +(Math.random() * 10000).toFixed(5);
numbers.push(n);
}
return numbers;
}
@hungdoansy
hungdoansy / useQueryParam.js
Last active Dec 23, 2020
Simple & minimal useQueryParam hook
View useQueryParam.js
import { useCallback } from 'react';
import { useHistory, useLocation } from 'react-router-dom';
import queryString from 'query-string';
const useQueryParam = (key) => {
const history = useHistory();
const location = useLocation();
const { search } = location;
const params = queryString.parse(search);
@hungdoansy
hungdoansy / simpleCarousel.js
Last active Jan 21, 2021
Simple Carousel with React and styled-component
View simpleCarousel.js
// Codesandbox: https://codesandbox.io/s/elegant-frog-hbs3f?file=/src/Carousel.js:0-5654
import React, { useRef, useState } from "react";
import classNames from "classnames";
import styled from "styled-components";
const ArrowDownContainer = styled.div`
display: flex;
justify-content: center;
align-items: center;
@hungdoansy
hungdoansy / useMultipleRefs.js
Created Dec 20, 2020
Use multiple refs for a list in React
View useMultipleRefs.js
/*
* This is helpful when you have multiple items and want to have some control over each of them.
*/
export const useMultipleRefs = (n) => {
const refs = useRef(
Array(n)
.fill(1)
.reduce((acc, _, index) => {
acc[index] = React.createRef();
@hungdoansy
hungdoansy / remove-ads.js
Last active Sep 15, 2020
User script used in TamperMonkey to remove ads in Facebooks' news feed | Currently, working with the new theme in English
View remove-ads.js
// ==UserScript==
// @name Hide sponsored items
// @namespace whatisthis
// @version 0.1
// @description try to sanitize Facebook
// @author Sy Hung Doan
// @match https://www.facebook.com/*
// @match https://facebook.com/*
// @grant none
// ==/UserScript==
@hungdoansy
hungdoansy / .hyper.js
Last active Aug 12, 2020
Hyper configuration
View .hyper.js
// Future versions of Hyper may add additional config options,
// which will not automatically be merged into this file.
// See https://hyper.is#cfg for all currently supported options.
module.exports = {
config: {
// choose either `'stable'` for receiving highly polished,
// or `'canary'` for less polished but more frequent updates
updateChannel: "stable",