Skip to content

Commit

Permalink
Create string_replace_all function
Browse files Browse the repository at this point in the history
This function replace all chars to a defined string.
The `string_replace` function was changed to pass an empty value as
"replacement".
  • Loading branch information
henriquemoody committed Sep 25, 2013
1 parent 9933a4e commit 5e8f64b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][]
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/replace.bash
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ string_replace()

if [[ -z "${1}" ]]; then
return 1
elif [[ -z "${2}" ]]; then
return 2
fi

case "${type}" in
Expand All @@ -29,7 +27,7 @@ string_replace()
replace="${2}"
;;
*)
return 3
return 2
;;
esac

Expand Down
13 changes: 13 additions & 0 deletions src/replace/all.bash
Original file line number Diff line number Diff line change
@@ -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}"
}
10 changes: 2 additions & 8 deletions tests/replace.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
21 changes: 21 additions & 0 deletions tests/replace/all.bats
Original file line number Diff line number Diff line change
@@ -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}" ]]
}

0 comments on commit 5e8f64b

Please sign in to comment.