¿

Beautiful APIS are all roses with thorns

1. ToLocaleString related background

Nuggets – Want to be lazy, toLocaleString?

MDN – Date​.prototype​.toLocale​String()

MDN – Number​.prototype​.toLocale​String()

This API will save you a lot of work by converting time/number to a format that doesn’t require you to write a re or drag a library

1.1 Common time conversion

The time format yyyY-MM-DD HH: MM :ss is used as an example. The library day.js or moment.js is used to complete the time format

format('YYYY-MM-DD HH:mm:ss')
Copy the code

Using toLocaleString

new Date()
  .toLocaleString('zh', { hour12: false/ /})"2019/4/21 13:00:09"
  .replace(/\//g, The '-') / /"The 2019-4-21 13:00:23"
Copy the code

1.2 Common digital conversion

For example, adding a comma to every three digits of an integer is usually done with a re

const number = 123456789 number.toString().replace(/\B(? =(\d{3})+\b)/g,', ') / /"12345678"
Copy the code

Using toLocaleString

const number = 123456789
number.toLocaleString() // "12345678"
Copy the code

2. The pit

It all looks pretty good, and while it has browser compatibility issues, I think node.js should be able to continue the good stuff

However, during a commit, the Node version was upgraded and there were a lot of problems with toLocaleString

FROM node:8.9-alpine // Change to FROM node:lts-alpineCopy the code

To break the good news, the test code is as follows

// index.js 
console.log("en: " + new Date().toLocaleString('en', { hour12: false }))
console.log("zh: " + new Date().toLocaleString('zh', { hour12: false}) // Dockerfile FROM node:8.9-alpine COPY. /app CMD ["node"."/app/index.js"]

Copy the code

Mirror node:8.9-alpine output

En: 4/21/2019, 06:42:03 en: 2019-4-21 06:42:03Copy the code

Mirror node:10.15-alpine output

en:   4/21/2019, 06:43:13
zh:   4/21/2019, 06:43:13
Copy the code

Mirror node:11.14-alpine output

en:   4/21/2019, 06:43:59
zh:   4/21/2019, 06:43:59
Copy the code

Mirror node:11.14 Output

en:   4/21/2019, 06:46:54
zh:   4/21/2019, 06:46:54
Copy the code

Native node.js 11.13 output, no wonder native debugging is good….

en:   4/21/2019, 14:49:27
zh:   2019-4-21 14:49:27
Copy the code

Chrome

en:   4/21/2019, 14:51:33
zh:   2019/4/21 14:51:33
Copy the code

3. Specific reason: internationalization support

Many features of Node.js/JavaScript provide internationalization support, for example

  • String.prototype.normalize()

  • String.prototype.toLowerCase()

  • Date.prototype.toLocaleString()

  • require('buffer').transcode()

  • . slightly

Node.js(and its underlying V8 engine) uses ICU for these functions

But supporting all of the world’s locales requires a very large ICU data file, and most users use only a small portion of ICU functionality, so Node.js provides only a subset of the full ICU data set by default

3.1 Build ICU options for Node.js

  • –with-intl=none/– with-intl: disables all internationalization functions

  • –with-intl= system-ICU: Depending on the system, most Linux distributions have ICU installed

  • –with-intl= small-ICU (default): part of a complete ICU, equivalent to: English only

  • –with-intl= full-ICU: full support

3.2 Provide ICU data at run time

  1. NPM module: full-ICU: Installs complete ICU data
  • throughnpm i full-icu, the data files will be copied in the./node_modules/full-icu.
  1. By setting environment variablesNODE_ICU_DATASpecify ICU options at runtime
  • env NODE_ICU_DATA=/some/directory node

3.3 test

  • NPM install full-ICU, make sure package.json has full-ICU

  • Write the following Dockerfile

FROM node:lts-alpine WORKDIR /app COPY package.json /app RUN yarn --registry=https://registry.npm.taobao.org COPY . /app  ENV NODE_ICU_DATA /app/node_modules/full-icu CMD ["node"."/app/index.js"]
Copy the code

The output

en:   4/21/2019, 09:13:13
zh:   2019/4/21 09:13:13
Copy the code

2019-4-21 09:13:13 Still can explain valid – –

4. Summary

  1. The toLocaleString API has more incompatibilities in browser environments and inconsistencies in Node environments, but can be resolved by setting up the ICU

  2. From a development perspective: while the API is cool, day.js/momemnt.js is more stable

  3. Refer to the official documentation for Internationalization Support for Node for knowledge about Internationalization Support