dice-roller / rpg-dice-roller

An advanced JS based dice roller that can roll various types of dice and modifiers, along with mathematical equations.
https://dice-roller.github.io/documentation
MIT License
238 stars 57 forks source link

Node Js flatMap is not defined #78

Closed OlivierDa closed 5 years ago

OlivierDa commented 5 years ago

Hello ! I ran into an issue with the latest version (3.1.0) in nodeJs When calling :

const { DiceRoller } = require('rpg-dice-roller');
const diceRoller = new DiceRoller();
let roll = diceRoller.roll('1d100');

i got this error :

ReferenceError: flatMap is not defined
 at rollGroups (C:\git\project\node_modules\rpg-dice-roller\lib\umd\bundle.js:66659:15)

When i look a the function rollGroups i see :

return group.flatMap(function (elm) {...}

It seems a working flatmap function has been created in this script but outside the scope of rollGroups and this semantics is not supported by NodeJs... It should must be something like this : return flatMap(group,function(elem{...}))

Thanks in advance!

GreenImp commented 5 years ago

Hi @OlivierDa .flatMap() is a native ES method, on the Array prototype: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap

Unfortunately, nodeJS doesn't include .flatMap() until v11. So if you're using an earlier version it will throw an error.

I was naively thinking that no-one would hit this issue - looks like I was wrong. After checking it seems that Edge also doesn't suport .flatMap() yet either: https://caniuse.com/#search=flatmap

A (hopefully) quick fix for you is to upgrade nodejs to v11+ (Latest version is 12), but I realise that may not be possible depeding on your project, especially as v10 is the LTS release. I'll look at getting a small polyfill in place to fix the issue as soon as I can. Should be in the next day or two.

GreenImp commented 5 years ago

Okay, that should actually be working now, if you pull down the latest version (v3.1.1).

Please let me know if it's not.

OlivierDa commented 5 years ago

Great news! I'm waiting for it to be available in Npm and confirm it !

GreenImp commented 5 years ago

Ah! Apologies, I forgot to publish it.. It should be available now: https://www.npmjs.com/package/rpg-dice-roller

OlivierDa commented 5 years ago

Unfortunately it is still not working... TypeError: Array.prototype.map.apply(...).flat is not a function It seems that flat is also nodejs11+: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat