Last active
April 13, 2016 03:08
-
-
Save twalker/d33941123b89fe28c059931679994673 to your computer and use it in GitHub Desktop.
Repro example for needing to Polyfill Date/Number prototypes in node.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express(); | |
if (global.Intl) { | |
// `Intl` exists, but it doesn't have the data we need, so load the | |
// polyfill and patch the constructors we need with the polyfill's. | |
var IntlPolyfill = require('intl'); | |
Intl.NumberFormat = IntlPolyfill.NumberFormat; | |
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat; | |
// HERE, Date/Number prototypes still seem to need polyfilled ([email protected], [email protected]). | |
IntlPolyfill.__applyLocaleSensitivePrototypes(); | |
} else { | |
// No `Intl`, so use and load the polyfill. | |
global.Intl = require('intl'); | |
// HERE, Even when replacing Intl with the polyfill, | |
// the the prototypes need polyfilled. | |
global.Intl.__applyLocaleSensitivePrototypes(); | |
} | |
app.get('/', function (req, res) { | |
var date = new Date(); | |
var options = { | |
weekday: 'long', | |
year: 'numeric', | |
month: 'long', | |
day: 'numeric' | |
}; | |
var ar = date.toLocaleDateString('ar', options); | |
var gb = date.toLocaleDateString('en-GB', options); | |
res.send('ar: ' + ar + ' gb: ' + gb); | |
}); | |
app.listen(3000, function () { | |
console.log('Example app listening on port 3000!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment