-1

I have a piece of code which uses spread syntax.

On my main server, the node js version is 0.10. For some reason, it's not possible to upgrade node js. So I'm converting all the arrow functions such that they are compatible with the old version of node js. I'm not able to convert the spread operator. I tried with Object.assign but it keeps showing syntax error. I'm confused on how to convert spread operator to normal javascript code.

Here is my code :

senddata = Array.from({ ...data, length: Math.max(...Object.keys(data)) });

I'm finding it really confusing to convert this. In fact I'm just not able to understand where to add Object.assign and how to fix the syntax errors regarding it.

For arrow function I converted them like this:

with arrow function :

dag.get('obj')
      .then((data) => {

        run();
    }, (err) => {
        console.log(err)
      })

without arrow function

dag.get('obj')
      .then(function(data)  {

        run();
    }, function(err) {
        console.log(err)
      })

But not able to convert spread syntax.

This is what I tried :

Object.assign({data},length: Math.max(Object.assign({Object},keys(data))

Moreover, how can I write the same code without using spread syntax or Object.assign ?

After referring to the answers and suggestions posted below,here is the code :

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

senddata = Array.from({ ...data,
  length: Math.max.apply(Math, _toConsumableArray(Object.keys(data)))
});

But I still get an error for ...data

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
node_man
  • 1,359
  • 4
  • 23
  • 50
  • 2
    *"I tried with `Object.assign` but it keeps showing syntax error"* - could you show a [mcve] of that? That's how transpilers like Babel convert spreads. – jonrsharpe Feb 22 '19 at 11:08
  • post `data` value – Thamaraiselvam Feb 22 '19 at 11:09
  • It's not helpful to show the code you *have* successfully converted, you're asking about the bit you *haven't* - show *that* attempt. – jonrsharpe Feb 22 '19 at 11:11
  • added the code for spread operator – node_man Feb 22 '19 at 11:13
  • Babel has an [online transpiler that will basically do this for you](https://babeljs.io/repl#?babili=false&browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=M4UwdgJhCGAu0AIC8CCCAnd0CeA6AZugPYC2AFAN4K40zwA0CANuAOawAWAXAgLJwdcJaAA8yNXAHkARgCsQAY1i4A1iGzAydaAEodCAL46A3EA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=es2015%2Creact%2Cstage-2&prettier=false&targets=&version=7.3.3) – Liam Feb 22 '19 at 11:13
  • 1
    Or better yet, just [use Babel](https://babeljs.io) – Liam Feb 22 '19 at 11:14
  • how can i write that same code without the spread operator? – node_man Feb 22 '19 at 11:15
  • Liam gave you the answer. Node 0.10, is that from 2009? – connexo Feb 22 '19 at 11:18
  • yea it's from 2009. It's an old server running some other scripts which will stop working if I upgrade node. – node_man Feb 22 '19 at 11:20
  • I checked Liam's answer but in the output, the spread operator is still there. – node_man Feb 22 '19 at 11:20
  • When you say that it is not possible to update Node, do you mean technically not possible, or you *should* not update it ? If the problem is technical, have you tried NVM (https://github.com/creationix/nvm) ? – Seblor Feb 22 '19 at 11:21
  • I will try to implement Liam's answer and will let you know – node_man Feb 22 '19 at 11:21
  • It is technically possible to update node but I should not update it – node_man Feb 22 '19 at 11:22
  • @node_man You need to select ENV_PRESET on the left, then check Node and configure your version number. – connexo Feb 22 '19 at 11:24

2 Answers2

3

{ ...obj } object spread syntax is syntactic sugar for Object.assign({}, obj).

Math.max(...iterable) is regular spread syntax, for function arguments it's translated to apply call.

It should be:

Object.assign({}, data, { length: Math.max.apply(null, Object.keys(data))) })
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
1

Babel creates this for your line of code with the correct preset:

"use strict";

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

senddata = Array.from(_objectSpread({}, data, {
  length: Math.max.apply(Math, _toConsumableArray(Object.keys(data)))
}));
connexo
  • 53,704
  • 14
  • 91
  • 128