Skip to content

Instantly share code, notes, and snippets.

View akirco's full-sized avatar
🎯
Focusing

akirco

🎯
Focusing
View GitHub Profile
@akirco
akirco / listen for an event once.js
Last active July 25, 2023 12:07
listen for an event once
// adds an event listener to ab element that will ibly run the callback the first time the event is triggered.
// - use EventTarget.addEventListener() to add an event listener to an element.
// - use {once:true} as options to only run the given callback once.
const listenOnce = (el, e, fn) => {
el.addEventListener(e, fn, { once: true });
};
listenOnce(document.getElementById("test"), "click", () =>
console.log("Hello world")
@akirco
akirco / go-cli-demo.md
Last active July 25, 2023 12:09
Golang Snippets
// ├───bin
// ├───models
// └───output

// module clitoy/upscale

// go 1.20

// require (
@akirco
akirco / generate_pwd.js
Last active July 25, 2023 12:08
snippets - generate random pwd
function generatePassword(length) {
let charset =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-+={}[]|\\:;\"'<>,.?/";
let password = "";
for (let i = 0; i < length; i++) {
let randomIndex = Math.floor(Math.random() * charset.length);
password += charset[randomIndex];
}
@akirco
akirco / dateFormate.js
Last active July 25, 2023 12:08
snippets - date formate
// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, // 月份
@akirco
akirco / pkg-build.js
Last active May 19, 2023 05:43
node.js-pkg build
const {
pkg_fetch_version,
node_version,
pkg_cache_path,
icon,
version,
description,
company,
name,
copyright,
@akirco
akirco / file slice upload.js
Last active May 19, 2023 05:46
file upload
/**
async (e: Event) => {
const chunkSize = 2 * 1024 * 1024;
const target = e.target as HTMLInputElement;
const targetFile = target.files![0];
const { type, name, size } = targetFile;
checkType(type);
let [index, start] = [0, 0];
while (start < size) {
@akirco
akirco / useSafeLocalStorage.ts
Created June 1, 2023 09:59
react hooks-useSafeLocalStorage
import { useState, useEffect } from "react";
export function useSafeLocalStorage(key: string, initialValue: any) {
const [valueProxy, setValueProxy] = useState(() => {
try {
const value = window.localStorage.getItem(key);
return value ? JSON.parse(value) : initialValue;
} catch {
return initialValue;
}
@akirco
akirco / usePrefersDarkMode.ts
Created June 1, 2023 12:56
react hooks- usePreferDarkMode
import { useEffect, useState } from "react";
export default function usePrefersDarkMode(defaultValue: boolean) {
const [value, setValue] = useState(() => defaultValue);
useEffect(() => {
if (typeof window !== "undefined") {
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
setValue(mediaQuery.matches);
@akirco
akirco / useDarkMode.ts
Created June 1, 2023 12:57
react hooks - useDarkMode
import { useEffect } from "react";
import usePrefersDarkMode from "./usePrefersDarkMode";
import { useSafeLocalStorage } from "./useSafeLocalStorage";
export function useDarkMode(isDark: boolean) {
const prefersDarkMode = usePrefersDarkMode(isDark);
const [isEnabled, setIsEnabled] = useSafeLocalStorage("dark-mode", isDark);
const enabled = isEnabled === undefined ? prefersDarkMode : isEnabled;
@akirco
akirco / getimgs.js
Created June 24, 2023 15:47
use set to cache repeat solutions
/**
* @description Get file tree by a mtp device parent path.
* @param parentPath The root address can be passed in the string `/`.
* @returns `Array<FileInfo>` File info array
* @example
*
* [{
* name: '1',
* size: 0,
* type: 'FOLDER',