2

I am trying to write a tool that takes a CSV and dynamically generates a definition based on the header row?

For example, a CSV with:

Title(STRING), Description(TEXT)
Title Example, Description Example
...

the Sequelize docs specify, for example:

var Entry = sequelize.define('Entry', {
  title: Sequelize.STRING,
  description: Sequelize.TEXT
})

How could I write this definition so that it could be dynamically defined - so that title and the data type Sequelize.STRING could be dynamically generated based on the CSV header row?


EDIT

Ok, after some research, I think the obvious question is "How to use variable names as dynamic key names in object literal" and has been answered several times.

As a result, it is simple to write this in bracket notation so:

var definitionObj = {}
definitionObj['title'] = sequelize.STRING;
definitionObj['description'] = sequelize.TEXT;

var Entry = sequelize.define('Entry', definitionObj);

However, then my question now is how do I use ES6 Computed Property Names in node? I'm using node 0.12.2 which I thought had ES6 support, and even with the --harmony flag, this simple code fails:

var Entry = sequelize.define('Entry', {
  ['title']: Sequelize.STRING,
  ['description']: Sequelize.TEXT
});

with SyntaxError: Unexpected token [

Is the only option really to go to with io.js?

EDIT 2

Actually this syntax still fails even with iojs, so I must be doing something wrong?

waffl
  • 5,179
  • 10
  • 73
  • 123
  • 1
    Here is the support for the feature you're trying to use: https://kangax.github.io/compat-table/es6/#object_literal_extensions_computed_properties. Neither node nor io currently support it. – Noah Freitas Jul 19 '15 at 15:12
  • Also, you don't seem to need any computed property names here, as all of your object definitions are equivalent (and use static `title` and `description` names)?! – Bergi Jul 19 '15 at 15:15
  • @NoahFreitas ah I see, so I'm not completely crazy then. Do you happen to know what this is referred to on the list? – waffl Jul 19 '15 at 15:45
  • @bergi sorry that was just for the example, in the actual use I want to use dynamic names as mentioned in the beginning (pulling header names from a CSV). – waffl Jul 19 '15 at 15:45
  • Can you please show how you pull the header names from the CSV? – Bergi Jul 19 '15 at 15:49
  • @NoahFreitas also perhaps you want to make that as an answer and I can accept it – waffl Jul 19 '15 at 16:32
  • @Bergi I'm parsing the CSV with node-csv and just pulling the first row – waffl Jul 19 '15 at 16:33
  • 2
    I'd highly recommend that you check out [babel](http://babeljs.io) – Mulan Jul 19 '15 at 17:47
  • with babel I've managed to get it running with computed properties :) (although perhaps a bit overkill as bracket syntax is working in this case without the overhead of a precompiler, but good to know) – waffl Jul 20 '15 at 03:44
  • It is now supported since NodeJS 4.x. – lapo Feb 29 '16 at 10:50

3 Answers3

6

In current Node release > 4.x you have some support for ES6. Status can be found in documentation: https://nodejs.org/en/docs/es6/

What you are looking for is called

Computed (dynamic) property names

and it is fully supported now. Details can be found here: https://github.com/lukehoban/es6features#enhanced-object-literals

Usage:

var obj = {
    // Computed (dynamic) property names
    [ 'prop_' + (() => 42)() ]: 42
};

Will produce object: {prop_42: 42}

Aleksander Gralak
  • 1,509
  • 10
  • 26
2

The ECMAScript 6 compatibility table shows that neither Node nor io.js currently have support for computed properties. The data is under object literal extensions > computed properties.

Noah Freitas
  • 17,240
  • 10
  • 50
  • 67
1

Now can use it in iojs v3.0

Changelog https://github.com/nodejs/io.js/blob/master/CHANGELOG.md#user-content-notable-changes

zhiyelee
  • 429
  • 4
  • 14