Make words of an uppercase array - jQuery

Asked

Viewed 135 times

0

Hello,

With code below, I make the first letter of any word uppercase, except the words that are in the array wordsToIgnoreand wordContainAt.

Now I thought to update the code and include an array called wordUpperCase, where the words that are in this array will be completely uppercase.

Example: let wordUppercase = ['LTDA', 'S.A']

I tried to use the following if, but to no avail.

if (wordUpperCase.indexOf($.trim(word)) != -1) {
    words[i] = words[i].toUpperCase();
}

Below is the code along with an example

$(window).on('load', function() {
    $.fn.capitalize = function() {
        // words to ignore
        let wordContainAt = '@',
            wordsToIgnore = ['to', 'and', 'the', 'it', 'or', 'that', 'this', 'dos', 'rua', 'das', 'rh'],
            minLength = 2;

        function getWords(str) {
            if (str == undefined) {
                str = 'abc def';
            } else {
                str = str;
            }
            return str.match(/\S+\s*/g);
        }
        this.each(function() {
            let words = getWords(this.value);
            $.each(words, function(i, word) {
                // only continues if the word is not in the ignore list or contains at '@'
                if (word.indexOf(wordContainAt) != -1) {
                    words[i] = words[i].toLowerCase();
                } else if (wordsToIgnore.indexOf($.trim(word)) == -1 && $.trim(word).length > minLength) {
                    words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1).toLowerCase();
                } else {
                    words[i] = words[i].toLowerCase();
                }
            });
            if (this.value != '') {
                this.value = words.join('');
            }
        });
    };

    // field onblur with class .title
    $(document).on('blur', '.lower', function() {
        $(this).capitalize();
    }).capitalize();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="lower">

  • Have this old post on SOPT talking about it who knows doesn’t help? https://answall.com/questions/224603/como-deixara-primeira-letra-mai%C3%Bascula

2 answers

1


Wagner, as your wordUpperCase array contains only uppercase letters, you need to perform the search by performing toUpperCase:

if (wordUpperCase.indexOf($.trim(word).toUpperCase()) != -1) {
    words[i] = words[i].toUpperCase();
}
  • I did the test with the complete code, as follows: https://pastebin.com/CTWugkZ

  • Daniel, it is not a rule, that words of the array wordUpperCase are capitalized, I didn’t want to have to worry about that.

  • If you’re not going to use some regex, I think the simplest way would still be to leave everything on the top and search on the top. Otherwise, you will need to perform the regex search of these words, ignoring their case.

0

Simplifying your problem, you just need to check if one value is contained in another set of values, correct?

I believe that the code below, in pure Javascript, will meet.

let words = ['foo', 'bar']

words = words.map(word => word.toUpperCase())

console.log(words)

let phrase = 'Lorem ipsum FOO'

phrase.split(' ').forEach(word => {
  console.log(`O conjunto contém a palavra ${word}: ${words.indexOf(word) !== -1}`)
})

Output result:

[ 'FOO', 'BAR' ]
O conjunto contém a palavra Lorem: false
O conjunto contém a palavra ipsum: false
O conjunto contém a palavra FOO: true

Browser other questions tagged

You are not signed in. Login or sign up in order to post.