Reputation:
This line of code is longer than 80 characters - is there any way I can make it shorter?
const playerVars = Object.assign({}, oldSettings.playerVars, newSettings.playerVars);
Here is the entire project on a JSFiddle.
This is the function in which the line is contained:
function combineSettings(oldSettings, newSettings) {
const playerVars = Object.assign({}, oldSettings.playerVars, newSettings.playerVars);
const settings = Object.assign({}, oldSettings, newSettings);
settings.playerVars = playerVars;
return settings;
}
Upvotes: 1
Views: 128
Reputation: 19372
I like to code as if I write in functional programming language.
So my solution for Your question will be:
function combineSettings(oldSettings, newSettings) {
return Object.assign(
{},
oldSettings,
newSettings,
{
playerVars:
Object.assign(
{},
oldSettings.playerVars,
newSettings.playerVars
)
}
);
}
const settings1 = {
someSet1: 'a',
playerVars: {
notifications: true,
someVar: 1
}
};
const settings2 = {
someSet1: 'aa',
someSet2: 'b',
playerVars: {
notifications: false,
mail: "[email protected]"
}
};
console.log(combineSettings(settings1, settings2));
If You want to minify one-liner You've to move that variables to array and then just call Object.assign
with spread operator:
function combineSettings(oldSettings, newSettings) {
var playerVars = [
oldSettings.playerVars,
newSettings.playerVars
];
playerVars = Object.assign({}, ...playerVars); // Your one-liner
// other operations
return {playerVars};
}
const settings1 = {
playerVars: {
notifications: true,
someVar: 1
}
};
const settings2 = {
playerVars: {
notifications: false,
mail: "[email protected]"
}
};
console.log(combineSettings(settings1, settings2));
OR
Split it to lines:
function combineSettings(oldSettings, newSettings) {
var playerVars =
Object.assign(
{},
oldSettings.playerVars,
newSettings.playerVars
);
// other operations
return {playerVars};
}
const settings1 = {
playerVars: {
notifications: true,
someVar: 1
}
};
const settings2 = {
playerVars: {
notifications: false,
mail: "[email protected]"
}
};
console.log(combineSettings(settings1, settings2));
Warning: By changing parameter names to be shorter we are loosing their self-documentation. oldSettings
, newSettings
are self-explaining.
So only beautiful way to make code readable and extendable by splitting long line to many lines.
I've googled it by query: js style guide
and found these good resources to read:
https://google.github.io/styleguide/jsguide.html
https://github.com/felixge/node-style-guide
Extra: most of IDEs has beautify method that will do it for You
Upvotes: -1
Reputation: 97
Why not just create a few variables? Like this
function combineSettings(oldSettings, newSettings) {
const oldSet1 = oldSettings.playerVars;
const newSet1 = newSettings.playerVars;
const playerVars = Object.assign({}, oldSet1, newSet1);
const settings = Object.assign({}, oldSettings, newSettings);
settings.playerVars = playerVars;
return settings;
}
Upvotes: 0
Reputation: 18515
You can also make an "alias"
function for the whole Object.assign({}, ...args)
and move the params to separate lines like this:
const merge = (...args) => Object.assign({}, ...args)
function combineSettings(oldSettings, newSettings) {
const playerVars = merge(
oldSettings.playerVars,
newSettings.playerVars
);
const settings = merge(oldSettings, newSettings);
settings.playerVars = playerVars;
return settings;
}
This is more trivial/simple, readable and also adds merge
as another function you can later use etc.
FYI if you are using lodash
merge function already exists as well as _.defaults
and _.extend/assignIn
. Note: obviously not suggesting you use lodash just for this :)
Upvotes: 1
Reputation: 19581
Although this is not what StackOverflow is for you can refactor a bit more :
function combine( ...args ) {
return Object.assign( {}, ...args );
}
function combineSettings( oldSettings, newSettings ) {
return combine( oldSettings, newSettings, {
playerVars : combine( oldSettings.playerVars, newSettings.playerVars )
} );
}
console.log( combineSettings(
{ a : "foo", playerVars : { b : "bar" } },
{ b : "bar", playerVars : { a : "foo" } }
) ); // { a: 'foo', playerVars: { b: 'bar', a: 'foo' }, b: 'bar' }
Upvotes: 0
Reputation: 44105
Just change your parameter names to be shorter:
function combineSettings(oldS, newS) {
const playerVars = Object.assign({}, oldS.playerVars, newS.playerVars);
const settings = Object.assign({}, oldS, newS);
settings.playerVars = playerVars;
return settings;
}
Upvotes: 2