Skip to content

Commit

Permalink
Merge pull request #41 from louib/better_replace_function
Browse files Browse the repository at this point in the history
😹😹 Improve tokenization
  • Loading branch information
mrowa44 authored May 31, 2019
2 parents ee87348 + b23ebf6 commit f561dab
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 13 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ Note: if you want colors you need to specify the `--color` option.

To invoke help run `emojify -h`.

Testing
-------

The project can be tested using [bats](https://github.com/sstephenson/bats#installing-bats-from-source):
```bash
$ bats emojify_tests.bats
```

Related
-------
Expand Down
60 changes: 47 additions & 13 deletions emojify
Original file line number Diff line number Diff line change
Expand Up @@ -2791,21 +2791,55 @@ declare -A emojis=(
)

# Gets emoji from the associative array
# Returns raw emoji character or word
to_emoji () {
# Returns raw emoji character or original token
emojify_token () {
local value=${emojis[$1]}
[[ -n $value ]] && echo -e "$value " || echo "$1"
[[ -n $value ]] && echo -e "$value" || echo "$1"
}

# Function to parse a line, split it into an array of words and then emojify
# each word.
parse_line () {
IFS=' '; read -ra words_arr <<< "$*"
out=()
for word in "${words_arr[@]}"; do
out+=("$(to_emoji "$word")")
# Function to emojify a line.
emojify_line () {
line=$*
current_token=""
emojified_line=""
for (( i=0; i<${#line}; i++ )); do
current_char="${line:$i:1}"

if [[ -z "$current_token" ]]; then
# starting a new token.
if [[ $current_char =~ ^:$ ]]; then
current_token+=$current_char
# not starting a new token.
else
emojified_line+=$current_char
fi
else
# finishing the current token.
if [[ $current_char =~ ^:$ ]]; then
emojified_token=$(emojify_token "$current_token$current_char")

# nothing changed. we still need the ending ':' as it might
# serve for an upcoming emoji πŸ˜πŸ˜πŸŽ‰πŸŽ‰
if [[ "$emojified_token" == "$current_token$current_char" ]]; then
emojified_line+=$current_token
current_token=$current_char
else
emojified_line+=$emojified_token
current_token=""
fi
# continuing the current token.
elif [[ $current_char =~ ^[a-zA-Z0-9_+-]$ ]]; then
current_token+=$current_char
# dropping the current token.
else
current_token+=$current_char
emojified_line+=$current_token
current_token=""
fi
fi
done
echo "${out[*]}"
emojified_line+=$current_token
echo "$emojified_line"
}

# Function to print help info.
Expand Down Expand Up @@ -2850,12 +2884,12 @@ if [[ -n $1 ]]; then

* )
check_version
parse_line "$*"
emojify_line "$*"
;;
esac
else
check_version
while IFS=''; read -r line || [ -n "$line" ]; do
parse_line "$line"
emojify_line "$line"
done
fi
54 changes: 54 additions & 0 deletions emojify_tests.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bats

@test "handles an input without a single emoji 😿" {
result=$(./emojify "no emoji :(")
[ "$result" = "no emoji :(" ]
}

@test "handles an input with a single emoji 😹" {
result=$(./emojify "an emoji :grin:")
[ "$result" = "an emoji 😁" ]
}

@test "handles an input with a lot of emojis 😻" {
result=$(./emojify "emojis :grin::grin: :tada:yay:champagne:")
[ "$result" = "emojis 😁😁 πŸŽ‰yay🍾" ]
}

@test "handles emojis with underscores and numbers πŸ’―" {
result=$(./emojify "this is perfect :100: :1st_place_medal:")
[ "$result" = "this is perfect πŸ’― πŸ₯‡" ]
}

@test "handles emojis with + and - πŸ‘" {
result=$(./emojify "great :+1::+1::-1:")
[ "$result" = "great πŸ‘πŸ‘πŸ‘Ž" ]
}

@test "handles right-hand side emojis πŸ‘‰" {
result=$(./emojify ":not_an_emoji:point_right:")
[ "$result" = ":not_an_emojiπŸ‘‰" ]
result=$(./emojify "::::point_right:")
[ "$result" = ":::πŸ‘‰" ]
}

@test "handles punctuations just after aliases" {
result=$(./emojify "Enter the :airplane:!")
[ "$result" = "Enter the ✈️!" ]
}

@test "handles multiple spaces after an emoji" {
result=$(./emojify ":sparkles: Three spaces")
[ "$result" = "✨ Three spaces" ]
result=$(./emojify ":sparkles: Five spaces")
[ "$result" = "✨ Five spaces" ]
result=$(./emojify ":sparkles: One space")
[ "$result" = "✨ One space" ]
}

@test "handles the examples from the readme πŸ˜‰" {
result=$(./emojify "Hey, I just :raising_hand: you, and this is :scream: , but here's my :calling: , so :telephone_receiver: me, maybe?")
[ "$result" = "Hey, I just πŸ™‹ you, and this is 😱 , but here's my πŸ“² , so πŸ“ž me, maybe?" ]
result=$(./emojify "To :bee: , or not to :bee: : that is the question... To take :muscle: against a :ocean: of troubles, and by opposing, end them?")
[ "$result" = "To 🐝 , or not to 🐝 : that is the question... To take πŸ’ͺ against a 🌊 of troubles, and by opposing, end them?" ]
}

0 comments on commit f561dab

Please sign in to comment.