@nickyzj2023/utils 1.0.37 → 1.0.39

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "$schema": "https://biomejs.dev/schemas/2.3.12/schema.json",
2
+ "$schema": "https://biomejs.dev/schemas/2.3.13/schema.json",
3
3
  "vcs": {
4
4
  "enabled": true,
5
5
  "clientKind": "git",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nickyzj2023/utils",
3
- "version": "1.0.37",
3
+ "version": "1.0.39",
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.12",
13
- "@types/bun": "latest",
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.8.2"
17
+ "typescript": "^5.9.3"
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",
package/src/string.ts CHANGED
@@ -1,46 +1,73 @@
1
1
  import { fetcher } from "./network";
2
2
 
3
+ export 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: string) => {
10
- return str.replace(/_([a-zA-Z])/g, (match, pattern) => pattern.toUpperCase());
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
+ export 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: string) => {
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
+ export 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: string) => {
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
+ export type Decapitalize<S extends string> =
57
+ S extends `${infer P1}${infer Rest}`
58
+ ? P1 extends Lowercase<P1>
59
+ ? P1
60
+ : `${Lowercase<P1>}${Rest}`
61
+ : S;
62
+
36
63
  /**
37
64
  * 字符串首字母小写
38
65
  *
39
66
  * @example
40
67
  * decapitalize("Hello") // "hello"
41
68
  */
42
- export const decapitalize = (s: string) => {
43
- return s.charAt(0).toLowerCase() + s.slice(1);
69
+ export const decapitalize = <S extends string>(s: S): Decapitalize<S> => {
70
+ return (s.charAt(0).toLowerCase() + s.slice(1)) as Decapitalize<S>;
44
71
  };
45
72
 
46
73
  /**