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

Make Underscorejs functions functioning on generators #2924

Closed
ghost opened this issue Apr 17, 2021 · 3 comments
Closed

Make Underscorejs functions functioning on generators #2924

ghost opened this issue Apr 17, 2021 · 3 comments

Comments

@ghost
Copy link

ghost commented Apr 17, 2021

note: I port my question on stackoverflow here.

I have dozens of functions acting on arrays. See here for the sake of illustration.

As you can see I am using Underscorejs for most operations.

However I want to write a transformation on my main iteratee which is a list of JSON objects like the following:

var listings = [{ title: 'title1', d: 0, desc: 'hello world', pass: 'qub7s1ya', tags: ["tag1", "tag2"] }]

As I want to really optimize performance, I want to write a transformer on desc values. I will be using lzutf8 for this; So that I store in disk and in memory only compressed values. On each Underscorejs operation I would like to act on the decompressed value of key desc.

Apparently, Underscore operations do not act on generators directly, so my question is how to achieve this ?

edit: I believe all operations of Underscorejs are sequential, so why not support generators ? or am I missing something ?

@jgonggrijp
Copy link
Collaborator

@bacloud14 Please include a link to the Stack Overflow question when you do this. I found it though:

https://stackoverflow.com/questions/67138146/make-underscorejs-functions-functioning-on-generators

I consider this a duplicate of #2147 and will close the current issue for this reason. I will still answer your question on Stack Overflow, because I believe the answer could be useful to a wider audience. Please clarify what exactly you're trying to do first, though, by updating your SO question as I've asked here.

@ghost
Copy link
Author

ghost commented Apr 18, 2021

@jgonggrijp thank you so much for the follow

@ghost
Copy link
Author

ghost commented Apr 19, 2021

for sake of illustration here is an example with getter attached at runtime which saved my day (instead of generators)

listings = [{ title: 'title1', d: 0, desc: 'oipfjezojifze', pass: 'qub7s1ya', tags: ["tag1", "tag2"] }]
listings.forEach(item => {
    Object.defineProperty(item, 'desc_', {
        get: function () { return (this.desc.toUpperCase()) }
    });
});
var results = _.where(listings, { desc_: "oipfjezojifze".toUpperCase() })
// it also returns the object intact
console.log(results[0].desc_)
// and other methods _#filter, _#findWhere, _#where, _#reject, _#pick do as well

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

1 participant