mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-09 19:13:26 +03:00
lib.strings: init toCamelCase
This commit is contained in:
parent
24249d45f7
commit
24e8a790b7
3 changed files with 80 additions and 0 deletions
|
@ -345,6 +345,7 @@ let
|
||||||
upperChars
|
upperChars
|
||||||
toLower
|
toLower
|
||||||
toUpper
|
toUpper
|
||||||
|
toCamelCase
|
||||||
toSentenceCase
|
toSentenceCase
|
||||||
addContextFrom
|
addContextFrom
|
||||||
splitString
|
splitString
|
||||||
|
|
|
@ -1500,6 +1500,63 @@ rec {
|
||||||
addContextFrom str (toUpper firstChar + toLower rest)
|
addContextFrom str (toUpper firstChar + toLower rest)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Converts a string to camelCase. Handles snake_case, PascalCase,
|
||||||
|
kebab-case strings as well as strings delimited by spaces.
|
||||||
|
|
||||||
|
# Inputs
|
||||||
|
|
||||||
|
`string`
|
||||||
|
: The string to convert to camelCase
|
||||||
|
|
||||||
|
# Type
|
||||||
|
|
||||||
|
```
|
||||||
|
toCamelCase :: string -> string
|
||||||
|
```
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
:::{.example}
|
||||||
|
## `lib.strings.toCamelCase` usage example
|
||||||
|
|
||||||
|
```nix
|
||||||
|
toCamelCase "hello-world"
|
||||||
|
=> "helloWorld"
|
||||||
|
toCamelCase "hello_world"
|
||||||
|
=> "helloWorld"
|
||||||
|
toCamelCase "hello world"
|
||||||
|
=> "helloWorld"
|
||||||
|
toCamelCase "HelloWorld"
|
||||||
|
=> "helloWorld"
|
||||||
|
```
|
||||||
|
|
||||||
|
:::
|
||||||
|
*/
|
||||||
|
toCamelCase =
|
||||||
|
str:
|
||||||
|
lib.throwIfNot (isString str) "toCamelCase does only accepts string values, but got ${typeOf str}" (
|
||||||
|
let
|
||||||
|
separators = splitStringBy (
|
||||||
|
prev: curr:
|
||||||
|
elem curr [
|
||||||
|
"-"
|
||||||
|
"_"
|
||||||
|
" "
|
||||||
|
]
|
||||||
|
) false str;
|
||||||
|
|
||||||
|
parts = lib.flatten (
|
||||||
|
map (splitStringBy (
|
||||||
|
prev: curr: match "[a-z]" prev != null && match "[A-Z]" curr != null
|
||||||
|
) true) separators
|
||||||
|
);
|
||||||
|
|
||||||
|
first = if length parts > 0 then toLower (head parts) else "";
|
||||||
|
rest = if length parts > 1 then map toSentenceCase (tail parts) else [ ];
|
||||||
|
in
|
||||||
|
concatStrings (map (addContextFrom str) ([ first ] ++ rest))
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Appends string context from string like object `src` to `target`.
|
Appends string context from string like object `src` to `target`.
|
||||||
|
|
||||||
|
|
|
@ -969,6 +969,28 @@ runTests {
|
||||||
|
|
||||||
testToSentenceCasePath = testingThrow (strings.toSentenceCase ./.);
|
testToSentenceCasePath = testingThrow (strings.toSentenceCase ./.);
|
||||||
|
|
||||||
|
testToCamelCase = {
|
||||||
|
expr = strings.toCamelCase "hello world";
|
||||||
|
expected = "helloWorld";
|
||||||
|
};
|
||||||
|
|
||||||
|
testToCamelCaseFromKebab = {
|
||||||
|
expr = strings.toCamelCase "hello-world";
|
||||||
|
expected = "helloWorld";
|
||||||
|
};
|
||||||
|
|
||||||
|
testToCamelCaseFromSnake = {
|
||||||
|
expr = strings.toCamelCase "hello_world";
|
||||||
|
expected = "helloWorld";
|
||||||
|
};
|
||||||
|
|
||||||
|
testToCamelCaseFromPascal = {
|
||||||
|
expr = strings.toCamelCase "HelloWorld";
|
||||||
|
expected = "helloWorld";
|
||||||
|
};
|
||||||
|
|
||||||
|
testToCamelCasePath = testingThrow (strings.toCamelCase ./.);
|
||||||
|
|
||||||
testToInt = testAllTrue [
|
testToInt = testAllTrue [
|
||||||
# Naive
|
# Naive
|
||||||
(123 == toInt "123")
|
(123 == toInt "123")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue