shim.js 736 B

123456789101112131415161718192021222324252627282930313233
  1. /* global Float32Array */
  2. // Credit: https://github.com/paulmillr/es6-shim/blob/master/es6-shim.js
  3. "use strict";
  4. var toFloat32;
  5. if (typeof Float32Array === "undefined") {
  6. toFloat32 = (function () {
  7. var pack = require("../_pack-ieee754")
  8. , unpack = require("../_unpack-ieee754");
  9. return function (value) { return unpack(pack(value, 8, 23), 8, 23); };
  10. })();
  11. } else {
  12. toFloat32 = (function () {
  13. var float32Array = new Float32Array(1);
  14. return function (num) {
  15. float32Array[0] = num;
  16. return float32Array[0];
  17. };
  18. })();
  19. }
  20. module.exports = function (value) {
  21. if (isNaN(value)) return NaN;
  22. value = Number(value);
  23. if (value === 0) return value;
  24. if (!isFinite(value)) return value;
  25. return toFloat32(value);
  26. };