Part 2: What’s New with JavaScript in MarkLogic 10?

February 08, 2020 Data & AI, MarkLogic

V8 Engine Upgrade in MarkLogic 10

In this continuation of the series, What’s New with JavaScript in MarkLogic 10?, we’ll discuss in detail about the new API’s the V8 Engine upgrade comes with. In the previous blog, we mentioned how MarkLogic 10 comes with a ton of new features, some of which include:

  1. The addition of JavaScript Modules, also known as MJS — which was discussed in detail in the previous blog.
  2. The V8 Engine was upgraded from version 5.3 to version 6.7. This is the actual motor that ‘powers’ JavaScript in MarkLogic.
  3. Additional features from the ECMAScript 2015 JavaScript standard have been added. Go to http://www.ecma-international.org/ecma-262/6.0/ to find out what you can do with ECMAScript 2015.

Now I’ll take a deep dive into some of the new methods I have found most useful in my work.

String Utility Methods

Some new utility methods have been added to string, including padStart() and padEnd(). For example:

let stringToPad = "7";

stringToPad.padStart(3 , "0"); // -> '007'

stringToPad.padEnd(3 , "0"); // -> '700'

Similarly, there is trimStart() and trimEnd():

let stringToTrim = " hello world ";

stringToTrim.trimStart(); // -> 'hello world '

stringToTrim.trimEnd(); // -> ' hello world'

Object Functions

Another useful feature in the new V8 Engine is that there are now a object values() function. In MarkLogic 9, we had the Object.keys()function, which returned the property names of a given object. However, if you wanted to get the values of the properties in an object — say a person JSON object — you were forced to do something like this:

const person = { 
   firstName: 'Michael' , 
   lastName: 'Knight' 
};

Object.values(person) 

// --> ["Michael", "Knight"]

With MarkLogic 10, we can use Object.values() function to do the same thing:

const person = {

  firstName: 'Michael' ,
  last Name: 'Knight'

};

Object.entries(person)
.map(([key, value])
    => key + ":" + value.toUpperCase());

//-> ["firstName=Michael", "lastName=Knight"]

Another useful function that was added is Object.entries(), which  allows you to iterate the actual property-value pairs inside a JavaScript object. For example:

const person = {
  firstName: 'Michael',
  last Name: 'Knight'
};

Object.entries(person)
.map(([key, value])
    => key + ":" + value.toUpperCase());

//-> ["firstName=Michael", "lastName=Knight"]

These new functions allow you to write more concise code when compared to MarkLogic 9.

Internationalization Library

The upgrade also provides more features for the internationalization library. Firstly, we can specify explicitly how we want a date formatted. In the example below, we specify how we want to display a French formatted date:

let date = DATE.UTC(2019, 11, 17, 15, 0, 42);
let formatter = new Intl.DateTimeFormat('fr-fr', {
  weekday: 'long',
  year: 'numeric', 
  month: 'numeric', 
  day: 'numeric', 
  hour: 'numeric',
  minute: 'numeric',
  second: 'numeric',
});
formatter.format(date);

//-> mardi 17/12/2019 à 16:00:00:42

The result contains the name of the day of the week followed by the date and time. If you want to take this result and parse particular parts of it, like the week, day, or time, you would have to do a bit more work using string manipulation to display that on a GUI. To make this a little less complicated, the function formatToParts() can be used. For example:

formatter.formatToParts(date);

[
  {
     "type": "weekday",
     "value": "mardi"
   },
  {
     "type": "literal",
     "value": " "
   },
  {
     "type": "day",
     "value": "17"
   },
  {
     "type": "literal",
     "value": "/"
   }
]

So instead of having the date returned as a string, we get an array of objects containing the formatted date in parts. This makes using the individual parts of the date information easier.

Next Steps

We just had a brief look at the new APIs that came with the V8 Engine upgrade in MarkLogic 10 (and how you can use them). If you would like to review over the differences between Server-side JavaScript (SJS) and JavaScript Module (MJS) scripts, read the previous blog in the series, JavaScript Modules in MarkLogic 10.

In the next blog in the series, we will discuss another piece of functionality that was included in the upgrade, Object Rest and Spread Properties, and how they can be used in your Data Hub code.

Dermot Smyth

Dermot is a Senior Technologist with over 15 years of experience architecting, developing and delivering software using state-of-the-art technologies. Prior to joining MarkLogic, he worked internationally in a broad range of industries including Investment Banking, Insurance, Energy and Tertiary Education.

Originally from Australia, he now lives in Paris and is enjoying working his way through all the different types of French cheese (over 400!)