r/javascript • • Feb 21 '21

PHP 8 features I wish also existed in JavaScript

https://dev.to/devmount/php-8-features-i-wish-also-existed-in-javascript-1a5
0 Upvotes

6 comments sorted by

7

u/beavis07 Feb 21 '21

You can get named parameters by using an object as your parameter - not perfect but you can.

Optional chaining is already a thing and they’re are loads of libraries which implement a version of “match” along with a proposal to add to the language.

So i guess the authors dreams can come true 😂

2

u/ILikeChangingMyMind Feb 21 '21

As everyone else said, 2/3 of this article is (basically) already in Javascript (destructured object arguments are essentially named parameters). But, I have to say, that remaining 1/3 ... essentially of a better switch/case syntax ... would certainly be nice to have.

I don't use switch/case all that often (or at least, not since I left Redux years ago), but it still is relevant every now and then. The JS syntax is very heavy (and unnecessarily so) ... so much so that I sometimes use the ({1: x, 2: y})[switchVar] style instead, so having the PHP-style match would be an improvement.

3

u/Markavian Feb 21 '21

+1 for not using switch; object hash of functions or function names with a default is much more extendable and safer than the kinds of errors that can occur from a badly written switch statement.

2

u/CalgaryAnswers Feb 26 '21

Switch just has so much semantic overhead.

2

u/Markavian Feb 21 '21

I love PHP and JavaScript

  1. Named arguments can be done with object destructing in the arguments of a function

  2. Match looks like an object hash; not identical but very produceable if you think of code as config

  3. Optional chaining is coming finally! It's basically here!

I use PHP as a cheap webserver / API layer for Auth and RESTful JSON and filesystems, node js for command line, testing, and repository management tooling, and browser JS (Vue.js or vanilla js) for client side websites. It's a neat stack for many many things and doesn't require a degree in AWS/GCloud/Azure like my day job.

2

u/shuckster Feb 21 '21 edited Feb 22 '21

Didn't realise PHP had such nice language features these days!

Here's a JS solution for #2 just for fun:

// Helpers
//
const memoize = (fn, cache = new Map()) =>
  x => cache.has(x)
     ? cache.get(x)
     : cache.set(x, fn(x)).get(x)

const matchMaker = lookFor => 
                   haystack => 
                   needle => 
                     lookFor(needle, haystack)

const match = matchMaker((needle, haystack) => {
  const found = haystack
    .filter(x => Array.isArray(x[0]))
    .find(x => x[0].includes(needle))
  return found !== undefined 
    ? found[1] 
    : haystack.find(x => x.length === 1)[0]
})

// Implementation
//
const fontSize = memoize(match([
  [[100, 200],        "Super Thin"],
  [[300],             "Thin"],
  [[400, 500],        "Normal"],
  [[600, 700, 800],   "Bold"],
  [[900],             "Heavy"],
  [/* default */      "Not valid"],
]))

;[100, 200, 300, 400, 500, 600, 700, 800, 900, 901]
  .forEach(size => {
    console.log(`${size} = `, fontSize(size))
  })

// 100 =  Super Thin
// 200 =  Super Thin
// 300 =  Thin
// 400 =  Normal
// 500 =  Normal
// 600 =  Bold
// 700 =  Bold
// 800 =  Bold
// 900 =  Heavy
// 901 =  Not valid

I included a memoizer just so that it would (with usage) have the "lookup-iness" of switch, but obviously it's not quite as elegant as PHP's match!