Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

value.trim() in not function at types.js:40 #469

Closed
imanjra opened this issue Jul 21, 2020 · 6 comments
Closed

value.trim() in not function at types.js:40 #469

imanjra opened this issue Jul 21, 2020 · 6 comments
Labels

Comments

@imanjra
Copy link

imanjra commented Jul 21, 2020

Describe the bug

A clear and concise description of what the bug is.

Non-number/string value is allowed through helper/get.js causes runtime error value.trim() is not a function at

return !value.trim().length

Error:

node_modules/fuse.js/dist/fuse.common.js:242
  return !value.trim().length;
                ^

TypeError: value.trim is not a function

Version

The Fuse.js version where this bug is happening.

6.3.0 and above

Is this a regression?

Yes, the previous version in which this bug was not present was: ...

Yes, version below 6.3.0
Bug seem to have been introduced by 938db2a

🔬Minimal Reproduction

const Fuse = require('fuse.js')

const data = [{first: false}]
const options = {keys: [{name: 'first'}]}
const fuse = new Fuse(data, options)

fuse.search('a')

Additional context

else if below is always true

} else if (path.length) {

since line below returns an array with at least one item

return isArray(key) ? key : key.split('.')

Which leads into another deepGet with obj = false which leads to if block below and pushing non-string/number value into list

// If there's no path left, we've arrived at the object we care about.

@imanjra imanjra added the bug label Jul 21, 2020
@krisk krisk closed this as completed in 226d868 Jul 26, 2020
@Techn1x
Copy link

Techn1x commented Sep 3, 2020

Hi @krisk , I just upgraded to 6.4.1 and seem to encounter this error.

Screenshot from 2020-09-03 12-31-49

Screenshot from 2020-09-03 12-32-11

My items look like;

[
  { value: 'placement lesson' },
  {
    value: {
      grade: 1,
      lessonNumber: "1",
      name: "Lesson 1",
    },
  },
]

My options look like;

{
  threshold: 0.3,
  ignoreLocation: true,
  keys: ['value', 'value.name', 'value.lessonNumber'],
}

value is included in the keys list because sometimes in our data, value is a string, other times it's an object.

Downgrading to < 6.3.0 does fix the issue for now. Is my usage a supported use-case or do I need to refactor my code?

@joaobibiano
Copy link

I'm having this problem when the lib try read a instance of Date... someone knows how to fix?

@joaobibiano
Copy link

We can probably get a treatment. When receiving an object, do a JSON.stringify ()

@rendall
Copy link

rendall commented Jun 17, 2021

This (jest) test throws the error:

import Fuse from 'fuse.js';
import { Books } from './fixtures/Books';

describe('Fuse.js functions as expected', () => {
  const fuse = new Fuse(Books, {
    includeScore: true,
    keys: ['author', 'tags']
  });
  it('finds the correct assets', () => {
    const results = fuse.search('tion');
    console.log({ results });
  });
})

where Books is

export const Books = [
  {
    title: "Old Man's War",
    author: 'John Scalzi',
    tags: ['fiction']
  },
  {
    title: 'The Lock Artist',
    author: 'Steve',
    tags: ['thriller']
  }
];

The full stack trace is:

[FAIL] src/utils/fuse.spec.ts
  ● Test suite failed to run

    TypeError: value.trim is not a function

      3 |
      4 | describe('Fuse.js functions as expected', () => {
    > 5 |   const fuse = new Fuse(Books, {
        |                ^
      6 |     includeScore: true,
      7 |     keys: ['author', 'tags']
      8 |   });

      at isBlank (node_modules/fuse.js/dist/fuse.common.js:249:17)
      at node_modules/fuse.js/dist/fuse.common.js:648:21
          at Array.forEach (<anonymous>)
      at FuseIndex._addObject (node_modules/fuse.js/dist/fuse.common.js:604:17)
      at node_modules/fuse.js/dist/fuse.common.js:542:18
          at Array.forEach (<anonymous>)
      at FuseIndex.create (node_modules/fuse.js/dist/fuse.common.js:541:19)
      at createIndex (node_modules/fuse.js/dist/fuse.common.js:680:11)
      at Fuse.setCollection (node_modules/fuse.js/dist/fuse.common.js:1909:32)
      at new Fuse (node_modules/fuse.js/dist/fuse.common.js:1897:10)
      at packages/project/src/utils/fuse.spec.ts:5:16
      at Object.<anonymous> (packages/project/src/utils/fuse.spec.ts:4:1)
          at runMicrotasks (<anonymous>)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:387:19)

@Ne-Ne
Copy link

Ne-Ne commented Oct 27, 2021

I am getting the same error using a custom react app with the ESM build.

However no issues using CodeSandbox with the exact same code.

@apptimise-sam
Copy link

apptimise-sam commented Oct 27, 2021

We are also seeing this same issue, but with Date objects.

Currently we are reverting back to 6.2.1 to avoid the issue.

It looks like this commit fixed an issue with booleans but Dates weren't covered by the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants