Skip to content

Commit e590910

Browse files
committed
Add delimiter option to change-case
1 parent 907cf6f commit e590910

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed

packages/change-case/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Included case functions:
3535

3636
All methods accept an `options` object as the second argument:
3737

38+
- `delimiter?: string` The character to use between words. Default depends on method, e.g. `_` in snake case.
3839
- `locale?: string[] | string | false` Lower/upper according to specified locale, defaults to host environment. Set to `false` to disable.
3940
- `separateNumbers?: boolean` Splits `foo123` into `foo 123` instead of keeping them together. Defaults to `true`.
4041
- `prefixCharacters?: string` Retain at the beginning of the string. Defaults to `""`. Example: use `"_"` to keep the underscores in `__typename`.

packages/change-case/src/index.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,27 @@ const tests: [string, Result, Options?][] = [
104104
trainCase: "Test-String",
105105
},
106106
],
107+
[
108+
"Test String",
109+
{
110+
split: ["Test", "String"],
111+
camelCase: "test$String",
112+
capitalCase: "Test$String",
113+
constantCase: "TEST$STRING",
114+
dotCase: "test$string",
115+
kebabCase: "test$string",
116+
noCase: "test$string",
117+
pascalCase: "Test$String",
118+
pascalSnakeCase: "Test$String",
119+
pathCase: "test$string",
120+
sentenceCase: "Test$string",
121+
snakeCase: "test$string",
122+
trainCase: "Test$String",
123+
},
124+
{
125+
delimiter: "$",
126+
},
127+
],
107128
[
108129
"TestV2",
109130
{

packages/change-case/src/index.ts

+20-37
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type Locale = string[] | string | false | undefined;
2121

2222
export interface Options extends SplitOptions {
2323
locale?: Locale;
24+
delimiter?: string;
2425
prefixCharacters?: string;
2526
}
2627

@@ -67,7 +68,7 @@ export function noCase(input: string, options?: Options) {
6768
prefix +
6869
split(input, options)
6970
.map(lowerFactory(options?.locale))
70-
.join(" ")
71+
.join(options?.delimiter ?? " ")
7172
);
7273
}
7374

@@ -86,7 +87,7 @@ export function camelCase(input: string, options?: Options) {
8687
if (index === 0) return lower(word);
8788
return transform(word, index);
8889
})
89-
.join("")
90+
.join(options?.delimiter ?? "")
9091
);
9192
}
9293

@@ -99,23 +100,17 @@ export function pascalCase(input: string, options?: Options) {
99100
const upper = upperFactory(options?.locale);
100101
return (
101102
prefix +
102-
split(input, options).map(pascalCaseTransformFactory(lower, upper)).join("")
103+
split(input, options)
104+
.map(pascalCaseTransformFactory(lower, upper))
105+
.join(options?.delimiter ?? "")
103106
);
104107
}
105108

106109
/**
107110
* Convert a string to pascal snake case (`Foo_Bar`).
108111
*/
109112
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 });
119114
}
120115

121116
/**
@@ -129,7 +124,7 @@ export function capitalCase(input: string, options?: Options) {
129124
prefix +
130125
split(input, options)
131126
.map(capitalCaseTransformFactory(lower, upper))
132-
.join(" ")
127+
.join(options?.delimiter ?? " ")
133128
);
134129
}
135130

@@ -138,35 +133,33 @@ export function capitalCase(input: string, options?: Options) {
138133
*/
139134
export function constantCase(input: string, options?: Options) {
140135
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+
);
143142
}
144143

145144
/**
146145
* Convert a string to dot case (`foo.bar`).
147146
*/
148147
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 });
152149
}
153150

154151
/**
155152
* Convert a string to kebab case (`foo-bar`).
156153
*/
157154
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 });
161156
}
162157

163158
/**
164159
* Convert a string to path case (`foo/bar`).
165160
*/
166161
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 });
170163
}
171164

172165
/**
@@ -184,32 +177,22 @@ export function sentenceCase(input: string, options?: Options) {
184177
if (index === 0) return transform(word);
185178
return lower(word);
186179
})
187-
.join(" ")
180+
.join(options?.delimiter ?? " ")
188181
);
189182
}
190183

191184
/**
192185
* Convert a string to snake case (`foo_bar`).
193186
*/
194187
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 });
198189
}
199190

200191
/**
201192
* Convert a string to header case (`Foo-Bar`).
202193
*/
203194
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 });
213196
}
214197

215198
function lowerFactory(locale: Locale): (input: string) => string {

0 commit comments

Comments
 (0)