Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AshCoolman/b7e9d5fd7a478d0c5c91f432088c4a91 to your computer and use it in GitHub Desktop.
Save AshCoolman/b7e9d5fd7a478d0c5c91f432088c4a91 to your computer and use it in GitHub Desktop.
Create number format from Number.prototype.toLocaleString
// The following must be executed in browser, and can be pasted into
//
// WHY?
// 1. Browser support is a bit spotty
// 2. Testing with node framework made harder as node doesn't come with internationalistion (full-icu) data by default
// 3. Int shim is ~56Kb https://github.com/andyearnshaw/Intl.js/blob/master/dist/Intl.min.js. This is ~7kb
var getNumberFormatFromIso2 = (iso) => {
let thousandsSeparatorSymbol;
let decimalSymbol;
try {
[, thousandsSeparatorSymbol = ',', decimalSymbol = '.'] = (new Intl.NumberFormat(iso)).format(1000.1).match(/^\d(.)\d\d\d(.)\d/);
} catch (e) {
}
return {
thousandsSeparatorSymbol,
decimalSymbol,
};
};
var genNumberFormat = () => {
const iso2List = 'AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BQ BR BS BT BV BW BY BZ CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ DE DJ DK DM DO DZ EC EE EG EH ER ES ET FI FJ FK FM FO FR GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY HK HM HN HR HT HU ID IE IL IM IN IO IQ IR IS IT JE JM JO JP KE KG KH KI KM KN KP KR KW KY KZ LA LB LC LI LK LR LS LT LU LV LY MA MC MD ME MF MG MH MK ML MM MN MO MP MQ MR MS MT MU MV MW MX MY MZ NA NC NE NF NG NI NL NO NP NR NU NZ OM PA PE PF PG PH PK PL PM PN PR PS PT PW PY QA RE RO RS RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS ST SV SX SY SZ TC TD TF TG TH TJ TK TL TM TN TO TR TT TV TW TZ UA UG UM US UY UZ VA VC VE VG VI VN VU WF WS YE YT ZA ZM ZW'.toLowerCase().split(' ').filter(e => e);
console.log(iso2List.slice(0, 5));
const map = iso2List.reduce((prev, iso2) => {
console.log({ iso2 });
const { thousandsSeparatorSymbol, decimalSymbol } = getNumberFormatFromIso2(iso2);
if (thousandsSeparatorSymbol && decimalSymbol) {
prev[iso2] = [thousandsSeparatorSymbol, decimalSymbol]; // eslint-disable-line
}
return prev;
},
{});
return map;
};
JSON.stringify(genNumberFormat(), null, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment