mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-11 20:25:32 +03:00
lib.strings: init splitStringBy (#385643)
This commit is contained in:
commit
a9320986b3
3 changed files with 187 additions and 0 deletions
|
@ -631,6 +631,101 @@ runTests {
|
|||
];
|
||||
};
|
||||
|
||||
testSplitStringBySimpleDelimiter = {
|
||||
expr = strings.splitStringBy (
|
||||
prev: curr:
|
||||
builtins.elem curr [
|
||||
"."
|
||||
"-"
|
||||
]
|
||||
) false "foo.bar-baz";
|
||||
expected = [
|
||||
"foo"
|
||||
"bar"
|
||||
"baz"
|
||||
];
|
||||
};
|
||||
|
||||
testSplitStringByLeadingDelimiter = {
|
||||
expr = strings.splitStringBy (prev: curr: builtins.elem curr [ "." ]) false ".foo.bar.baz";
|
||||
expected = [
|
||||
""
|
||||
"foo"
|
||||
"bar"
|
||||
"baz"
|
||||
];
|
||||
};
|
||||
|
||||
testSplitStringByTrailingDelimiter = {
|
||||
expr = strings.splitStringBy (prev: curr: builtins.elem curr [ "." ]) false "foo.bar.baz.";
|
||||
expected = [
|
||||
"foo"
|
||||
"bar"
|
||||
"baz"
|
||||
""
|
||||
];
|
||||
};
|
||||
|
||||
testSplitStringByMultipleConsecutiveDelimiters = {
|
||||
expr = strings.splitStringBy (prev: curr: builtins.elem curr [ "." ]) false "foo...bar";
|
||||
expected = [
|
||||
"foo"
|
||||
""
|
||||
""
|
||||
"bar"
|
||||
];
|
||||
};
|
||||
|
||||
testSplitStringByKeepingSplitChar = {
|
||||
expr = strings.splitStringBy (prev: curr: builtins.elem curr [ "." ]) true "foo.bar.baz";
|
||||
expected = [
|
||||
"foo"
|
||||
".bar"
|
||||
".baz"
|
||||
];
|
||||
};
|
||||
|
||||
testSplitStringByCaseTransition = {
|
||||
expr = strings.splitStringBy (
|
||||
prev: curr: builtins.match "[a-z]" prev != null && builtins.match "[A-Z]" curr != null
|
||||
) true "fooBarBaz";
|
||||
expected = [
|
||||
"foo"
|
||||
"Bar"
|
||||
"Baz"
|
||||
];
|
||||
};
|
||||
|
||||
testSplitStringByEmptyString = {
|
||||
expr = strings.splitStringBy (prev: curr: builtins.elem curr [ "." ]) false "";
|
||||
expected = [ "" ];
|
||||
};
|
||||
|
||||
testSplitStringByComplexPredicate = {
|
||||
expr = strings.splitStringBy (
|
||||
prev: curr:
|
||||
prev != ""
|
||||
&& curr != ""
|
||||
&& builtins.match "[0-9]" prev != null
|
||||
&& builtins.match "[a-z]" curr != null
|
||||
) true "123abc456def";
|
||||
expected = [
|
||||
"123"
|
||||
"abc456"
|
||||
"def"
|
||||
];
|
||||
};
|
||||
|
||||
testSplitStringByUpperCaseStart = {
|
||||
expr = strings.splitStringBy (prev: curr: builtins.match "[A-Z]" curr != null) true "FooBarBaz";
|
||||
expected = [
|
||||
""
|
||||
"Foo"
|
||||
"Bar"
|
||||
"Baz"
|
||||
];
|
||||
};
|
||||
|
||||
testEscapeShellArg = {
|
||||
expr = strings.escapeShellArg "esc'ape\nme";
|
||||
expected = "'esc'\\''ape\nme'";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue