-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Vendor in case package * Create a kebabToCamel JS function for locales * Update the lock file Signed-off-by: Olga Bulat <[email protected]> * Add suggestions from code review Signed-off-by: Olga Bulat <[email protected]> --------- Signed-off-by: Olga Bulat <[email protected]>
- Loading branch information
Showing
13 changed files
with
132 additions
and
29 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
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
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,71 @@ | ||
// Copied from https://github.com/blakeembrey/change-case | ||
// Case-related utility functions are vendored in because `case` package | ||
// cannot be used in Nuxt 3 which requires ESM compatibility. | ||
|
||
// Regexps involved with splitting words in various case formats. | ||
const SPLIT_LOWER_UPPER_RE = /([\p{Ll}\d])(\p{Lu})/gu | ||
const SPLIT_UPPER_UPPER_RE = /(\p{Lu})([\p{Lu}][\p{Ll}])/gu | ||
|
||
// Regexp involved with stripping non-word characters from the result. | ||
const DEFAULT_STRIP_REGEXP = /[^\p{L}\d]+/giu | ||
|
||
// The replacement value for splits. | ||
const SPLIT_REPLACE_VALUE = "$1\0$2" | ||
|
||
/** | ||
* Split any cased input strings into an array of words. | ||
*/ | ||
function split(input: string) { | ||
let result = input.trim() | ||
|
||
result = result | ||
.replace(SPLIT_LOWER_UPPER_RE, SPLIT_REPLACE_VALUE) | ||
.replace(SPLIT_UPPER_UPPER_RE, SPLIT_REPLACE_VALUE) | ||
.replace(DEFAULT_STRIP_REGEXP, "\0") | ||
|
||
let start = 0 | ||
let end = result.length | ||
|
||
// Trim the delimiter from around the output string. | ||
while (result.charAt(start) === "\0") start++ | ||
if (start === end) return [] | ||
while (result.charAt(end - 1) === "\0") end-- | ||
|
||
return result.slice(start, end).split(/\0/g) | ||
} | ||
|
||
function pascalCaseTransformFactory() { | ||
return (word: string, index: number) => { | ||
const char0 = word[0] | ||
const initial = | ||
index > 0 && char0 >= "0" && char0 <= "9" | ||
? "_" + char0 | ||
: char0.toUpperCase() | ||
return initial + word.slice(1).toLowerCase() | ||
} | ||
} | ||
|
||
function capitalCaseTransformFactory() { | ||
return (word: string) => | ||
`${word[0].toUpperCase()}${word.slice(1).toLowerCase()}` | ||
} | ||
|
||
/** | ||
* Convert a string to camel case (`fooBar`). | ||
*/ | ||
export function camelCase(input: string) { | ||
const transform = pascalCaseTransformFactory() | ||
return split(input) | ||
.map((word, index) => { | ||
if (index === 0) return word.toLowerCase() | ||
return transform(word, index) | ||
}) | ||
.join("") | ||
} | ||
|
||
/** | ||
* Convert a string to capital case (`Foo Bar`). | ||
*/ | ||
export function capitalCase(input: string) { | ||
return split(input).map(capitalCaseTransformFactory()).join(" ") | ||
} |
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
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,29 @@ | ||
import { camelCase, capitalCase } from "~/utils/case" | ||
|
||
describe("camelCase", () => { | ||
it.each` | ||
input | output | ||
${"media"} | ${"media"} | ||
${"mediaTitle"} | ${"mediaTitle"} | ||
${"Media Title"} | ${"mediaTitle"} | ||
${"media title"} | ${"mediaTitle"} | ||
${"media-title"} | ${"mediaTitle"} | ||
${"media_title"} | ${"mediaTitle"} | ||
`("returns $output for $input", ({ input, output }) => { | ||
expect(camelCase(input)).toBe(output) | ||
}) | ||
}) | ||
|
||
describe("capitalCase", () => { | ||
it.each` | ||
input | output | ||
${"media"} | ${"Media"} | ||
${"mediaTitle"} | ${"Media Title"} | ||
${"Media Title"} | ${"Media Title"} | ||
${"media title"} | ${"Media Title"} | ||
${"media-title"} | ${"Media Title"} | ||
${"media_title"} | ${"Media Title"} | ||
`("returns $output for $input", ({ input, output }) => { | ||
expect(capitalCase(input)).toBe(output) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.