readerFor.js 904 B

12345678910111213141516171819202122232425262728
  1. "use strict";
  2. var utils = require("../utils");
  3. var support = require("../support");
  4. var ArrayReader = require("./ArrayReader");
  5. var StringReader = require("./StringReader");
  6. var NodeBufferReader = require("./NodeBufferReader");
  7. var Uint8ArrayReader = require("./Uint8ArrayReader");
  8. /**
  9. * Create a reader adapted to the data.
  10. * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data to read.
  11. * @return {DataReader} the data reader.
  12. */
  13. module.exports = function (data) {
  14. var type = utils.getTypeOf(data);
  15. utils.checkSupport(type);
  16. if (type === "string" && !support.uint8array) {
  17. return new StringReader(data);
  18. }
  19. if (type === "nodebuffer") {
  20. return new NodeBufferReader(data);
  21. }
  22. if (support.uint8array) {
  23. return new Uint8ArrayReader(utils.transformTo("uint8array", data));
  24. }
  25. return new ArrayReader(utils.transformTo("array", data));
  26. };