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

Allow to use functions instead of strings for keys specification #99

Closed
Phyks opened this issue Aug 8, 2016 · 7 comments
Closed

Allow to use functions instead of strings for keys specification #99

Phyks opened this issue Aug 8, 2016 · 7 comments

Comments

@Phyks
Copy link

Phyks commented Aug 8, 2016

Hi,

When using Immutable.JS, one has to convert back and forth between Immutable objects and JavaScript objects, to pass them to Fuse.

If we could use functions to specify which key to use, it would allow the use of non-array objects such as Immutable ones:

var books = Immutable.fromJS([{
  title: "Old Man's War"
  author: {
    firstName: "John",
    lastName: "Scalzi"
  }
}]);

var fuse = new Fuse(books, { keys: [item -> item.title, item -> item.getIn(["author", "firstName"])] });

Thanks

@krisk krisk added the feature label Aug 9, 2016
@krisk
Copy link
Owner

krisk commented Aug 10, 2016

@Phyks, in your example you're passing in books into the Fuse instance, however, the first argument is required to be an array, and not an object. This is because it needs to be properly iterated on. I'm all for being able to use functions as key accessors, but the list must remain an array.

@Phyks
Copy link
Author

Phyks commented Aug 10, 2016

Hmm, books should be an Immutable List of Immutable Map. Immutable List is indeed an object, but is the Immutable equivalent of Array I think?

@krisk
Copy link
Owner

krisk commented Aug 10, 2016

By doing var books = Immutable.fromJS([...]), books is now not an Array:

var books = Immutable.fromJS([{
  title: "Old Man's War"
  author: {
    firstName: "John",
    lastName: "Scalzi"
  }
}])
Object.prototype.toString.call(books) // [object Object]

vs:

var books = [{
  title: "Old Man's War"
  author: {
    firstName: "John",
    lastName: "Scalzi"
  }
}]
Object.prototype.toString.call(books) // [object Array]

As a result, Fuse.js cannot iterate on that 😢 .

@Phyks
Copy link
Author

Phyks commented Aug 10, 2016

forEach is implemented in Immutable.List, so replacing looping by functional forEach would make the code work for both Array and Immutable.List I think. https://facebook.github.io/immutable-js/docs/#/List/forEach

@krisk
Copy link
Owner

krisk commented Aug 10, 2016

I'm merely saying that Fuse.js should not (arguably) be making that decision 😄 .

Note the following:

  • forEach is not supported in all platforms
  • length is not a property of Immutable.List. Instead, we'd have to use the count() function. Which, again, is "Immutable" specific.

This is why the object being passed in should be an [object Array], and not an Array-like object.

@krisk krisk added the question label Aug 10, 2016
@Phyks
Copy link
Author

Phyks commented Aug 10, 2016

Ok. I got your point. So being able to support Immutable objects directly in Fuse would add to Immutable-specific code and it would be better to convert back to JS Arrays and pass them?

@krisk
Copy link
Owner

krisk commented Aug 11, 2016

@Phyks, exactly 😄

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

No branches or pull requests

2 participants