@nickyzj2023/utils 1.0.37 → 1.0.38
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.
- package/biome.json +1 -1
- package/package.json +4 -4
- package/src/string.ts +34 -8
package/biome.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nickyzj2023/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.38",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -9,12 +9,12 @@
|
|
|
9
9
|
"build": "bun build --target=bun --outdir ./dist --minify ./src/index.ts && tsc && bun run docs"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"@biomejs/biome": "^2.3.
|
|
13
|
-
"@types/bun": "
|
|
12
|
+
"@biomejs/biome": "^2.3.13",
|
|
13
|
+
"@types/bun": "^1.3.8",
|
|
14
14
|
"typedoc": "^0.28.16"
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"typescript": "^5.
|
|
17
|
+
"typescript": "^5.9.3"
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
package/src/string.ts
CHANGED
|
@@ -1,46 +1,72 @@
|
|
|
1
1
|
import { fetcher } from "./network";
|
|
2
2
|
|
|
3
|
+
type SnakeToCamel<S extends string> =
|
|
4
|
+
S extends `${infer P1}_${infer P2}${infer P3}`
|
|
5
|
+
? `${P1}${Capitalize<SnakeToCamel<`${P2}${P3}`>>}`
|
|
6
|
+
: S;
|
|
7
|
+
|
|
3
8
|
/**
|
|
4
9
|
* 下划线命名法转为驼峰命名法
|
|
5
10
|
*
|
|
6
11
|
* @example
|
|
7
12
|
* snakeToCamel("user_name") // "userName"
|
|
8
13
|
*/
|
|
9
|
-
export const snakeToCamel = (str:
|
|
10
|
-
return str.replace(/_([a-zA-Z])/g, (match, pattern) =>
|
|
14
|
+
export const snakeToCamel = <S extends string>(str: S): SnakeToCamel<S> => {
|
|
15
|
+
return str.replace(/_([a-zA-Z])/g, (match, pattern) =>
|
|
16
|
+
pattern.toUpperCase(),
|
|
17
|
+
) as SnakeToCamel<S>;
|
|
11
18
|
};
|
|
12
19
|
|
|
20
|
+
type CamelToSnake<S extends string> =
|
|
21
|
+
S extends `${infer P1}${infer P2}${infer Rest}`
|
|
22
|
+
? P2 extends Capitalize<P2>
|
|
23
|
+
? `${P1}_${Lowercase<P2>}${CamelToSnake<Rest>}`
|
|
24
|
+
: `${P1}${CamelToSnake<`${P2}${Rest}`>}`
|
|
25
|
+
: S;
|
|
26
|
+
|
|
13
27
|
/**
|
|
14
28
|
* 驼峰命名法转为下划线命名法
|
|
15
29
|
*
|
|
16
30
|
* @example
|
|
17
31
|
* camelToSnake("shouldComponentUpdate") // "should_component_update"
|
|
18
32
|
*/
|
|
19
|
-
export const camelToSnake = (str:
|
|
33
|
+
export const camelToSnake = <S extends string>(str: S): CamelToSnake<S> => {
|
|
20
34
|
return str.replace(
|
|
21
35
|
/([A-Z])/g,
|
|
22
36
|
(match, pattern) => `_${pattern.toLowerCase()}`,
|
|
23
|
-
)
|
|
37
|
+
) as CamelToSnake<S>;
|
|
24
38
|
};
|
|
25
39
|
|
|
40
|
+
type Capitalize<S extends string> = S extends `${infer P1}${infer Rest}`
|
|
41
|
+
? P1 extends Capitalize<P1>
|
|
42
|
+
? S
|
|
43
|
+
: `${Uppercase<P1>}${Rest}`
|
|
44
|
+
: S;
|
|
45
|
+
|
|
26
46
|
/**
|
|
27
47
|
* 字符串首字母大写
|
|
28
48
|
*
|
|
29
49
|
* @example
|
|
30
50
|
* capitalize("hello") // "Hello"
|
|
31
51
|
*/
|
|
32
|
-
export const capitalize = (s:
|
|
33
|
-
return s.charAt(0).toUpperCase() + s.slice(1)
|
|
52
|
+
export const capitalize = <S extends string>(s: S): Capitalize<S> => {
|
|
53
|
+
return (s.charAt(0).toUpperCase() + s.slice(1)) as Capitalize<S>;
|
|
34
54
|
};
|
|
35
55
|
|
|
56
|
+
type Decapitalize<S extends string> = S extends `${infer P1}${infer Rest}`
|
|
57
|
+
? P1 extends Lowercase<P1>
|
|
58
|
+
? P1
|
|
59
|
+
: `${Lowercase<P1>}${Rest}`
|
|
60
|
+
: S;
|
|
61
|
+
|
|
36
62
|
/**
|
|
37
63
|
* 字符串首字母小写
|
|
38
64
|
*
|
|
39
65
|
* @example
|
|
40
66
|
* decapitalize("Hello") // "hello"
|
|
41
67
|
*/
|
|
42
|
-
export const decapitalize = (s:
|
|
43
|
-
return s.charAt(0).toLowerCase() + s.slice(1)
|
|
68
|
+
export const decapitalize = <S extends string>(s: S): Decapitalize<S> => {
|
|
69
|
+
return (s.charAt(0).toLowerCase() + s.slice(1)) as Decapitalize<S>;
|
|
44
70
|
};
|
|
45
71
|
|
|
46
72
|
/**
|