nmccready / angular-simple-logger

Basic logger with level logging which can also be independent.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@$log is invalid thrown if Array is polyfilled

Lavan opened this issue · comments

I have noticed that if the Array object is polyfilled with a few extra methods (the ones I use are contains, find and equals) then angular-simple-logger breaks when checking if the $log object is a valid logging object.

This is because the for-in loop that you are using will find those polyfilled methods in addition to the actual array values. In normal JavaScript I'd either use hasOwnProperty() or even simpler and faster, use the numeric index.

I really don't do CoffeeScript so I'll leave the fix to you. But maybe something like this?

_isValidLogObject = (logObject) ->
  if !logObject
    return false
  idx = 0
  while idx < _fns.length
    val = _fns[idx]
    if logObject[val] == null or typeof logObject[val] != 'function'
      return false
    ++idx
  true

As you see I switched the for loop to a while (since CoffeeScript for loops are weird) and removed the isValid variable and replaced it with instant return statements instead.

The second part is optional of course, I just find it cleaner. 😄

I think I am going to leave the for in, but I will add Object.hasOwnProperty check.

Array.forEach would work well as well and the cleanest, but not the fastest.

If possible you could use underscore/lodash instead of Array.forEach. You could have an onload test for loop alternatives.

No as this library is light weight and I do not want to impose lodash on other libs. Yes ui-gmap uses lodash but angular-leaflet-directive does not.

Again it would be really helpful if you wrote some specs and PR it to this library to break the current functionality. It would save me and you a lot of time.

Whats wrong with using ESMA5 Array.forEach , do you really need browser support less than IE 9?

Lastly i recommend not extending the Array class. But i am working on some fixes anyway.

Nothing wrong with Array.forEach, you just mentioned that the performance might not be the best. I know that lodash's implementation is better than Array's (at least in older browsers, might have changed now) which is why I mentioned it. Underscore is just a wrapper around Array in this instance I believe.

In this instance it's a one off forEach call on an array of 5 elements so performance is not really an issue. As long as it doesn't pick up polyfilled methods I'm happy. 😄 My current solution was to remove all polyfills as I currently have no need of them.

yeah that is true on perf, but like u said the array size is very small

See commit fixed

Will cut new release asap.