diff --git a/README.md b/README.md index 4d5c763..45cbaca 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ Strings handler written in Bash. * [string_repeat][] * [string_scape][] * [string_replace][] +* [string_replace_all][] * [string_dash_camelcase][] * [string_separator_camelcase][] * [string_underscore_camelcase][] @@ -106,6 +107,19 @@ Using REGEX: string_replace -t regex "[^a-z]" "" "That's my boy." # hatsmyboy ```` +### string_replace_all +Replaces all chars to a defined string + +Passing string as argument: +````bash +string_replace_all "*" "p4ssw0rd" # ******** +```` + +Using pipes +````bash +echo "p4ssw0rd" | string_replace_all "*" # ******** +```` + ### string_dash_camelcase Convert dash to camelcase. @@ -190,6 +204,7 @@ echo "thisIsAString" | string_camelcase_underscore # this_Is_A_String [string_repeat]: #string_repeat [string_scape]: #string_scape [string_replace]: #string_replace +[string_replace_all]: #string_replace_all [string_dash_camelcase]: #string_dash_camelcase [string_separator_camelcase]: #string_separator_camelcase [string_underscore_camelcase]: #string_underscore_camelcase diff --git a/src/replace.bash b/src/replace.bash index 26e984d..96483c0 100644 --- a/src/replace.bash +++ b/src/replace.bash @@ -15,8 +15,6 @@ string_replace() if [[ -z "${1}" ]]; then return 1 - elif [[ -z "${2}" ]]; then - return 2 fi case "${type}" in @@ -29,7 +27,7 @@ string_replace() replace="${2}" ;; *) - return 3 + return 2 ;; esac diff --git a/src/replace/all.bash b/src/replace/all.bash new file mode 100644 index 0000000..5a10d9b --- /dev/null +++ b/src/replace/all.bash @@ -0,0 +1,13 @@ +# @depends string_escape +# @depends string_replace +string_replace_all() +{ + local replacement="${1}" + local string="${2}" + + if [[ -z "${string}" ]] && [ ! -t 0 ]; then + string=$(cat /dev/stdin) + fi + + string_replace --type regex "." "$(echo "${replacement}" | string_escape --type regex)" "${string}" +} diff --git a/tests/replace.bats b/tests/replace.bats index 1f799ec..e34bc3e 100644 --- a/tests/replace.bats +++ b/tests/replace.bats @@ -8,16 +8,10 @@ load ../source [ "${status}" -eq 1 ] } -@test "Finish with 2 when passing no 'replace' argument" { - run string_replace "search" - - [ "${status}" -eq 2 ] -} - -@test "Finish with 3 when passing an invalid '--type' option value" { +@test "Finish with 2 when passing an invalid '--type' option value" { run string_replace -t something "search" "replace" "String to search." - [ "${status}" -eq 3 ] + [ "${status}" -eq 2 ] } @test "Replace a using plain text" { diff --git a/tests/replace/all.bats b/tests/replace/all.bats new file mode 100644 index 0000000..c6e27a5 --- /dev/null +++ b/tests/replace/all.bats @@ -0,0 +1,21 @@ +#!/usr/bin/env bats + +load ../../source + +@test "Replace all chars to defined argument" { + run string_replace_all '*' 'Henrique Moody' + + [[ "${output}" = '**************' ]] +} + +@test "Replace all chars to defined argument in a pipe" { + output=$(echo "Henrique Moody" | string_replace_all '*') + + [[ "${output}" = '**************' ]] +} + +@test "Replace all chars to defined argument in a pipe even if it if empty" { + output=$(echo "Henrique Moody" | string_replace_all) + + [[ -z "${output}" ]] +}