feat(medusa, types): Improve DX of model extensions (#4398)

This commit is contained in:
Riqwan Thamir
2023-06-29 13:45:16 +02:00
committed by GitHub
parent 1e88b4d5d9
commit 9760d4a96c
17 changed files with 405 additions and 39 deletions
@@ -0,0 +1,36 @@
import { toCamelCase } from "../to-camel-case"
describe("toCamelCase", function () {
it("should convert all cases to camel case", function () {
const expectations = [
{
input: "testing-camelize",
output: "testingCamelize",
},
{
input: "testing-Camelize",
output: "testingCamelize",
},
{
input: "TESTING-CAMELIZE",
output: "testingCamelize",
},
{
input: "this_is-A-test",
output: "thisIsATest",
},
{
input: "this_is-A-test ANOTHER",
output: "thisIsATestAnother",
},
{
input: "testingAlreadyCamelized",
output: "testingAlreadyCamelized",
},
]
expectations.forEach((expectation) => {
expect(toCamelCase(expectation.input)).toEqual(expectation.output)
})
})
})
@@ -0,0 +1,36 @@
import { upperCaseFirst } from "../upper-case-first"
describe("upperCaseFirst", function () {
it("should convert first letter of the word to capital letter", function () {
const expectations = [
{
input: "testing capitalize",
output: "Testing capitalize",
},
{
input: "testing",
output: "Testing",
},
{
input: "Testing",
output: "Testing",
},
{
input: "TESTING",
output: "TESTING",
},
{
input: "t",
output: "T",
},
{
input: "",
output: "",
},
]
expectations.forEach((expectation) => {
expect(upperCaseFirst(expectation.input)).toEqual(expectation.output)
})
})
})
+2
View File
@@ -8,12 +8,14 @@ export * from "./is-email"
export * from "./is-object"
export * from "./is-string"
export * from "./lower-case-first"
export * from "./upper-case-first"
export * from "./medusa-container"
export * from "./object-to-string-path"
export * from "./set-metadata"
export * from "./simple-hash"
export * from "./wrap-handler"
export * from "./to-kebab-case"
export * from "./to-camel-case"
export * from "./stringify-circular"
export * from "./build-query"
export * from "./handle-postgres-database-error"
@@ -0,0 +1,7 @@
export function toCamelCase(str: string): string {
return /^([a-z]+)(([A-Z]([a-z]+))+)$/.test(str)
? str
: str
.toLowerCase()
.replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase())
}
@@ -0,0 +1,3 @@
export function upperCaseFirst(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1)
}