3

I've got:

const fs = require('fs');

const packageConfig = JSON.parse(fs.readFileSync('./package.json'));
const { jspm: { configFile: jspmConfigFile }} = packageConfig;

but packageConfig's value for configFile could be undefined. In that case, I'd like to default jspmConfigFile to 'config.js

Is it possible to do this without creating an extended packageConfig object?

I realize I could do something like:

const { jspm: { configFile: jspmConfigFile }} = _.extend({
    jspm: { config: 'config.js'}
}, packageConfig);

but that's pretty messy just to get a sensible default with destructuring.

What am I missing?

2
  • How about const jspmConfigFile = packageConfig.jspm.configFile || 'config.js';? Commented Oct 4, 2015 at 19:59
  • Well, I thought about this, but I was trying to learn more about destructuring + leverage the fact I can safely access nested properties without checking for the existence of parent properties. Commented Oct 4, 2015 at 20:05

2 Answers 2

5

The syntax for default values in object destructuring is:

const { jspm: { configFile: jspmConfigFile = 'config.js' }} = packageConfig;

But this is not yet implemented in Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=932080

Sign up to request clarification or add additional context in comments.

1 Comment

No problem. And hi @SeanAnderson!
2

I'm not really sure ES2015 destructuring actually buys you anything here. At the end of the day since you're only destructuring a single element. You're pretty much writing a fancy way to do:

const jspmConfigFile = packageConfig.jspm.configFile;

You can assign a default value with = 'defaultValue' in the innermost section but I'm not convinced that's actually better than the ES5 || 'default' version of it.

As a side note, you can require JSON files in Node:

const jspmConfigFile = require("./package.json").jspm.configFile || "default";

Is a one liner which I think is shorter. You can import it in a one liner directly too as:

import  { jspm: { configFile: jspmConfigFile = 'config.js' }} from "./package.json"

But I honestly don't think that's clearer.

9 Comments

Well, it does get me the benefit of not having to check if "jspm" is undefined without erroring.
@SeanAnderson yes, but IMO destructuring a single nested property often creates confusing code. I've considered whether or not to add this answer for a bit and decided to add it when Rob added his answer to provide an alternative to the "ES2015" way :)
Yeah, it's certainly a valid argument. I think after seeing the resulting code I'll stick with the old way, but I'm glad I at least checked out other ways.
@SeanAnderson Why do you think that you can use it without checking whether jspm is defined? ({ jspm: { configFile: jspmConfigFile }} = {}); throws "TypeError: can't convert undefined to object" in Firefox 41.0.
Oh. I thought that was a benefit? Clearly still super new to ES6. Alrighty, I dont' see the point in using it at all for this scenario then!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.