• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2// General purpose utility functions go in this file.
3
4
5function allAreFinite(args) {
6  for (var i = 0; i < args.length; i++) {
7    if (args[i] !== undefined && !Number.isFinite(args[i])) {
8      return false;
9    }
10  }
11  return true;
12}
13
14function toBase64String(bytes) {
15  if (isNode) {
16    return Buffer.from(bytes).toString('base64');
17  } else {
18    // From https://stackoverflow.com/a/25644409
19    // because the naive solution of
20    //     btoa(String.fromCharCode.apply(null, bytes));
21    // would occasionally throw "Maximum call stack size exceeded"
22    var CHUNK_SIZE = 0x8000; //arbitrary number
23    var index = 0;
24    var length = bytes.length;
25    var result = '';
26    var slice;
27    while (index < length) {
28      slice = bytes.slice(index, Math.min(index + CHUNK_SIZE, length));
29      result += String.fromCharCode.apply(null, slice);
30      index += CHUNK_SIZE;
31    }
32    return btoa(result);
33  }
34}
35
36