添加一些utils

This commit is contained in:
熊潇 2024-09-26 19:36:08 +08:00
parent a04e057119
commit cae4f74f6c
5 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,12 @@
{
"name": "react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

View File

@ -0,0 +1,11 @@
export function extractKeysFromBraces(text: string) {
const regex = /\{\{\s*(.*?)\s*\}\}/g;
const keys: string[] = [];
let matches: RegExpExecArray | null;
while ((matches = regex.exec(text)) !== null) {
keys.push(matches[1]); // 获取{{}}中间的key
}
return keys;
}

View File

@ -0,0 +1,22 @@
type To = string | Location;
type State<T> = {
[key: string]: any;
} & T;
export const push = <T = any>(to: To, state?: State<T>, refresh = true) => {
const _history = window.history;
if (typeof to === 'string') {
// must key is default, so react navigate can work
_history.pushState({ key: 'default', usr: state }, '', to);
} else {
// const path = to.pathname;
_history.pushState({ key: 'default', usr: state }, '', to.pathname);
}
// must dispatch popstate event, so react navigate can work
refresh && window.dispatchEvent(new Event('popstate'));
};
export const history = {
push,
};
// import { createBrowserHistory } from 'history';
// export const history = createBrowserHistory();

View File

@ -0,0 +1,9 @@
export const isObjectNull = (value: any) => {
if (value === null || value === undefined) {
return true;
}
if (JSON.stringify(value) === '{}') {
return true;
}
return false;
};

View File

@ -2,7 +2,13 @@ import { customAlphabet } from 'nanoid';
// 全小写的字母和数字
const alphabetLetter = 'abcdefghijklmnopqrstuvwxyz';
const alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
export const alphabetLetterAll = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
export const generateId6 = customAlphabet(alphabet, 6);
export const customNanoid = customAlphabet(alphabetLetterAll, 12);
export const generateId = (size = 6) => {
return 'b-' + generateId6(size);
};
export const generate = (size = 6) => {
return customNanoid(size);
};