3 ms·
Intl API is new to me!
by kupopuffs 4y ago
Intl API is new to me!
- panzi 4y agoBroadcastChannel was the only thing new to me. I only knew the LocalStorage based hack (listening to the storage event and exchanging messages via the LocalStorage).
- Chris_Newton 4y agoIntl.Collator is a web UI superpower but apparently a well-kept secret: it gives you string sorting that takes into account local conventions for things like accented characters and even sorts strings containing numerical parts into natural rather than lexicographical order (that is, it correctly sorts “Item 1” before “Item 2” before “Item 10”).
- unsupp0rted 4y agoThis alone is worthy of its own post!
- jamesfinlayson 4y agoOh wow - lexicographical sorting built in to the browser!
- masklinn 4y agoIntl.DateTimeFormat is also really nicely done, because it limits the developer to "skeletons": you say what fields you want / need, and the browser will render what it has with at least those. Meaning the OS / user can override your whims if that's better or more readable. There's also formatRange which can provide localised range notations. Although later on formatToParts was added which allows the developer to ignore the settings and really only use the bits they wanted anyway.
- bakkoting 4y agoThe same options [1] are built into `String.prototype.localeCompare` (which is just a handy shortcut for calling `Collator` [2]). So for example: ``` ['B 1', 'A 20', 'A 3'].sort((a, b) => a.localeCompare(b, 'en', { numeric: true })) // ['A 3', 'A 20', 'B 1'] ``` [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#options https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... [2] https://tc39.es/ecma402/#sup-String.prototype.localeCompare https://tc39.es/ecma402/#sup-String.prototype.localeCompare