-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Switch case dependency to internal helper (#296)
* feat: Switch case dependency to internal helper * Add better tests * Fold eth2 case handling into strings object * Update tests * lint
- Loading branch information
Showing
4 changed files
with
60 additions
and
11 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
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,31 @@ | ||
// Convert camelCase strings to various other formats -- assumes input field is camelCase or camel1Case | ||
|
||
export const Case = { | ||
snake: (field: string): string => | ||
field | ||
.replaceAll(/[^0-z]/g, "") | ||
.replaceAll(/[a-z][A-Z]|[0-9][A-Z]/g, (substr) => substr[0] + "_" + substr[1].toLowerCase()), | ||
constant: (field: string): string => | ||
field | ||
.replaceAll(/[^0-z]/g, "") | ||
.replaceAll(/[a-z][A-Z]|[0-9][A-Z]/g, (substr) => substr[0] + "_" + substr[1]) | ||
.toUpperCase(), | ||
pascal: (field: string): string => { | ||
const first = field[0].toUpperCase(); | ||
return (first + field.slice(1)).replaceAll(/[^0-z]/g, ""); | ||
}, | ||
camel: (field: string): string => { | ||
return field[0].toLowerCase() + field.slice(1); | ||
}, | ||
header: (field: string): string => { | ||
const first = field[0].toUpperCase(); | ||
return ( | ||
first + | ||
field | ||
.slice(1) | ||
.replaceAll(/[^0-z]/g, "") | ||
.replaceAll(/[a-z][A-Z]|[0-9][A-Z]/g, (substr) => substr[0] + "-" + substr[1]) | ||
); | ||
}, | ||
eth2: (field: string): string => Case.snake(field).replace(/(\d)$/, "_$1"), | ||
}; |
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,25 @@ | ||
import {expect} from "chai"; | ||
|
||
import {Case} from "../../src/util/strings"; | ||
import {KeyCase} from "../../src/type/container"; | ||
const testCases = ["fooBar", "foo1Bar", "fooBarBaz", "foo1Bar2Baz", "fooBar1"]; | ||
const expectedResults: {[key in KeyCase]: string[]} = { | ||
snake: ["foo_bar", "foo1_bar", "foo_bar_baz", "foo1_bar2_baz", "foo_bar1"], | ||
eth2: ["foo_bar", "foo1_bar", "foo_bar_baz", "foo1_bar2_baz", "foo_bar_1"], | ||
pascal: ["FooBar", "Foo1Bar", "FooBarBaz", "Foo1Bar2Baz", "FooBar1"], | ||
camel: ["fooBar", "foo1Bar", "fooBarBaz", "foo1Bar2Baz", "fooBar1"], | ||
constant: ["FOO_BAR", "FOO1_BAR", "FOO_BAR_BAZ", "FOO1_BAR2_BAZ", "FOO_BAR1"], | ||
header: ["Foo-Bar", "Foo1-Bar", "Foo-Bar-Baz", "Foo1-Bar2-Baz", "Foo-Bar1"], | ||
}; | ||
|
||
const keys = Object.keys(expectedResults) as KeyCase[]; | ||
for (const key of keys) { | ||
describe(`should convert string to ${key} case`, () => { | ||
for (let x = 0; x < 5; x++) { | ||
const field: string = Case[key as KeyCase](testCases[x]); | ||
it(`should convert to ${expectedResults[key as KeyCase][x]}`, () => { | ||
expect(field).to.equal(expectedResults[key][x]); | ||
}); | ||
} | ||
}); | ||
} |
900811c
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.
Possible performance regression was detected for some benchmarks.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold.
Full benchmark results