Hacker News new | ask | show | jobs
by c0nfused 2985 days ago
I rather like your syntax compared to the proposed version. It is less concise but must more consistent and parsable:

  return match(user, [
          [{first: $, middle: $, last: $}, (f, m, l) => f + ' ' + m + ' ' + l],
          [{first: $, last: $}           , (f, l) => f + ' ' + l],
          [_, 'unknown']
      ]);
1 comments

I think that this is not always guaranteed to work, because {first: $, middle: $, last: $} is the same as {first: $, last: $, middle: $}

... so calling Object.keys() is not necessarily going to use a consistent ordering. I think that is why they have to use the more verbose syntax in the other library.

I think you could do something like: {first: $.0, middle: $.1, last: $.2}, (f, m, l) => ... pretty easily, though.

I believe in es6 Object literals are guaranteed to keep their ordering, and Object.keys() will iterate through them in the order defined.
The ordering of Object.keys() is equal to insertion order of the keys into the object. So if you have an Object generation function, it can insert the keys in the proper order (and take advantage of stuff like v8 hidden classes)

http://2ality.com/2015/10/property-traversal-order-es6.html