@@ -21,6 +21,7 @@ export type Locale = string[] | string | false | undefined;
21
21
22
22
export interface Options extends SplitOptions {
23
23
locale ?: Locale ;
24
+ delimiter ?: string ;
24
25
prefixCharacters ?: string ;
25
26
}
26
27
@@ -67,7 +68,7 @@ export function noCase(input: string, options?: Options) {
67
68
prefix +
68
69
split ( input , options )
69
70
. map ( lowerFactory ( options ?. locale ) )
70
- . join ( " " )
71
+ . join ( options ?. delimiter ?? " " )
71
72
) ;
72
73
}
73
74
@@ -86,7 +87,7 @@ export function camelCase(input: string, options?: Options) {
86
87
if ( index === 0 ) return lower ( word ) ;
87
88
return transform ( word , index ) ;
88
89
} )
89
- . join ( "" )
90
+ . join ( options ?. delimiter ?? "" )
90
91
) ;
91
92
}
92
93
@@ -99,23 +100,17 @@ export function pascalCase(input: string, options?: Options) {
99
100
const upper = upperFactory ( options ?. locale ) ;
100
101
return (
101
102
prefix +
102
- split ( input , options ) . map ( pascalCaseTransformFactory ( lower , upper ) ) . join ( "" )
103
+ split ( input , options )
104
+ . map ( pascalCaseTransformFactory ( lower , upper ) )
105
+ . join ( options ?. delimiter ?? "" )
103
106
) ;
104
107
}
105
108
106
109
/**
107
110
* Convert a string to pascal snake case (`Foo_Bar`).
108
111
*/
109
112
export function pascalSnakeCase ( input : string , options ?: Options ) {
110
- const prefix = getPrefix ( input , options ?. prefixCharacters ) ;
111
- const lower = lowerFactory ( options ?. locale ) ;
112
- const upper = upperFactory ( options ?. locale ) ;
113
- return (
114
- prefix +
115
- split ( input , options )
116
- . map ( capitalCaseTransformFactory ( lower , upper ) )
117
- . join ( "_" )
118
- ) ;
113
+ return capitalCase ( input , { delimiter : "_" , ...options } ) ;
119
114
}
120
115
121
116
/**
@@ -129,7 +124,7 @@ export function capitalCase(input: string, options?: Options) {
129
124
prefix +
130
125
split ( input , options )
131
126
. map ( capitalCaseTransformFactory ( lower , upper ) )
132
- . join ( " " )
127
+ . join ( options ?. delimiter ?? " " )
133
128
) ;
134
129
}
135
130
@@ -138,35 +133,33 @@ export function capitalCase(input: string, options?: Options) {
138
133
*/
139
134
export function constantCase ( input : string , options ?: Options ) {
140
135
const prefix = getPrefix ( input , options ?. prefixCharacters ) ;
141
- const upper = upperFactory ( options ?. locale ) ;
142
- return prefix + split ( input , options ) . map ( upper ) . join ( "_" ) ;
136
+ return (
137
+ prefix +
138
+ split ( input , options )
139
+ . map ( upperFactory ( options ?. locale ) )
140
+ . join ( options ?. delimiter ?? "_" )
141
+ ) ;
143
142
}
144
143
145
144
/**
146
145
* Convert a string to dot case (`foo.bar`).
147
146
*/
148
147
export function dotCase ( input : string , options ?: Options ) {
149
- const prefix = getPrefix ( input , options ?. prefixCharacters ) ;
150
- const lower = lowerFactory ( options ?. locale ) ;
151
- return prefix + split ( input , options ) . map ( lower ) . join ( "." ) ;
148
+ return noCase ( input , { delimiter : "." , ...options } ) ;
152
149
}
153
150
154
151
/**
155
152
* Convert a string to kebab case (`foo-bar`).
156
153
*/
157
154
export function kebabCase ( input : string , options ?: Options ) {
158
- const prefix = getPrefix ( input , options ?. prefixCharacters ) ;
159
- const lower = lowerFactory ( options ?. locale ) ;
160
- return prefix + split ( input , options ) . map ( lower ) . join ( "-" ) ;
155
+ return noCase ( input , { delimiter : "-" , ...options } ) ;
161
156
}
162
157
163
158
/**
164
159
* Convert a string to path case (`foo/bar`).
165
160
*/
166
161
export function pathCase ( input : string , options ?: Options ) {
167
- const prefix = getPrefix ( input , options ?. prefixCharacters ) ;
168
- const lower = lowerFactory ( options ?. locale ) ;
169
- return prefix + split ( input , options ) . map ( lower ) . join ( "/" ) ;
162
+ return noCase ( input , { delimiter : "/" , ...options } ) ;
170
163
}
171
164
172
165
/**
@@ -184,32 +177,22 @@ export function sentenceCase(input: string, options?: Options) {
184
177
if ( index === 0 ) return transform ( word ) ;
185
178
return lower ( word ) ;
186
179
} )
187
- . join ( " " )
180
+ . join ( options ?. delimiter ?? " " )
188
181
) ;
189
182
}
190
183
191
184
/**
192
185
* Convert a string to snake case (`foo_bar`).
193
186
*/
194
187
export function snakeCase ( input : string , options ?: Options ) {
195
- const prefix = getPrefix ( input , options ?. prefixCharacters ) ;
196
- const lower = lowerFactory ( options ?. locale ) ;
197
- return prefix + split ( input , options ) . map ( lower ) . join ( "_" ) ;
188
+ return noCase ( input , { delimiter : "_" , ...options } ) ;
198
189
}
199
190
200
191
/**
201
192
* Convert a string to header case (`Foo-Bar`).
202
193
*/
203
194
export function trainCase ( input : string , options ?: Options ) {
204
- const prefix = getPrefix ( input , options ?. prefixCharacters ) ;
205
- const lower = lowerFactory ( options ?. locale ) ;
206
- const upper = upperFactory ( options ?. locale ) ;
207
- return (
208
- prefix +
209
- split ( input , options )
210
- . map ( capitalCaseTransformFactory ( lower , upper ) )
211
- . join ( "-" )
212
- ) ;
195
+ return capitalCase ( input , { delimiter : "-" , ...options } ) ;
213
196
}
214
197
215
198
function lowerFactory ( locale : Locale ) : ( input : string ) => string {
0 commit comments