]> git.cworth.org Git - empires-server/commitdiff
Use spread syntax rather than fill(nill) in generate_id
authorCarl Worth <cworth@cworth.org>
Wed, 10 Jun 2020 01:14:29 +0000 (18:14 -0700)
committerCarl Worth <cworth@cworth.org>
Wed, 10 Jun 2020 01:14:29 +0000 (18:14 -0700)
We don't actually need the array filled with null values specifically,
we just need something that map() will act on (since it doesn't act on
the empty entries we get from Array(4)). Using [...Array(4)] gives us
an array of undefined values which does the trick, and is a bit more
compact than .fill(null).

Of course, I blow away any savings in compactness with the giant
comment I've added here!

lmno.js

diff --git a/lmno.js b/lmno.js
index de95aa417a390caa65d26579a54d7e61a3106978..71ccc58165298b3e313fa7f60e0422fb19e3b7b6 100644 (file)
--- a/lmno.js
+++ b/lmno.js
@@ -101,7 +101,11 @@ class LMNO {
   }
 
   generate_id() {
-    return Array(4).fill(null).map(() => LMNO.letters.charAt(Math.floor(Math.random() * LMNO.letters.length))).join('');
+    /* Note: The copy from Array(4) to [...Array(4)] is necessary so
+     * that map() will actually work, (which it doesn't on an array
+     * from Array(N) which is in this strange state of having "empty"
+     * items rather than "undefined" as we get after [...Array(4)] */
+    return [...Array(4)].map(() => LMNO.letters.charAt(Math.floor(Math.random() * LMNO.letters.length))).join('');
   }
 
   create_game(engine_name) {