olivernn / lunr.js

A bit like Solr, but much smaller and not as bright
http://lunrjs.com
MIT License
8.96k stars 548 forks source link

TypeError: Cannot call method 'replace' of null #27

Closed gambhiro closed 11 years ago

gambhiro commented 11 years ago

I got TypeError: Cannot call method 'replace' of null for line 74 in lunr.js.

Just inserting a check for null argument fixes it:

lunr.tokenizer = function (str) {
  if (str == null) return new Array // check for null argument
  if (Array.isArray(str)) return str

  var str = str.replace(/^\s+/, '')
  ...
olivernn commented 11 years ago

Good catch and thanks for reporting this bug! I'd be happy to accept a pull request with a fix for this issue.

You don't need to change the 'built' version of the library, just the individual files, in this case just lib/tokenizer.js.

Also, include a simple test to prevent any regressions of this bug in the future, you should add your test to test/tokenizer_test.js.

Lastly, it might be a little more idiomatic JavaScript to change your modification to be like the following:

if (!str) return []

Let me know if you don't feel comfortable with contributing a patch and I'll put together a fix.

gambhiro commented 11 years ago

Interesting excercise for a one-line fix :) Thanks for the mini-tutorial too.

olivernn commented 11 years ago

I've pushed a new version, 0.3.2, which includes a fix for this issue.

gambhiro commented 11 years ago

Great, thanks!