Last active: 5 months ago
remove query in url hash
/**
* 根据 key 删除 url 中 hash 中的指定 query
*
* #/?booth_id=Ep3wmDXbjk2&code=041Cax100CXyaS1mjI1005Tw1E3Cax1N
*
* @param hash url 中的 hash
* @param deleteKey 需要删除的 query 的 key
*/
export function removeHash(hash: string, deleteKey: string) {
if (hash.length < 3) return;
const body = hash
.slice(3)
// ['booth_id=Ep3wmDXbjk2', 'code=041Cax100CXyaS1mjI1005Tw1E3Cax1N']
.split('&')
// [{'booth_id': 'Ep3wmDXbjk2'}, {code: '041Cax100CXyaS1mjI1005Tw1E3Cax1N'}]
.map((query) => {
const string = query.split('=');
return {
[string[0]]: string[1],
};
Last active: 6 months ago
incraseNumber js javascript animation
/**
* 按间隔更新数字,数字增长的动画。
*
* @param start 动画开始的值
* @param end 动画最后结束的值
* @param updateFn 更新值状态的方法
* @param duration 动画总时长
* @param setpDuration 每次更新的间隔
* @param precision 数字精度
*
* 数字精度只会影响在动画时显示的值,不会影响最终值
*/
export function incraseNumber(
start = 0,
end: number,
updateFn: (state: number) => void,
duration = 500,
setpDuration = 25,
precision = 0
) {
Last active: 7 months ago
Byte iterator
struct ByteIter<'remainder> {
remainder: &'remainder [u8],
}
/* impl<'remainder> ByteIter<'remainder> {
fn next(&mut self) -> Option<&'remainder u8> {
if self.remainder.is_empty() {
None
} else {
let byte = &self.remainder[0];
self.remainder = &self.remainder[1..];
Some(byte)
}
}
} */
impl<'remainder> Iterator for ByteIter<'remainder> {
type Item = &'remainder u8;
fn next(&mut self) -> Option<Self::Item> {
if self.remainder.is_empty() {
Last active: 7 months ago
Build css module className
/**
* clsx
*/
export function cn(...rest: string[]) {
return rest.join(' ');
}
/**
* 构建 cn 函数
*
* Usage:
*
* ```ts
* import styles from 'index.module.less';
* const cn = buildCN(styles);
* <div className={cn('left', 'align-center')}>
* ```
*/
export function buildCN(module: typeof import('*.less')) {
return (...rest: string[]) => {
Last active: 7 months ago
Tooltip with CSS
.body {
display: flex;
}
.btn {
display: flex;
position: relative;
// &:hover {
// background: rgba(0, 0, 0, 0.15);
// }
}
.btn-right {
&::after {
position: absolute;
left: calc(100% + 0.14rem);
border-radius: 0.04rem;
padding: 0.05rem 0.06rem;
display: flex;
Last active: 7 months ago
Tooltip with CSS
import styles from '@/projects/viewer/Layouts/Default/Tooltip.module.less';
import { buildCN } from '@/utils';
import { nanoid } from 'nanoid';
import { memo, useMemo } from 'react';
const cn = buildCN(styles);
export type ToolTipProps = {
title: string;
position?: 'top' | 'right';
} & React.DetailedHTMLProps<
React.HTMLAttributes<HTMLDivElement>,
HTMLDivElement
>;
/**
* 使用 CSS 的 Tooltip
*/
const Tooltip = ({ title, position, children, ...rest }: ToolTipProps) => {
const id = useMemo(() => `more-btn-${nanoid()}`, []);
Last active: 8 months ago
rust memory vulnerability
fn main() {
let mut safety = &String::from("hello");
println!("address of safety {:p}: {}", &safety, safety);
{
let name = "xfy".to_string();
let name = expand(&name);
println!("address of name {:p}: {}", &name, name);
safety = &name;
}
println!("address of safety {:p}: {}", &safety, safety);
}
pub const fn lifetime_translator<'a, 'b, T>(_val_a: &'a &'b (), _val_b: &'b T) -> &'a T {
_val_b
}
pub fn lifetime_translator_mut<'a, 'b, T>(_val_a: &'a &'b (), _val_b: &'b mut T) -> &'a mut T {
_val_b
}
Last active: 9 months ago
rust argon2 password hash
use anyhow::{anyhow, Context};
use tokio::task;
use argon2::password_hash::SaltString;
use argon2::{password_hash, Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
pub async fn hash(password: String) -> anyhow::Result<String> {
task::spawn_blocking(move || {
let salt = SaltString::generate(rand::thread_rng());
Ok(Argon2::default()
.hash_password(password.as_bytes(), &salt)
.map_err(|e| anyhow!(e).context("failed to hash password"))?
.to_string())
})
.await
.context("panic in hash()")?
}
pub async fn verify(password: String, hash: String) -> anyhow::Result<bool> {
task::spawn_blocking(move || {
Last active: 9 months ago
count git commit by author
git log --since="2023-01-01" --until="2023-12-31" --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5
Last active: 9 months ago
rust async read tcp stream as header to string
pub async fn read_headers<R>(reader: R) -> Result<String>
where
R: AsyncRead + std::marker::Unpin,
{
let mut request_string = String::new();
let mut reader = BufReader::new(reader);
loop {
let byte = reader.read_line(&mut request_string).await?;
if byte < 3 {
break;
}
}
Ok(request_string)
}
Last active: 9 months ago
rust accept async function in generic
use std::{collections::HashMap, future::Future};
pub struct Rymo<'a, F, Fut>
where
F: FnOnce() -> Fut + 'static + Send + Sync,
Fut: Future<Output = ()>,
{
pub port: String,
pub handle: HashMap<&'a str, F>,
}