Skip to content

Instantly share code, notes, and snippets.

@nkabrown
Last active June 5, 2019 03:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkabrown/6c08453f3a31e5fc5130b45856d53261 to your computer and use it in GitHub Desktop.
Save nkabrown/6c08453f3a31e5fc5130b45856d53261 to your computer and use it in GitHub Desktop.
Return a default value when passing empty arrays to Intl.ListFormat
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