outaTiME / applause

Human-friendly replacements.

Home Page:https://outatime.github.io/applause.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BUG: TypeError: content.match is not a function

MrJithil opened this issue · comments

While using applause for replacements, we are getting below errors.

TypeError: content.match is not a function
    at d:\OpenSource\nodejs.org\node_modules\applause\src\applause.js:159:26
    at Array.forEach (<anonymous>:null:null)
    at Applause.replace (d:\OpenSource\nodejs.org\node_modules\applause\src\applause.js:106:12)
    at d:\OpenSource\nodejs.org\node_modules\metalsmith-replace\lib\replace.js:24:43
    at Array.forEach (<anonymous>:null:null)
    at d:\OpenSource\nodejs.org\node_modules\metalsmith-replace\lib\replace.js:16:34
    at Array.forEach (<anonymous>:null:null)
    at Ware.<anonymous> (d:\OpenSource\nodejs.org\node_modules\metalsmith-replace\lib\replace.js:14:28)
    at Ware.<anonymous> (d:\OpenSource\nodejs.org\node_modules\wrap-fn\index.js:45:19)
    at next (d:\OpenSource\nodejs.org\node_modules\ware\lib\index.js:85:20)
    at d:\OpenSource\nodejs.org\node_modules\wrap-fn\index.js:121:18
    at d:\OpenSource\nodejs.org\node_modules\metalsmith-layouts\lib\index.js:141:17
    at runMicrotasks (<anonymous>:null:null)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

This error is being shown while using the metalsmith-replace which uses applause internally.

Hi @MrJithil,
great one, but replace a method only accepts string as a parameter, what kind of data type are you passing? I actually handling falsy contents as well to prevent issues like you see in the following line:

https://github.com/outaTiME/applause/blob/master/src/applause.js#L96

@outaTiME There are chances to accept numeric endpoints like 404. So the datatype I'm passing here is number

I was building a repository where the metalsmith and replace being used. There I faced this issue. After the fix, everything worked well.

Also, in the mentioned line in above comment, will not replace the numbers with string. In case the variable contents has some number or boolean values, it may try to analyse and replace the contents.

I understand, but I think the best thing to do would be to filter the content that is not a string to avoid the call to replace, it does not make sense to call it with some type of data that is not a string, it is probably an error of metalsmith rather than of applause.

I think the best thing would be to applause throw an error if it receives any data that is not of the string type, in your PR you detect the data type and return a valid replacement result, and probably this should not.

Also from what I see metalsmith-replace does the replacements on the content of the files that are string therefore it is making use of applause correctly. Could you share some of your code with me to see if we can make any adjustments there?

@outaTiME, we have workarounds in pitch already.
metalsmith-replace is not only sending strings. We can not guaranty that all the titles are string datatypes. If a title only contains non-string data type values like 404.md, true.md, JavaScript type inference considers it to the number and boolean datatype. So, we cannot state that clients of applause should only send a string to applause.

You may replicate the same by creating a metalsmith repo with metalsmith-replace integration. And some files like above in it.

Trying to use the match function without type-check or typecasting is should definitely fix from our end in my perspective.

ok i understand, but i still think that metalsmith-replace should convert the datatype it receives to a string

however I am going to accept your workaround PR like this and add some test cases,

thanks for your contribution !!!

Thanks, @outaTiME , I will work on that repo and identify that. I will definitely add you there for a review.