commenthol / date-holidays-parser

parser for worldwide holidays
ISC License
46 stars 23 forks source link

o.str.substr is not a function #18

Closed Twisterking closed 3 years ago

Twisterking commented 3 years ago

Error:

Exception while invoking method 'getHolidays' TypeError: o.str.substr is not a function
     at Parser._shorten (/app/node_modules/date-holidays-parser/lib/Parser.js:192:21)

with the country 'AT':

const Holidays = require('date-holidays');
let holidays = new Holidays();
holidays.init('AT');
holidays.getHolidays(2020); // ERROR

After some debugging:

    key: "_shorten",
    value: function _shorten(o, cap0) {
      console.log({ o, cap0 });
      o.str = o.str.substr(cap0.length, o.str.length);
    }

it seems like that o and cap0 are both functions!!

Please fix, this is very annoying as this completely breaks the whole library for me 😢

leonidasv commented 3 years ago

After some digging (tks @berreis), I've discovered that the problem lies in PostRule.js:89:

      for (const i in arr) {
        const p = parser.parse(arr[i]);
        ...

for ... in in Arrays is susceptible to Prototype overrides. So, if somewhere in your code you have something like:

Array.prototype.something = function() { ...

The loop above will run into:

      for (const i = 'something' in arr) {
        const p = parser.parse(arr['something']);

And, since arr['something'] is a function, the code will proceed and that function will get into o.str and o.rule. And, since functions have no substr, _shorten() will break.

This can also happen if your prototype override is a number, a boolean or anything else that's not a string.

I will submit a PR changing the for in approach to a for of, which is the recommend approach for Arrays in JS.