Part 2: What’s New with JavaScript in MarkLogic 10?
Posted by Dermot Smyth on 08 February 2020 05:10 PM |
|
V8 Engine Upgrade in MarkLogic 10In 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:
Now I’ll take a deep dive into some of the new methods I have found most useful in my work. String Utility MethodsSome new utility methods have been added to let stringToPad = "7"; stringToPad.padStart(3 , "0"); // -> '007' stringToPad.padEnd(3 , "0"); // -> '700' Similarly, there is let stringToTrim = " hello world "; stringToTrim.trimStart(); // -> 'hello world ' stringToTrim.trimEnd(); // -> ' hello world' Object FunctionsAnother useful feature in the new V8 Engine is that there are now a object const person = { firstName: 'Michael' , lastName: 'Knight' }; Object.values(person) // --> ["Michael", "Knight"] With MarkLogic 10, we can use 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 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 LibraryThe 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 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 StepsWe 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. The post Part 2: What’s New with JavaScript in MarkLogic 10? appeared first on MarkLogic. | |