Skip to content

Commit

Permalink
fix: ensure we have stdin data before processing it
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayeu committed Sep 29, 2018
1 parent 1961664 commit e7227d2
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions slugify
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

VERSION=1.0.1
VERSION=1.0.2

# This is the magic
to_slug() {
# Forcing the c local so alnum is only 0-9A-Za-z
export LANG=c
export LC_ALL=c
export LANG=POSIX
export LC_ALL=POSIX
# Keep only alphanumeric value
sed -e 's/[^[:alnum:]]/-/g' |
# Keep only one dash if there is multiple one consecutively
Expand All @@ -35,12 +35,16 @@ to_slug() {
sed -e 's/-$//'
}

# Test if we are called via a pipe
# Consume stdin if it exist
if test -p /dev/stdin; then
read -r input
fi

# Now check if there was input in stdin
if test -n "${input}"; then
echo "${input}" | to_slug
exit
# Test if there is an argument
# No stdin, let's check if there is an argument
elif test -n "${1}"; then
echo "${1}" | to_slug
exit
Expand Down

0 comments on commit e7227d2

Please sign in to comment.