moleculerjs / moleculer

:rocket: Progressive microservices framework for Node.js
https://moleculer.services/
MIT License
6.07k stars 575 forks source link

[lodash] Remove usage of _.omit #752

Open abdavid opened 4 years ago

abdavid commented 4 years ago

See #433 for context.

Describe the solution you'd like Remove usage of _.omit throughout Moleculer.

Describe alternatives you've considered Source: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_omit

// Native
var { a, c, ...result2 } = object;
console.log(result2)
// output: { 'b': '2' }

Additional context https://github.com/moleculerjs/moleculer/search?q=_.omit&unscoped_q=_.omit

icebob commented 4 years ago

For omit need a function because the name of properties comes from users.

abdavid commented 4 years ago

For omit need a function because the name of properties comes from users.

So we are actually talking about https://lodash.com/docs/4.17.15#omitBy ?

icebob commented 4 years ago

No, it's the _.omit but the list of properties comes from another variable, so not static.

abdavid commented 4 years ago

No, it's the _.omit but the list of properties comes from another variable, so not static.

This one here you are thinking about? https://github.com/moleculerjs/moleculer/blob/07dbf1c0ead1038d157ec6616ac76509b6974549/src/service.js#L197

icebob commented 4 years ago

Yeah, e.g.

abdavid commented 4 years ago

We could do an inverse of suggestion from #748 then maybe?

const omit = (o, xs) => {
    if(!Array.isArray(xs))
        xs = [xs];
    const objectPropertyNames = Object.getOwnPropertyNames(o)
        .filter(n => !xs.includes(n));
    return objectPropertyNames.reduce((acc,x) => {
        if(o.hasOwnProperty(x))
            acc[x] = o[x]
        return acc;
    }, {})
};
icebob commented 4 years ago

By the way, omit & pick should work with nested paths as well :) _.pick(obj, ["a.b", "a.c"]);

abdavid commented 4 years ago

Noted :) I belive we then might be able to reuse #753 implementation there