moleculer icon indicating copy to clipboard operation
moleculer copied to clipboard

[lodash] Remove usage of _.omit

Open abdavid opened this issue 4 years ago • 8 comments

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

abdavid avatar May 22 '20 19:05 abdavid

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

icebob avatar May 22 '20 19:05 icebob

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 ?

abdavid avatar May 22 '20 19:05 abdavid

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

icebob avatar May 22 '20 19:05 icebob

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

abdavid avatar May 22 '20 19:05 abdavid

Yeah, e.g.

icebob avatar May 22 '20 19:05 icebob

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;
    }, {})
};

abdavid avatar May 22 '20 19:05 abdavid

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

icebob avatar May 22 '20 20:05 icebob

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

abdavid avatar May 22 '20 20:05 abdavid