sgratzl / chartjs-chart-boxplot

Chart.js Box Plots and Violin Plot Charts
https://www.sgratzl.com/chartjs-chart-boxplot/
MIT License
107 stars 24 forks source link

Remove use of window to check for Float64Array #109

Closed smarr closed 6 months ago

smarr commented 6 months ago

Hi,

I'd like to suggest this change to make your library work on Node.js as it previously did. Some changes of last year introduced the use of window, which is not available on Node.js.

I'd suggest to use typeof as it is always safe: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#interaction_with_undeclared_and_uninitialized_variables

Thank you! Stefan

sgratzl commented 6 months ago

I see your point but using just the type without a scope will result in an error when the type is not defined.

what about using globalThis instead? that should work in every scope

smarr commented 6 months ago

I see your point but using just the type without a scope will result in an error when the type is not defined.

I suppose globalThis also works, at least on the one Node.js version I tested, yes. Though, my point was that typeof is defined to be always safe (as described in the linked documentation). (And strictly speaking there's always the lexical scope.)

Perhaps this example demonstrates that it works: (for convenience https://jsfiddle.net/da1m53y4/)

function nothingDefined() {
  return typeof FooBar;
}

function notDefinedAccess() {
  return FooBar;
}

function definedAccess() {
  function FooBar() {}
  return FooBar;
}

function tryIt(fn) {
  try {
    return fn();
  } catch (e) {
    return e;
  }
}

console.log(tryIt(nothingDefined));    // prints undefined, demonstrating, typeof is safe
console.log(tryIt(notDefinedAccess));  // prints ReferenceError FooBar, as expected
console.log(tryIt(definedAccess));     // and just prints the FooBar
sgratzl commented 6 months ago

cool didn't know that. thx

smarr commented 6 months ago

Thank you :)