Compartilhar via


Prototypes of JavaScript Globalization & Math, String, and Number extensions

As the HTML5 platform becomes more fully featured, web applications become richer, and scenarios that require server side interaction for trivial tasks become more tedious. This makes deficits in the capabilities of JavaScript as a runtime come into focus.

Microsoft is committed to advancing the JavaScript standard. Through active participation in the Ecma TC39 working group, we have endorsed and pushed for the completion of proposed standards which provide extensions to the intrinsic Math, Number, and String libraries and introduce support for Globalization. We shared the first version of prototypes for the libraries at the standards meeting on the Microsoft campus in July and are shared our Globalization implementation at the standards meeting last week at Apple’s Cupertino campus. In addition, we are also releasing these reference implementations so that the JavaScript community can provide feedback on applying their use in practice.

What’s in this drop

This drop includes extensions to the Math, Number, and String built-in libraries:

Math

String

Number

cosh, sinh, tanh

startsWith, endsWith

isFinite

acosh, asinh, atanh

contains

isNaN

log1p, log2, log10

Repeat

isInteger

sign

toArray

toInteger

trunc

reverse

 

 

To illustrate, a simple code sample using some of these functions is included below:

var aStr = "24-"; var aStrR = aStr.reverse(); var num = aStrR * 1; if (Number.isInteger(num)) { console.log("The sign of " + num + " is " + Math.sign(num)); };

This drop also includes an implementation of the evolving Globalization specification. Globalization is the software discipline that makes sure that applications can deal correctly with changes in number and date formats, for example. It’s a part of the localization of an application to run in a local language. With this library, you can show date and numbers in the specified locale and specify collation properties for the purposes of sorting and searching in other languages. You can also set standard date and number formats to use alternate calendars like the Islamic calendar or formats to show currency as a Chinese Yuan. Again, a code sample illustrates below:

 var nf = new Globalization.NumberFormat(localeList, {
 style : "currency",
 currency : "CNY",
 currencyDisplay: "symbol",
 maxmimumFractionDigit: 1
 })
 
 nf.format(100); // "¥100.00"
 
 
 var dtf = new Globalization.DateTimeFormat(
 new Globalization.LocaleList(["ar-SA-u-ca-islamic-nu-latin"]), {
 weekday : "long",
 })
 
 
 dtf.format() // today's date
 dtf.format(new Date("11/15/2011")); // "الثلاثاء, ١٢ ١٩ ٣٢"
 

How to get the bits

The prototypes should install automatically if you view the Intrinsics Extensions demo and the Globalization demo. Or to install the prototype, run the MSIs found here.

Note that as with all previous releases of HTML5 labs, this is an unsupported component with an indefinite lifetime. This should be used for evaluation purposes only and should not be used for production level applications.

Providing Feedback

We’ve created a couple of sample applications so you can see what this functionality enables. Once you’ve installed the bits, view the Intrinsics Extensions demo and the Globalization demo to see the APIs in action. 

As usual, we encourage you to play with the sample apps, download the prototype, and develop your own app to see how it feels. Once you’ve tried it out, let us know if you have any feedback or suggestions. We look forward to improving JavaScript and making it ever easier to build great web applications using standard APIs.

Thanks for your interest!

Claudio Caldato, Adalberto Foresti – Interoperability Strategy Team