Last active
June 5, 2019 03:09
-
-
Save nkabrown/6c08453f3a31e5fc5130b45856d53261 to your computer and use it in GitHub Desktop.
Return a default value when passing empty arrays to Intl.ListFormat
This file contains 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 countryList = ['Mexico', 'United States', 'Canada']; | |
// prefer not to use the Oxford comma so use locale 'en-GB' | |
var phraseFormatter = new Intl.ListFormat('en-GB', {type: 'conjunction'}); | |
phraseFormatter.default = 'North America'; | |
// see http://exploringjs.com/es6/ch_proxies.html#_intercepting-method-calls | |
const handler = { | |
get(target, key) { | |
return function(args) { | |
console.log(args); | |
return key == 'format' && args.length == 0 ? target.default : target.format(args); | |
}; | |
} | |
} | |
var phraseProxy = new Proxy(phraseFormatter, handler); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment