Skip to content

Commit 0bfede2

Browse files
Reorder random char helpers
1 parent e14d0d8 commit 0bfede2

File tree

1 file changed

+165
-165
lines changed

1 file changed

+165
-165
lines changed

posix-shell-script-kit

Lines changed: 165 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,171 @@ rm_all() {
855855
find "${1:-.}" -type f -exec rm {} \;
856856
}
857857

858+
##
859+
# Text helpers
860+
##
861+
862+
# trim: remove any space characters at the text's start or finish.
863+
#
864+
# Example:
865+
# ```
866+
# trim " foo "
867+
# => foo
868+
#```
869+
trim() {
870+
printf %s\\n "$*" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//'
871+
}
872+
873+
# slug: convert a string from any characters to solely lowercase and single internal dash characters.
874+
#
875+
# Example:
876+
# ```
877+
# slug "**Foo** **Goo** **Hoo**"
878+
# => foo-goo-hoo
879+
#```
880+
slug() {
881+
printf %s\\n "$*" | sed 's/[^[:alnum:]]/-/g; s/--*/-/g; s/^-*//; s/-*$//;' | tr '[[:upper:]]' '[[:lower:]]'
882+
}
883+
884+
# slugs: convert a string from any characters to solely lowercase and single internal dash characters and slash characters.
885+
#
886+
# Example:
887+
# ```
888+
# slugs "**Foo** / **Goo** / **Hoo**"
889+
# => foo/goo/hoo
890+
#```
891+
slugs(){
892+
printf %s\\n "$*" | sed 's/[^[:alnum:]\/]/-/g; s/--*/-/g; s/^-*//; s/-*$//; s/-*\/-*/\//g' | tr '[[:upper:]]' '[[:lower:]]'
893+
}
894+
895+
# upper_format: convert text from any lowercase letters to uppercase letters.
896+
#
897+
# Example:
898+
# ```
899+
# upper_format AbCdEf
900+
# => ABCDEF
901+
#```
902+
upper_format() {
903+
printf %s\\n "$*" | tr '[[:lower:]]' '[[:upper:]]'
904+
}
905+
906+
# lower_format: convert text from any uppercase letters to lowercase letters.
907+
#
908+
# Example:
909+
# ```
910+
# lower_format AbCdEf
911+
# => abcdef
912+
#```
913+
lower_format() {
914+
printf %s\\n "$*" | tr '[[:upper:]]' '[[:lower:]]'
915+
}
916+
917+
# chain_format: convert a string from any characters to solely alphanumeric and single internal dash characters.
918+
#
919+
# Example:
920+
# ```
921+
# chain_format "**Foo** **Goo** **Hoo**"
922+
# => Foo-Goo-Hoo
923+
#```
924+
chain_format() {
925+
printf %s\\n "$*" | sed 's/[^[:alnum:]]\{1,\}/-/g; s/-\{2,\}/-/g; s/^-\{1,\}//; s/-\{1,\}$//;'
926+
}
927+
928+
# snake_format: convert a string from any characters to solely alphanumeric and single internal underscore characters.
929+
#
930+
# Example:
931+
# ```
932+
# snake_format "**Foo** **Goo** **Hoo**"
933+
# => Foo_Goo_Hoo
934+
#```
935+
snake_format() {
936+
printf %s\\n "$*" | sed 's/[^[:alnum:]]\{1,\}/_/g; s/_\{2,\}/_/g; s/^_\{1,\}//; s/_\{1,\}$//;'
937+
}
938+
939+
# space_format: convert a string from any characters to solely alphanumeric and single internal space characters.
940+
#
941+
# Example:
942+
# ```
943+
# space_format "**Foo** **Goo** **Hoo**"
944+
# => Foo Goo Hoo
945+
#```
946+
space_format() {
947+
printf %s\\n "$*" | sed 's/[^[:alnum:]]\{1,\}/ /g; s/ \{2,\}/ /g; s/^ \{1,\}//; s/ \{1,\}$//;'
948+
}
949+
950+
# touch_format: convert a string from any characters to solely a command "touch -t" timestamp format.
951+
#
952+
# Example:
953+
# ```
954+
# touch_format "Foo 2021-05-04 22:57:54 Goo"
955+
# => 202105042257.54
956+
#```
957+
touch_format() {
958+
printf %s\\n "$*" | sed 's/[^[:digit:]]//g; s/^\([[:digit:]]\{12\}\)\([[:digit:]]\{2\}\)/\1.\2/;'
959+
}
960+
961+
# select_character_class: get a string's characters that match a class, with optional offset and length.
962+
#
963+
# Syntax:
964+
# ```
965+
# select_character_class <string> <class> [offset [length]]
966+
# ```
967+
#
968+
# Example with character class:
969+
# ```
970+
# select_character_class foo123goo456 alpha
971+
# => foogoo
972+
# ```
973+
#
974+
# Example with character class and substring offset:
975+
# ```
976+
# select_character_class foo123goo456 alpha 3
977+
# => goo
978+
# ```
979+
#
980+
# Example with character class and substring offset and length:
981+
# ```
982+
# select_character_class foo123goo456 alpha 3 1
983+
# => g
984+
# ```
985+
select_character_class() {
986+
string=${1//[^[:$2:]]/}
987+
offset=${3:-0}
988+
length=${4:-${#string}}
989+
printf %s\\n ${string:$offset:$length}
990+
}
991+
992+
# reject_character_class: get a string's characters that don't match a class, with optional offset and length.
993+
#
994+
# Syntax:
995+
# ```
996+
# reject_character_class <string> <class> [offset [length]]
997+
# ```
998+
#
999+
# Example with character class:
1000+
# ```
1001+
# reject_character_class foo123goo456 alpha
1002+
# => -123--456
1003+
# ```
1004+
#
1005+
# Example with character class and substring offset:
1006+
# ```
1007+
# reject_character_class foo123goo456 alpha 6
1008+
# => 456
1009+
# ```
1010+
#
1011+
# Example with character class and substring offset and length:
1012+
# ```
1013+
# reject_character_class foo123goo456 alpha 6 1
1014+
# => 4
1015+
# ```
1016+
reject_character_class() {
1017+
string=${1//[[:$2:]]/}
1018+
offset=${3:-0}
1019+
length=${4:-${#string}}
1020+
printf %s\\n ${string:$offset:$length}
1021+
}
1022+
8581023
##
8591024
# Random character helpers
8601025
##
@@ -1116,171 +1281,6 @@ random_char_xdigit() {
11161281
random_char '[:xdigit:]' "$@"
11171282
}
11181283

1119-
##
1120-
# Text helpers
1121-
##
1122-
1123-
# trim: remove any space characters at the text's start or finish.
1124-
#
1125-
# Example:
1126-
# ```
1127-
# trim " foo "
1128-
# => foo
1129-
#```
1130-
trim() {
1131-
printf %s\\n "$*" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//'
1132-
}
1133-
1134-
# slug: convert a string from any characters to solely lowercase and single internal dash characters.
1135-
#
1136-
# Example:
1137-
# ```
1138-
# slug "**Foo** **Goo** **Hoo**"
1139-
# => foo-goo-hoo
1140-
#```
1141-
slug() {
1142-
printf %s\\n "$*" | sed 's/[^[:alnum:]]/-/g; s/--*/-/g; s/^-*//; s/-*$//;' | tr '[[:upper:]]' '[[:lower:]]'
1143-
}
1144-
1145-
# slugs: convert a string from any characters to solely lowercase and single internal dash characters and slash characters.
1146-
#
1147-
# Example:
1148-
# ```
1149-
# slugs "**Foo** / **Goo** / **Hoo**"
1150-
# => foo/goo/hoo
1151-
#```
1152-
slugs(){
1153-
printf %s\\n "$*" | sed 's/[^[:alnum:]\/]/-/g; s/--*/-/g; s/^-*//; s/-*$//; s/-*\/-*/\//g' | tr '[[:upper:]]' '[[:lower:]]'
1154-
}
1155-
1156-
# upper_format: convert text from any lowercase letters to uppercase letters.
1157-
#
1158-
# Example:
1159-
# ```
1160-
# upper_format AbCdEf
1161-
# => ABCDEF
1162-
#```
1163-
upper_format() {
1164-
printf %s\\n "$*" | tr '[[:lower:]]' '[[:upper:]]'
1165-
}
1166-
1167-
# lower_format: convert text from any uppercase letters to lowercase letters.
1168-
#
1169-
# Example:
1170-
# ```
1171-
# lower_format AbCdEf
1172-
# => abcdef
1173-
#```
1174-
lower_format() {
1175-
printf %s\\n "$*" | tr '[[:upper:]]' '[[:lower:]]'
1176-
}
1177-
1178-
# chain_format: convert a string from any characters to solely alphanumeric and single internal dash characters.
1179-
#
1180-
# Example:
1181-
# ```
1182-
# chain_format "**Foo** **Goo** **Hoo**"
1183-
# => Foo-Goo-Hoo
1184-
#```
1185-
chain_format() {
1186-
printf %s\\n "$*" | sed 's/[^[:alnum:]]\{1,\}/-/g; s/-\{2,\}/-/g; s/^-\{1,\}//; s/-\{1,\}$//;'
1187-
}
1188-
1189-
# snake_format: convert a string from any characters to solely alphanumeric and single internal underscore characters.
1190-
#
1191-
# Example:
1192-
# ```
1193-
# snake_format "**Foo** **Goo** **Hoo**"
1194-
# => Foo_Goo_Hoo
1195-
#```
1196-
snake_format() {
1197-
printf %s\\n "$*" | sed 's/[^[:alnum:]]\{1,\}/_/g; s/_\{2,\}/_/g; s/^_\{1,\}//; s/_\{1,\}$//;'
1198-
}
1199-
1200-
# space_format: convert a string from any characters to solely alphanumeric and single internal space characters.
1201-
#
1202-
# Example:
1203-
# ```
1204-
# space_format "**Foo** **Goo** **Hoo**"
1205-
# => Foo Goo Hoo
1206-
#```
1207-
space_format() {
1208-
printf %s\\n "$*" | sed 's/[^[:alnum:]]\{1,\}/ /g; s/ \{2,\}/ /g; s/^ \{1,\}//; s/ \{1,\}$//;'
1209-
}
1210-
1211-
# touch_format: convert a string from any characters to solely a command "touch -t" timestamp format.
1212-
#
1213-
# Example:
1214-
# ```
1215-
# touch_format "Foo 2021-05-04 22:57:54 Goo"
1216-
# => 202105042257.54
1217-
#```
1218-
touch_format() {
1219-
printf %s\\n "$*" | sed 's/[^[:digit:]]//g; s/^\([[:digit:]]\{12\}\)\([[:digit:]]\{2\}\)/\1.\2/;'
1220-
}
1221-
1222-
# select_character_class: get a string's characters that match a class, with optional offset and length.
1223-
#
1224-
# Syntax:
1225-
# ```
1226-
# select_character_class <string> <class> [offset [length]]
1227-
# ```
1228-
#
1229-
# Example with character class:
1230-
# ```
1231-
# select_character_class foo123goo456 alpha
1232-
# => foogoo
1233-
# ```
1234-
#
1235-
# Example with character class and substring offset:
1236-
# ```
1237-
# select_character_class foo123goo456 alpha 3
1238-
# => goo
1239-
# ```
1240-
#
1241-
# Example with character class and substring offset and length:
1242-
# ```
1243-
# select_character_class foo123goo456 alpha 3 1
1244-
# => g
1245-
# ```
1246-
select_character_class() {
1247-
string=${1//[^[:$2:]]/}
1248-
offset=${3:-0}
1249-
length=${4:-${#string}}
1250-
printf %s\\n ${string:$offset:$length}
1251-
}
1252-
1253-
# reject_character_class: get a string's characters that don't match a class, with optional offset and length.
1254-
#
1255-
# Syntax:
1256-
# ```
1257-
# reject_character_class <string> <class> [offset [length]]
1258-
# ```
1259-
#
1260-
# Example with character class:
1261-
# ```
1262-
# reject_character_class foo123goo456 alpha
1263-
# => -123--456
1264-
# ```
1265-
#
1266-
# Example with character class and substring offset:
1267-
# ```
1268-
# reject_character_class foo123goo456 alpha 6
1269-
# => 456
1270-
# ```
1271-
#
1272-
# Example with character class and substring offset and length:
1273-
# ```
1274-
# reject_character_class foo123goo456 alpha 6 1
1275-
# => 4
1276-
# ```
1277-
reject_character_class() {
1278-
string=${1//[[:$2:]]/}
1279-
offset=${3:-0}
1280-
length=${4:-${#string}}
1281-
printf %s\\n ${string:$offset:$length}
1282-
}
1283-
12841284
##
12851285
# Array helpers
12861286
##

0 commit comments

Comments
 (0)