jcubic/tagger

Silent error when first argument is jQuery object

jcubic opened this issue · 4 comments

If you pass jQuery object as first argument component is created but the return object is not tagger object. Which in turn throws different error when you try to invoke a method.

The issue is that tagger thinks that jQuery object is an array and doesn't return tagger object but an array.

        if (input.length) { 
             return Array.from(input).map(function(input) { 
                 return new tagger(input, options); 
             }); 
         }

This also occurs if you initialise using document.querySelectorAll which will return a NodeList even if only a single element exists.

Adding this to the top of the function tagger(input, options) will flatten the array if it contains only one element, works for both $() and document.querySelectorAll()

if (input.length && input.length === 1) {
    input = Array.from(input).pop();
}

@lucasnetau do you want to create a PR with this fix?

@jcubic sure I'll make one. I'll also add some documentation around if a single instance is created it will always be returned as an object even if it was provided in a single element array.

Single Instance initialised => tagger() returns object
Multiple instances initialised => tagger() returns object[]