-
-
Save makeusabrew/4451255 to your computer and use it in GitHub Desktop.
// 1) colon aligned, no spaces where possible | |
var Foo = { | |
host :"foo", | |
db :"bar", | |
details:"baz" | |
}; | |
// 2) colon aligned, space after key | |
var Foo = { | |
host : "foo", | |
db : "bar", | |
details: "baz" | |
}; | |
// 3) colon aligned, space before & after key | |
var Foo = { | |
host : "foo", | |
db : "bar", | |
details : "baz" | |
}; | |
// 4) space aligned, no spaces where possible | |
var Foo = { | |
host: "foo", | |
db: "bar", | |
details:"baz" | |
}; | |
// 5) space aligned, space after key | |
var Foo = { | |
host: "foo", | |
db: "bar", | |
details: "baz" | |
}; | |
// 6) space aligned, space before & after key | |
var Foo = { | |
host : "foo", | |
db : "bar", | |
details : "baz" | |
}; | |
// or something else entirely? |
Generally 2 or no alignment at all. One thing I do do though, is make sure keys are quoted. For some reason unquoted keys just don't sit right with me
#5, but without the alignment.
var Foo = {
host: "foo",
db: "bar",
details: "baz"
};
The same as @stephenmelrose for me, I used alignment for a while but didn't like all the whitespace diffs in commits - just preference.
@stephenmelrose & @JamieMason - interesting, I go with #5 sometimes (mainly as I have zero consistency) but always feel guilty about it. Whitespace diff is a good point.
@mheap - too much JSON in your life :D
@JamieMason @makeusabrew - You can ignore whitespace in diffs. Just use the -w flag on the command line, or add ?w=0 to any Github diff URL's to ignore it
Another vote for #5, sometimes with alignment, sometimes without.
I just don't like the alignment, I find it much easier to read without, which I know is strange. I also think it's neater without.
#5 for me, but I occasionally slip in to the same as stephenmelrose
Ah cool, thanks for the tip @mheap.
#5 without alignment fits nicely and consistently with 1 line definitions too, e.g.
var Foo = { host: "foo", db: "bar" };
var Foo = {
host: "foo",
db: "bar",
details: "baz"
};
Believe it or not I was going to list various other options without indentation (such as @stephenmelrose's variation of #5) but thought I'd get burnt at the stake for even suggesting such a thing. Good to know :)
#5 but without alignment, same as @stephenmelrose
I'm with @stephenmelrose
@makeusabrew I think comma-first would have got you burnt at the stake :)
Same as steve for this, the real question is where to put the newlines and how much to indent when passing an object literal as a function argument!
@davedevelopment Ewww.
Wow, this is 4-year-old thread. But this how I align things.
Right align keys on colon. Values left aligned with space after colon.
{
key: 1,
string: "value",
object_key: {
string_key: "string",
integer_key: 2
},
function_key: () => {
}
}
Personally I try and stick to 2... but often meander into 3... or 1. Pretty hopeless really.