mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-09 09:36:20 +09:00
lib.strings: add trim
and trimWith
`strings.trim` returns a copy of the string with all leading and trailing whitespace removed. `strings.trimWith` does the same thing, but calling code can decide whether to trim the start and/or end of the string.
This commit is contained in:
parent
d1e70dde57
commit
aad87c2aa8
3 changed files with 129 additions and 1 deletions
|
@ -369,6 +369,72 @@ runTests {
|
|||
expected = "hellohellohellohellohello";
|
||||
};
|
||||
|
||||
# Test various strings are trimmed correctly
|
||||
testTrimString = {
|
||||
expr =
|
||||
let
|
||||
testValues = f: mapAttrs (_: f) {
|
||||
empty = "";
|
||||
cr = "\r";
|
||||
lf = "\n";
|
||||
tab = "\t";
|
||||
spaces = " ";
|
||||
leading = " Hello, world";
|
||||
trailing = "Hello, world ";
|
||||
mixed = " Hello, world ";
|
||||
mixed-tabs = " \t\tHello, world \t \t ";
|
||||
multiline = " Hello,\n world! ";
|
||||
multiline-crlf = " Hello,\r\n world! ";
|
||||
};
|
||||
in
|
||||
{
|
||||
leading = testValues (strings.trimWith { start = true; });
|
||||
trailing = testValues (strings.trimWith { end = true; });
|
||||
both = testValues strings.trim;
|
||||
};
|
||||
expected = {
|
||||
leading = {
|
||||
empty = "";
|
||||
cr = "";
|
||||
lf = "";
|
||||
tab = "";
|
||||
spaces = "";
|
||||
leading = "Hello, world";
|
||||
trailing = "Hello, world ";
|
||||
mixed = "Hello, world ";
|
||||
mixed-tabs = "Hello, world \t \t ";
|
||||
multiline = "Hello,\n world! ";
|
||||
multiline-crlf = "Hello,\r\n world! ";
|
||||
};
|
||||
trailing = {
|
||||
empty = "";
|
||||
cr = "";
|
||||
lf = "";
|
||||
tab = "";
|
||||
spaces = "";
|
||||
leading = " Hello, world";
|
||||
trailing = "Hello, world";
|
||||
mixed = " Hello, world";
|
||||
mixed-tabs = " \t\tHello, world";
|
||||
multiline = " Hello,\n world!";
|
||||
multiline-crlf = " Hello,\r\n world!";
|
||||
};
|
||||
both = {
|
||||
empty = "";
|
||||
cr = "";
|
||||
lf = "";
|
||||
tab = "";
|
||||
spaces = "";
|
||||
leading = "Hello, world";
|
||||
trailing = "Hello, world";
|
||||
mixed = "Hello, world";
|
||||
mixed-tabs = "Hello, world";
|
||||
multiline = "Hello,\n world!";
|
||||
multiline-crlf = "Hello,\r\n world!";
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
testSplitStringsSimple = {
|
||||
expr = strings.splitString "." "a.b.c.d";
|
||||
expected = [ "a" "b" "c" "d" ];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue