Properly deal with undefined string in shorten function