添加一些utils
This commit is contained in:
parent
a04e057119
commit
cae4f74f6c
12
packages/react/package.json
Normal file
12
packages/react/package.json
Normal 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"
|
||||
}
|
11
packages/ui/src/utils/extra.ts
Normal file
11
packages/ui/src/utils/extra.ts
Normal 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;
|
||||
}
|
22
packages/ui/src/utils/history.ts
Normal file
22
packages/ui/src/utils/history.ts
Normal 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();
|
9
packages/ui/src/utils/is-null.ts
Normal file
9
packages/ui/src/utils/is-null.ts
Normal 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;
|
||||
};
|
@ -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);
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user