File tree 2 files changed +27
-1
lines changed
2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -40,3 +40,13 @@ export function capitalize(str: string): string {
40
40
export function kebabToCamelCase ( str : string ) : string {
41
41
return str . replace ( / - ./ g, ( match ) => match . charAt ( 1 ) . toUpperCase ( ) ) ;
42
42
}
43
+
44
+ /**
45
+ * Converts a camelCase string to snake_case.
46
+ *
47
+ * @param str The camelCase string to convert.
48
+ * @returns The snake_case string.
49
+ */
50
+ export function camelToSnakeCase ( str : string ) : string {
51
+ return str . replace ( / [ A - Z ] / g, ( match ) => `_${ match . toLowerCase ( ) } ` ) ;
52
+ }
Original file line number Diff line number Diff line change 1
1
import assert from "node:assert/strict" ;
2
2
import { describe , it } from "node:test" ;
3
3
4
- import { pluralize , capitalize , kebabToCamelCase } from "../src/string.js" ;
4
+ import {
5
+ pluralize ,
6
+ capitalize ,
7
+ kebabToCamelCase ,
8
+ camelToSnakeCase ,
9
+ } from "../src/string.js" ;
5
10
6
11
describe ( "string" , ( ) => {
7
12
describe ( "pluralize" , ( ) => {
@@ -44,4 +49,15 @@ describe("string", () => {
44
49
) ;
45
50
} ) ;
46
51
} ) ;
52
+
53
+ describe ( "camelToSnakeCase" , ( ) => {
54
+ it ( "Should convert a camelCase string to snake_case" , ( ) => {
55
+ assert . equal ( camelToSnakeCase ( "camelcasestring" ) , "camelcasestring" ) ;
56
+ assert . equal ( camelToSnakeCase ( "camelCaseString" ) , "camel_case_string" ) ;
57
+ assert . equal (
58
+ camelToSnakeCase ( "camelCASESTRING" ) ,
59
+ "camel_c_a_s_e_s_t_r_i_n_g" ,
60
+ ) ;
61
+ } ) ;
62
+ } ) ;
47
63
} ) ;
You can’t perform that action at this time.
0 commit comments