Skip to content

Instantly share code, notes, and snippets.

View almond-bongbong's full-sized avatar
💭
focusing

Max almond-bongbong

💭
focusing
View GitHub Profile
@almond-bongbong
almond-bongbong / transformOrientationFile.ts
Created October 27, 2020 07:47
Transform orientation file object
export const transformOrientationFile = (file: File): Promise<File> => (
new Promise<File>(((resolve) => {
loadImage(file, (img) => {
(img as HTMLCanvasElement).toBlob((blob) => {
if (blob) {
const newFile = new File([blob], file.name, { ...file });
resolve(newFile);
}
}, file.type);
},
@almond-bongbong
almond-bongbong / getBase64ByFile.ts
Last active October 27, 2020 07:48
File object to base64 string
export const getBase64 = (file: File | Blob): Promise<string> => (
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (): void => {
resolve(reader.result as string);
};
reader.onerror = (error): void => {
reject(error);
};
const searchParams = location.search
.replace('?', '')
.split('&')
.map(pair => pair.split('='))
.reduce((acc, [k, v]) => {
acc[k] = decodeURIComponent(v);
return acc;
}, {});
@almond-bongbong
almond-bongbong / useDetectScrollEnd.js
Created June 26, 2019 01:51
custom hook for detecting scroll end
import { useState, useRef, useEffect, useCallback } from 'react';
import throttle from 'lodash/throttle';
const useDetectScrollEnd = (endPercent = 0.9) => {
const scrollEnded = useRef(false);
const [isScrollEnd, setIsScrollEnd] = useState(false);
const handleScroll = useCallback(throttle(() => {
const { scrollY, innerHeight } = window;
const scrollPercent = (scrollY + innerHeight) / document.body.scrollHeight;
export const getTodayArray = (calc = 0) => {
const today = new Date();
const calcTime = new Date(today.getFullYear(), today.getMonth(), today.getDate() + calc);
const year = calcTime.getFullYear();
const month = calcTime.getMonth() + 1;
const day = calcTime.getDate();
return [year, month, day];
};
export const getYearMonth = ({ day = 0, month = 0 } = {}) => {
export const numberWithCommas = x => x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
export default {};
// Milliseconds
function sleep_ms(millisecs) {
var initiation = new Date().getTime();
while ((new Date().getTime() - initiation) < millisecs);
}
// Seconds
function sleep_s(secs) {
secs = (+new Date) + secs * 1000;
while ((+new Date) < secs);
// <input type="text" id="email">
// <input type="text" id="name">
// <input type="text" id="phone">
// <div id="notice"></div>
function show(text) {
document.getElementById('notice').innerHTML = text;
}
function set() {
@almond-bongbong
almond-bongbong / hoisting_exam_05.js
Created September 20, 2018 08:03
good case , ie9 <=
// <input type="text" id="email">
// <input type="text" id="name">
// <input type="text" id="phone">
// <div id="notice"></div>
function show(text) {
document.getElementById('notice').innerHTML = text;
}
function set() {