-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle snake case of ABcD which should become a_bc_d (#7883)
this also simplifies the regular expressions
- Loading branch information
1 parent
a1c6bb0
commit eb680f9
Showing
2 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { expect } from "chai"; | ||
import { snakeCase } from "../../../src/util/StringUtils"; | ||
|
||
describe("StringUtils", () => { | ||
describe("snakeCase", () => { | ||
it("should convert camelcase to snakecase", () => { | ||
const input = "camelCaseStringHere"; | ||
const expected = "camel_case_string_here"; | ||
const actual = snakeCase(input); | ||
|
||
expect(actual).to.be.equal(expected, `Failed for Input: ${input}`); | ||
}); | ||
|
||
it("should correctly convert an initial capital", () => { | ||
const input = "CamelCaseStringHere"; | ||
const expected = "camel_case_string_here"; | ||
const actual = snakeCase(input); | ||
|
||
expect(actual).to.be.equal(expected, `Failed for Input: ${input}`); | ||
}); | ||
|
||
it("should correctly convert strings of capitals", () => { | ||
const input = "testABCItem"; | ||
const expected = "test_abc_item"; | ||
const actual = snakeCase(input) | ||
|
||
expect(actual).to.be.equal(expected, `Failed for Input: ${input}`); | ||
}); | ||
|
||
it("should correctly convert repeating camelcase groups", () => { | ||
const input = "optionAOrB"; | ||
const expected = "option_a_or_b"; | ||
const actual = snakeCase(input); | ||
|
||
expect(actual).to.be.equal(expected, `Failed for Input: ${input}`); | ||
}); | ||
|
||
it("should do nothing with strings that are already snakecase", () => { | ||
const expected = "snake_case_string_here"; | ||
expect(snakeCase(expected)).to.be.equal(expected, expected); | ||
}); | ||
|
||
it("should correctly convert mixed strings into snakecase", () => { | ||
const input = "optionAOr_BOr_C"; | ||
const expected = "option_a_or_b_or_c"; | ||
const actual = snakeCase(input); | ||
|
||
expect(actual).to.be.equal(expected, `Failed for Input: ${input}`); | ||
}); | ||
|
||
it("should match the examples given in the older implementation", () => { | ||
// Pulled from https://regex101.com/r/QeSm2I/1 | ||
const examples = { | ||
"AbcItem": "abc_item", | ||
"ABCItem": "abc_item", | ||
"TestAbcItem": "test_abc_item", | ||
"testABCItem": "test_abc_item", | ||
"TestItemAbc": "test_item_abc", | ||
"TestItemABC": "test_item_abc", | ||
"abcItem": "abc_item", | ||
}; | ||
|
||
for (const [ input, expected] of Object.entries(examples)) { | ||
const actual = snakeCase(input); | ||
expect(actual).to.be.equal(expected, `Failed for Input: ${input}`); | ||
} | ||
}) | ||
}); | ||
}); |
eb680f9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is Actually a Breaking change for our Repository
our Entity is named "Fail2Ban"
old implementation => fail2_ban
new Implementation => fail2ban