Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert ES6 JavaScript to ES5 without transpiler

Tags:

javascript

I am not familiar with new JavaScript ES6 coding conventions and have been given some code where I need it to be plain old JavaScript ES5.

I need to convert this JS code without the use of Babel or any other transpiler. I cannot use Babel as I am not allowed to use it at work.

I realise that all the "const" can be converted to "var" but unsure of new arrow functions and other items.

I have tried converting but getting:

Uncaught ReferenceError: line is not defined

The ES6 code that I would like converted to ES5 is:

const data = [{ "rec": "1", "region": "LEFT", "intrface": "Line-1" },{ "rec": "1", "region": "LEFT", "intrface": "Line-2" },{ "rec": "1", "region": "RIGHT", "intrface": "Line-3" },{ "rec": "1", "region": "RIGHT", "intrface": "Line-4" }];

const s = Snap("#svg");
const height = 40;
const canvasWidth = 400;
const lineWidth = 180;
const rightOffset = canvasWidth/2 - lineWidth;

const leftLines = data.filter((line) => !isRightLine(line));
const rightLines = data.filter(isRightLine);

leftLines.forEach(drawLine);
rightLines.forEach(drawLine);

const numberOfLines = Math.max(leftLines.length, rightLines.length);
const rectSize = 20;
const rectangles = [];

for (let i = 0; i < numberOfLines; i++) {
    rectangles.push(drawRect(i));
}

function drawLine(data, index) {
    const {intrface} = data;
    const isRight = isRightLine(data);
    const x = isRight ? canvasWidth/2 + rightOffset : 0;
  const y = height * (index + 1);
  const stroke = isRight ? 'red' : 'black';

    const line = s.line(x, y, x + 180, y);
  line.attr({
    stroke,
    strokeWidth: 1
  });

  const text = s.text(x + 10, y - 5, intrface);

  text.attr({
    fill: stroke,
    cursor: 'pointer'
  });

  text.click(() => {
    console.log('clicked', data);

    //window.location.href = "http://stackoverflow.com/";
  });
}

function isRightLine({region}) {
    return region === 'RIGHT';
}

function drawRect(index) {
    const x = canvasWidth/2 - rectSize/2;
  const y = height * (index + 1) - rectSize/2;
    const rectangle = s.rect(x, y, rectSize, rectSize);

  rectangle.attr({
    fill: 'black'
  });

  console.log('rr', x, y);

  return rectangle;
}
like image 259
tonyf Avatar asked May 23 '16 02:05

tonyf


People also ask

Does Babel compile ES6 to ES5?

Babel is a transpiler because it translates JavaScript ES6 to JavaScript ES5. In contrast, a compiler translates source code from a higher level language to a lower level. An example of compilation would be C++ to machine code or Java to JVM bytecode.

Should I compile to ES6 or ES5?

For best performance, you should serve ES6 code to browsers that support it, and only serve ES5 code to browsers that don't support ES6. If you need to statically host your code and serve a single version to all browsers, compile to ES5.

Can you mix ES5 and ES6?

Yes, you can mix both ES6 and ES5 - ES6 is fully backwards compatible, so essentially you could think of your entire app as ES6, but only use the new syntax and functionality in new code.

What is the best way to convert ES6 code to ES5?

There are a number of transpilers that convert the ES6 code to browser readable ES5 code. The most popular one these days is Babel. It was named 6to5 earlier, but then got renamed to Babel to make the name generic. The other popular alternatives are Traceur from Google and TypeScript, the typed JavaScript language from Microsoft.

Why use Babel to transpile ES6 code to ES5?

In this article, we will use Babel to transpile ES6 code to ES5. The reason for choosing Babel is, amongst all other transpilers, Babel supports the most number of ES6 and ES7 features.

How to convert code from ES6 file to CommonJS file?

The ES2015 preset includes the Babel plugin to convert the code into CommonJS style module system, so this plugin detects the presence of modules in the ES6 file and converts it into CommonJS module.

What is the difference between ES5 and babeljs classes?

It has a constructor, which is called inside the class. There is extra code added using babeljs to get the functionality working for classes same as in ES5.BabelJs makes sure the functionality works same as it would have done in ES6.


1 Answers

I will assume that since you are not very familiar with es6 you are probably not very familiar with babeljs.io which gives you an option to convert back:

Check this link up.

"use strict";

var data = [{ "rec": "1", "region": "LEFT", "intrface": "Line-1" }, { "rec": "1", "region": "LEFT", "intrface": "Line-2" }, { "rec": "1", "region": "RIGHT", "intrface": "Line-3" }, { "rec": "1", "region": "RIGHT", "intrface": "Line-4" }];

var s = Snap("#svg");
var height = 40;
var canvasWidth = 400;
var lineWidth = 180;
var rightOffset = canvasWidth / 2 - lineWidth;

var leftLines = data.filter(function (line) {
  return !isRightLine(line);
});
var rightLines = data.filter(isRightLine);

leftLines.forEach(drawLine);
rightLines.forEach(drawLine);

var numberOfLines = Math.max(leftLines.length, rightLines.length);
var rectSize = 20;
var rectangles = [];

for (var i = 0; i < numberOfLines; i++) {
  rectangles.push(drawRect(i));
}

function drawLine(data, index) {
  var intrface = data.intrface;

  var isRight = isRightLine(data);
  var x = isRight ? canvasWidth / 2 + rightOffset : 0;
  var y = height * (index + 1);
  var stroke = isRight ? 'red' : 'black';

  var line = s.line(x, y, x + 180, y);
  line.attr({
    stroke: stroke,
    strokeWidth: 1
  });

  var text = s.text(x + 10, y - 5, intrface);

  text.attr({
    fill: stroke,
    cursor: 'pointer'
  });

  text.click(function () {
    console.log('clicked', data);

    //window.location.href = "http://stackoverflow.com/";
  });
}

// you might want to change this - howeverever you will have to change everywhere in the code that calls isRightLine to pass a primitive vs an object.
function isRightLine(_ref) {
  var region = _ref.region;

  return region === 'RIGHT';
}

function drawRect(index) {
  var x = canvasWidth / 2 - rectSize / 2;
  var y = height * (index + 1) - rectSize / 2;
  var rectangle = s.rect(x, y, rectSize, rectSize);

  rectangle.attr({
    fill: 'black'
  });

  console.log('rr', x, y);

  return rectangle;
}
like image 159
Neta Meta Avatar answered Nov 07 '22 04:11

Neta Meta