Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tiny-dodos-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Honor custom split and strip regular expressions passed to `String.noCase`.
15 changes: 11 additions & 4 deletions packages/effect/src/String.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1234,23 +1234,30 @@ export const noCase: {
readonly delimiter?: string | undefined
readonly transform?: (part: string, index: number, parts: ReadonlyArray<string>) => string
}): string => {
const splitRegExp = toRegExpArray(options?.splitRegExp ?? SPLIT_REGEXP)
const stripRegExp = toRegExpArray(options?.stripRegExp ?? STRIP_REGEXP)
const delimiter = options?.delimiter ?? " "
const transform = options?.transform ?? toLowerCase
return normalizeCase(input, SPLIT_REGEXP, STRIP_REGEXP, delimiter, transform)
return normalizeCase(input, splitRegExp, stripRegExp, delimiter, transform)
})

const toRegExpArray = (regexp: RegExp | ReadonlyArray<RegExp>): ReadonlyArray<RegExp> =>
predicate.isRegExp(regexp) ? [regexp] : regexp

const normalizeCase = (
input: string,
splitRegExp: ReadonlyArray<RegExp>,
stripRegExp: RegExp,
stripRegExp: ReadonlyArray<RegExp>,
delimiter: string,
transform: (part: string, index: number, parts: ReadonlyArray<string>) => string
): string => {
let result = input
for (const regexp of splitRegExp) {
result = result.replace(regexp, "$1\0$2")
}
result = result.replace(stripRegExp, "\0")
for (const regexp of stripRegExp) {
result = result.replace(regexp, "\0")
}
let start = 0
let end = result.length
// Trim the delimiter from around the output string.
Expand Down Expand Up @@ -1370,7 +1377,7 @@ export const constantCase: (self: string) => string = noCase({
* @since 4.0.0
*/
export const configCase: (self: string) => string = (self) =>
normalizeCase(self, CONFIG_SPLIT_REGEXP, STRIP_REGEXP, "_", toUpperCase)
normalizeCase(self, CONFIG_SPLIT_REGEXP, [STRIP_REGEXP], "_", toUpperCase)

/**
* Converts a string to kebab-case (lowercase with hyphens).
Expand Down
16 changes: 16 additions & 0 deletions packages/effect/test/String.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,22 @@ describe("String", () => {
strictEqual(pipe("helloWorld", S.noCase({ delimiter: "-" })), "hello-world")
})

it("uses a custom split regular expression", () => {
strictEqual(S.noCase("ab", { splitRegExp: /([a])([b])/g }), "a b")
})

it("uses custom split regular expressions", () => {
strictEqual(S.noCase("abc", { splitRegExp: [/([a])([b])/g, /([b])([c])/g] }), "a b c")
})

it("uses a custom strip regular expression", () => {
strictEqual(S.noCase("a_b-c", { stripRegExp: /_/g }), "a b-c")
})

it("uses custom strip regular expressions", () => {
strictEqual(S.noCase("a_b-c", { stripRegExp: [/_/g, /-/g] }), "a b c")
})

it("handles underscores and hyphens", () => {
strictEqual(S.noCase("hello_world"), "hello world")
strictEqual(S.noCase("hello-world"), "hello world")
Expand Down
Loading