-1

Hello and thank you for your time.

I am trying to make a JavaScript example application working.

The fact is I do not know exactly how to use this static function:

core.utils.js

 static parseUrl(url) {
    const data = {};
    data.filename = '';
    data.extension = '';
    data.pathname = '';
    data.query = '';

    let parsedUrl = URL.parse(url);

    data.pathname = parsedUrl.pathname;
    data.query = parsedUrl.query;

    if (data.query) {
      // Find "filename" parameter value, if present
      data.filename = data.query.split('&').reduce((acc, fieldval) => {
        let fvPair = fieldval.split('=');
        if (fvPair.length > 0 && fvPair[0] == 'filename') {
            acc = fvPair[1];
        }
        return acc;
      });
    }

    // get file name
    if (!data.filename) {
      data.filename = data.pathname.split('/').pop();
    }


    // find extension
    let splittedName = data.filename.split('.');
    if (splittedName.length <= 1) {
      data.extension = 'dicom';
    } else {
      data.extension = data.filename.split('.').pop();
    }

    if (!isNaN(data.extension)) {
      data.extension = 'dicom';
    }

    if (data.query &&
      data.query.includes('contentType=application%2Fdicom')) {
      data.extension = 'dicom';
    }

    return data;
  }

Into the javascript file which has the appliaciton's logic.

I have tried the direct import used by the IDE itself, webstorm in my case:

import CoreUtils from "../../src/core/core.utils";

However, the browser's console says:

Uncaught SyntaxError: Unexpected identifier

And the line where is the error is:

import CoreUtils from "../../src/core/core.utils";

Also I have tried by myself to fix this, and I have read: Calling a javascript function in another js file

So then I did what Fernando Mendez explains, which is to import the js file in the index.html, as:

    <script src="../../src/core/core.utils.js"></script>

So then I commented out the import in the application js:

// import CoreUtils from "../../src/core/core.utils";

And the result is:

Uncaught SyntaxError: Unexpected identifier

In the following line:

import Validators from './core.validators';

Would you be kind and help me a little bit?

In addition my question is related to a previous one: Javascript Trying to run AMIjs examples in local it does not work However I do put this separatedly because of I think the problem is with JavaScript and not a Library.

Thank you for reading me.

Yone
  • 2,064
  • 5
  • 25
  • 56
  • @baao: You can in cutting-edge browsers. The latest Chrome, for instance. – T.J. Crowder Jan 18 '18 at 17:42
  • You can? But does the module loading actually work now? @T.J.Crowder – baao Jan 18 '18 at 17:43
  • The code you've quoted is incomplete, it's a syntax error unless it's within a `class` body. How you use it will depend on the code in `core.utils.js`: If it uses `export`, then you'll need to only use a cutting-edge browser or transpile, because browsers are only just now getting module support. If it doesn't, it may support some other AMD standard like RequireJS or CommonJS, or may just create a global. So we'd really need to know more to help you (other than the above). – T.J. Crowder Jan 18 '18 at 17:44
  • 1
    @baao: Yes indeed. But again, cutting-edge. – T.J. Crowder Jan 18 '18 at 17:44
  • 1
    @baao: FWIW: http://plnkr.co/edit/OC5Pa5BKJAOcwfaNv0LY – T.J. Crowder Jan 18 '18 at 17:49

2 Answers2

0

Are you trying to call the static js function from another js file? It looks like you have a few problems going on here.

1.) import CoreUtils from "../../src/core/core.utils";

From your example code of CoreUtils, I don't see a exported CoreUtils object from src/core/core_utils.js.

In order to utilize the import statement to reference code in another file, you need to export the object from the original file. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import)

This can be accomplished quickly by modifying your function definition in core_utils.js as follows: change static parseUrl(url) { to export function parseUrl(url) {

Then, in application.js: change import CoreUtils from "../../src/core/core.utils"; to import {parseUrl} from "../../src/core/core.utils"

It looks like you are running into a similar error with import Validators from './core.validators';

Hope that helps!

zmferrell
  • 21
  • 4
-1

Try this:

Your parseUrl(url) function should be public, so change static parseUrl(url) to export function parseUrl(url).

Inside your main javascript file, import it like so:

import { parseUrl } from "../../src/core/core.utils";

And then just use it: var parsed = parseUrl(url);

Also, check this out

Tudor
  • 320
  • 1
  • 9