shim.js 741B

123456789101112131415161718192021222324252627282930313233343536
  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) {
  10. return unpack(pack(value, 8, 23), 8, 23);
  11. };
  12. }());
  13. } else {
  14. toFloat32 = (function () {
  15. var float32Array = new Float32Array(1);
  16. return function (num) {
  17. float32Array[0] = num;
  18. return float32Array[0];
  19. };
  20. }());
  21. }
  22. module.exports = function (value) {
  23. if (isNaN(value)) return NaN;
  24. value = Number(value);
  25. if (value === 0) return value;
  26. if (!isFinite(value)) return value;
  27. return toFloat32(value);
  28. };