@anjianshi/utils 2.2.7 → 2.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ /**
2
+ * 通过 React Hook 把 emotion css 转换成 className
3
+ * (不再需要 <ClassName>)
4
+ *
5
+ * 使用前提:
6
+ * 1. 只支持浏览器渲染
7
+ * 2. 用 EmotionCacheProvider 包裹 App 根元素
8
+ *
9
+ * 来自:
10
+ * https://github.com/emotion-js/emotion/issues/1853#issuecomment-623349622
11
+ */
12
+ import { type EmotionCache } from '@emotion/react';
13
+ import { type CSSInterpolation } from '@emotion/serialize';
14
+ export declare const useEmotionCache: () => EmotionCache | undefined;
15
+ export declare const EmotionCacheProvider: import("react").FC<{
16
+ children: React.ReactNode;
17
+ } & import("react").ClassAttributes<any>>;
18
+ export declare function useEmotionClassName(): (...args: CSSInterpolation[]) => string;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * 通过 React Hook 把 emotion css 转换成 className
3
+ * (不再需要 <ClassName>)
4
+ *
5
+ * 使用前提:
6
+ * 1. 只支持浏览器渲染
7
+ * 2. 用 EmotionCacheProvider 包裹 App 根元素
8
+ *
9
+ * 来自:
10
+ * https://github.com/emotion-js/emotion/issues/1853#issuecomment-623349622
11
+ */
12
+ import { withEmotionCache } from '@emotion/react';
13
+ import { serializeStyles } from '@emotion/serialize';
14
+ import { insertStyles } from '@emotion/utils';
15
+ import { createContext, useContext, useCallback } from 'react';
16
+ const CacheContext = createContext(undefined);
17
+ export const useEmotionCache = () => useContext(CacheContext);
18
+ export const EmotionCacheProvider = withEmotionCache(({ children }, cache) => {
19
+ return <CacheContext.Provider value={cache}>{children}</CacheContext.Provider>;
20
+ });
21
+ export function useEmotionClassName() {
22
+ const cache = useEmotionCache();
23
+ return useCallback((...args) => {
24
+ if (!cache) {
25
+ if (process.env.NODE_ENV === 'production') {
26
+ return 'emotion-cache-missing';
27
+ }
28
+ throw new Error('No emotion cache found!');
29
+ }
30
+ const serialized = serializeStyles(args, cache.registered);
31
+ insertStyles(cache, serialized, false);
32
+ return cache.key + '-' + serialized.name;
33
+ }, [cache]);
34
+ }
package/lang/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export * from './string.js';
4
4
  export * from './object.js';
5
5
  export * from './time.js';
6
6
  export * from './async.js';
7
+ export * from './random.js';
package/lang/index.js CHANGED
@@ -4,3 +4,4 @@ export * from './string.js';
4
4
  export * from './object.js';
5
5
  export * from './time.js';
6
6
  export * from './async.js';
7
+ export * from './random.js';
@@ -0,0 +1,12 @@
1
+ /**
2
+ * 返回随机数,包含 min 和 max
3
+ */
4
+ export declare function getRandomInt(min: number, max: number): number;
5
+ /**
6
+ * 返回随机字符串
7
+ */
8
+ export declare function getRandomString(length?: number, seed?: string): string;
9
+ /**
10
+ * 从给定的选择中随机选中一项
11
+ */
12
+ export declare function choiceRandom<T>(choices: T[]): NonNullable<T>;
package/lang/random.js ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * 返回随机数,包含 min 和 max
3
+ */
4
+ export function getRandomInt(min, max) {
5
+ const minCeiled = Math.ceil(min);
6
+ const maxFloored = Math.floor(max);
7
+ return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled);
8
+ }
9
+ /**
10
+ * 返回随机字符串
11
+ */
12
+ export function getRandomString(length = 6, seed = '0123456789abcdefghijklmnopqrstuvwxyz') {
13
+ let result = '';
14
+ while (result.length < length)
15
+ result += seed[getRandomInt(0, seed.length - 1)];
16
+ return result;
17
+ }
18
+ /**
19
+ * 从给定的选择中随机选中一项
20
+ */
21
+ export function choiceRandom(choices) {
22
+ return choices[getRandomInt(0, choices.length - 1)];
23
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anjianshi/utils",
3
- "version": "2.2.7",
3
+ "version": "2.2.8",
4
4
  "description": "Common JavaScript Utils",
5
5
  "homepage": "https://github.com/anjianshi/js-packages/utils",
6
6
  "bugs": {
@@ -21,7 +21,8 @@
21
21
  },
22
22
  "devDependencies": {
23
23
  "@types/lodash": "^4.17.7",
24
- "@types/node": "^20.15.0",
24
+ "@types/node": "^20.16.2",
25
+ "@types/react": "^18.3.5",
25
26
  "dotenv": "^16.4.5",
26
27
  "typeorm": "^0.3.20",
27
28
  "typescript": "^5.5.4",
@@ -29,11 +30,16 @@
29
30
  "@anjianshi/presets-eslint-node": "4.0.8",
30
31
  "@anjianshi/presets-eslint-typescript": "5.0.5",
31
32
  "@anjianshi/presets-typescript": "3.2.2",
33
+ "@anjianshi/presets-eslint-react": "4.0.7",
32
34
  "@anjianshi/presets-prettier": "3.0.1"
33
35
  },
34
36
  "peerDependencies": {
37
+ "@emotion/react": "^11.13.3",
38
+ "@emotion/serialize": "^1.3.1",
39
+ "@emotion/utils": "^1.4.0",
35
40
  "chalk": "^5.3.0",
36
- "dayjs": "^1.11.10"
41
+ "dayjs": "^1.11.10",
42
+ "react": "^18.3.1"
37
43
  },
38
44
  "peerDependenciesMeta": {
39
45
  "chalk": {
@@ -41,6 +47,18 @@
41
47
  },
42
48
  "dayjs": {
43
49
  "optional": true
50
+ },
51
+ "@emotion/react": {
52
+ "optional": true
53
+ },
54
+ "@emotion/serialize": {
55
+ "optional": true
56
+ },
57
+ "@emotion/utils": {
58
+ "optional": true
59
+ },
60
+ "react": {
61
+ "optional": true
44
62
  }
45
63
  },
46
64
  "eslintIgnore": [],