commenthol / date-holidays-parser

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

Type checking before using substr #6

Closed mpanneck closed 4 years ago

mpanneck commented 5 years ago

I am using the plugin date-holidays@1.3.6 (which uses date-holidays-parser@1.3.1) within an Ionic 3 App. While using, the plugin throws the error o.str.substr is not a function and stops working. That happens because o.str can have an other datatypes than string. While debugging I've seen, that o.str is a function sometimes and not a string for some reason. So it's obviously not possible to call substr on a function. In this PR I've added a typecheck before using substr.

commenthol commented 5 years ago

Hi @mpanneck, I like to let you know that I won't accept this PR unless knowing the exact cause for this issue. The parser shall only parse strings, so the reported is a strange behavior which should not occur. Please let me know the country, state, region you have set up or provide the string used for parsing.

mpanneck commented 5 years ago

Country is DE. State and region are not set explicitly.

commenthol commented 5 years ago

This is very strange. This project has extensive tests. Even node test/sample.js DE in date-holidays does not show this type of error. Are you using code mangling in your project? If so could you turn that off and report the findings? Can you provide a stack trace? How do you require/import the lib "date-holidays" within your project? Could you provide a demo project with a basic setup which throws the error?

mpanneck commented 5 years ago

Here is the stack trace: ERROR Error: Uncaught (in promise): TypeError: o.str.substr is not a function TypeError: o.str.substr is not a function at Parser._shorten (vendor.js:112361) at Parser._ruleWeekday (vendor.js:112617) at Parser._tokenize (vendor.js:112348) at Parser.parse (vendor.js:112304) at PostRule._findEventInYear (vendor.js:186187) at PostRule.disable (vendor.js:186172) at PostRule.getEvent (vendor.js:186115) at DateFn.inYear (vendor.js:184876) at Holidays._dateByRule (vendor.js:181935) at vendor.js:181725 at c (polyfills.js:3) at Object.reject (polyfills.js:3) at NavControllerBase._fireError (vendor.js:61354) at NavControllerBase._failed (vendor.js:61347) at vendor.js:61394 at t.invoke (polyfills.js:3) at Object.onInvoke (vendor.js:4982) at t.invoke (polyfills.js:3) at r.run (polyfills.js:3) at polyfills.js:3

I am importing the lib like this (Typescript): import Holidays, { Holiday, HolidaysInterface } from 'date-holidays';

After some research I would imagine it is some kind of side effect with any of the other plugins used in this project. If I create a new project everything works fine with date-holidays.

commenthol commented 5 years ago

The parser only parses strings which are obtained by holidays.json. So I would image that accepting your PR will have other side-effects such as holidays not being calculated.