Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test: Number.toLocaleString()

I would have expected (10000).toLocaleString('de-DE') to return "10.000" but instead I get "10000".

Is there a reason this is not supported? Are there better ways to format numbers?

like image 799
Ibti Avatar asked Jul 24 '16 21:07

Ibti


People also ask

What does toLocaleString() do?

The toLocaleString() method returns a string with a language-sensitive representation of this date. In implementations with Intl. DateTimeFormat API support, this method simply calls Intl. DateTimeFormat .

What is toLocaleString in angular?

Return Value: The toLocaleString() method in TypeScript returns a human readable string representing the number using the locale of the environment.


1 Answers

It is a webkit issue, and PhantomJS doesn't want to maintain internationalization... so unfortunately we are stuck with this for some undisclosed time.

https://github.com/ariya/phantomjs/issues/12581

What I ended up doing is writing a custom matcher that checks for both, since I run in Chrome and PhantomJS.

jasmine.addMatchers({
    isAnyOf: (util, customEqualityTesters) => {
      return {
        compare: (actual, expected) => {
          let result = {};
          for (let expect of expected) {
            console.log(actual == expect);
            if (expect == actual) {
              result.pass = true;
              return result;
            }
          }
          result.pass = false
          return result;
        }
      }
    }
  })

Then you can use it like

expect(actual).isAnyOf(['10000', '10.000']);
like image 62
Dinny Avatar answered Oct 18 '22 23:10

Dinny